-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogramming.js
159 lines (138 loc) · 3.88 KB
/
programming.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function minMovesToReachTarget(plan, currentFloor, targetFloor) {
let visited = new Array(Math.max(Math.max(...Object.keys(plan)), targetFloor) + 1).fill(false);
let queue = [[currentFloor, 0]];
while (queue.length > 0) {
let [currentFloor, moves] = queue.shift();
visited[currentFloor] = true;
if (currentFloor === targetFloor) {
return moves;
}
if (plan.hasOwnProperty(currentFloor)) {
let connectedFloors = plan[currentFloor];
for (let connectedFloor of connectedFloors) {
if (!visited[connectedFloor]) {
queue.push([connectedFloor, moves + 1]);
}
}
}
}
return -1;
}
// Example usage:
let plan = {
1: [2, 3],
2: [4, 5],
3: [6, 7],
4: [8, 9],
5: [10, 11]
};
let startFloor = 1;
let targetFloor = 11;
let result = minMovesToReachTarget(plan, startFloor, targetFloor);
console.log(result);
function floydTri(N) {
let num = 1;
for (let i = 1; i <= N; i++) {
let row = "";
for (let j = 0; j < i; j++) {
row += num + " ";
num++;
}
console.log(row);
}
}
// Calling the function with N=5
floydTri(5);
function lastWord(string = 'I am a human') {
const lastSpaceIndex = string.lastIndexOf(' ');
const word = string.substring(lastSpaceIndex + 1);
console.log(`The last word is '${word}' with length ${word.length}`);
}
// Calling the function
lastWord();
const spiral = (N) => {
const n = N.length;
let lengths = [];
for (let i = 1; i < n; i++) {
lengths.push(i, i)
}
lengths.push(n - 1);
let row = Math.floor(n / 2);
let col = Math.floor(n / 2);
let direction = 0;
let spiral = [N[row][col]]
for (let dir of lengths) {
for (let i = 0; i < dir; i++) {
if (direction === 0) {
row --;
} else if (direction === 1) {
col ++;
} else if (direction === 2) {
row ++;
} else {
col --;
}
spiral.push(N[row][col])
}
direction = (direction + 1) % 4
}
return spiral;
};
let spiral_matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(spiral(spiral_matrix))
const merge = function(arr) {
if (arr.length > 1) {
let mid = Math.floor(arr.length / 2);
let left = arr.slice(0, mid);
let right = arr.slice(mid, arr.length);
merge(left);
merge(right);
let i = j = k = 0;
while ( i < left.length && j < right.length ) {
if ( left[i] < right[j] ) {
arr[k] = left[i];
i ++;
} else {
arr[k] = right[j];
j ++;
}
k ++;
};
while ( i < left.length ) {
arr[k] = left[i];
i ++;
k ++;
};
while ( j < right.length ) {
arr[k] = right[j];
j ++;
k ++;
};
};
}
var arr = [9,4,1,76,23,4,2,0,-2,5,-5]
merge(arr)
console.log(arr)
let collatz = (n) => {
let result = String(n)
while ( ![1, 5, 17].includes(n) ) {
n = n % 2 == 0 ? Math.floor(n / 2) : n = 3 * n - 1;
result += `, ${String(n)}`;
};
return result;
};
console.log(collatz(3))
let get_repeated_items = (cart) => {
let repeated_items = [];
for ( let item = 0; item < cart.length; item++) {
if ( cart.slice(item + 1).includes(cart[item]) && !repeated_items.includes(cart[item]) ) {
repeated_items.push(cart[item]);
};
};
return repeated_items;
}
console.log(get_repeated_items(['b','a','b','c','d','a','e']))