-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay16.js
More file actions
131 lines (115 loc) · 2.27 KB
/
Copy pathDay16.js
File metadata and controls
131 lines (115 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Activty 1
//task-factoril recursive func
function fac(n) {
if (n == 0) {
return 1;
} else {
return n * fac(n - 1);
}
}
console.log(fac(5));
//task2--fibo
function fibo(n) {
if (n <= 1) {
return n;
} else {
return fibo(n - 1) + fibo(n - 2);
}
}
console.log(fibo(7));
//Activity 2---------
//task3
function arr(a) {
if (a.length == 0) {
return 0;
} else {
return a[0] + arr(a.slice(1));
}
}
a = [1, 2, 3, 4, 5, 3, 7];
console.log(arr(a));
//task4
function max(a, n) {
if (n == 1) {
return a[0];
} else {
const maxofrest = max(a, n - 1);
return Math.max(a[n - 1], maxofrest);
}
}
console.log(max(a, a.length));
//task5
function reverse(str) {
if (str === "") {
return "";
} else {
return str[str.length - 1] + reverse(str.slice(0, -1));
}
}
console.log(reverse("mrigaank"));
//task6
function palindrome(str, strt, end) {
if (strt >= end) {
return true;
}
if(str[strt]!==str[end]){
return false
}
return palindrome(str,strt+1,end-1)
}
function cpalindrome(str){
console.log(palindrome(str,0,str.length-1))
}
const testcases=[
"mkm",
"mrigaank",
"naman"
]
testcases.forEach((testcase,index)=>{
console.log(`test case${index+1}is palidrome ${cpalindrome(testcase)}`)
})
//task7---binar srcgh
function binary(a,s,e,ele){
if(s>e){
return -1
}
const mid=Math.floor((s+e)/2)
if(a[mid]===ele){
return `eleemnet founda at ${mid}`
}
if(a[mid]>ele){
return binary(a,mid-1,e,ele)
}
return binary(a,mid+1,e,ele)
}
function pbinary(a,ele){
return binary(a,0,arr.length-1,ele)
}
const testcas=[
{array:[1,2,3,4,4,5],target:4},
{array:[23,4,4,5,2],target:23}
]
testcas.forEach((testc,index)=>{
const {array,target}=testc
const result=pbinary(array,target)
console.log(result)
})
//task8
function occur(arr,target,index){
if(index===arr.length){
return 0;
}
const count=arr[index]===target?1:0;
return count +occur(arr,target,index+1);
}
function getc(arr,target){
return occur(arr,target,0)
}
const test=[
{array:[1,2,3,4,7],target:3},
{array:[12,3,3,4,4,5,2,7,7,88,2,2,2,22,3,3,],target:2}
]
test.forEach((tes,index)=>{
const {array,target}=tes;
console.log(`count:${getc(array,target)}`)
})