-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindWord-RemoveDuplicates-GetEvenAndSort.html
More file actions
148 lines (116 loc) · 3.42 KB
/
Copy pathfindWord-RemoveDuplicates-GetEvenAndSort.html
File metadata and controls
148 lines (116 loc) · 3.42 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
// How can you match substring of a sting?
// javascript approach
"Hello world".indexOf("world"); // it will work
// more cross language approach
function checkMatch(sentence, target){
var result = false;
var targetWord = target;
var splited = sentence.split('');
for( var j = 0; j < splited.length; j++ ){
if( targetWord[0] == splited[j] ) {
for( var i = 1; i< targetWord.length; i++ ){
j++;
if( targetWord[i] != splited[j] ){
break;
}
if(i == targetWord.length-1 ){
result = true;
}
}
}
}
return result
}
console.log(checkMatch("Hello world", "Hello") );
//===============================================================================
// How would you remove duplicate members from an array?
var duplicates = [ "w", "w", "w", "o", "r", "l" ];
var newArray = [];
for( var i = 0; i< duplicates.length; i++ ){
if(newArray.indexOf(duplicates[i]) == -1){
newArray.push(duplicates[i]);
}
}
console.log(newArray);
// ===================================
// Write a function (getEvenAndSort) that can be called on any array, and it returns the subarray of only the even numbers, but sorted.
// first - even 2, 4, 6,
// second - they should be sorted
var notSorted = [ 5, 2, 1, 4, 6 ];
// helper lowest
// make only even
function onlyEven(arr){
// before heapify we should make 0 element undefined
var output = [undefined];
for (var i = 0; i < arr.length; i++) {
if(arr[i] % 2 == 0){
output.push(arr[i]);
}
}
return output;
}
// sort array in heapify method
function heapify(arr){
fp = ffp(arr, arr.length-1)
while( fp != 1 ){
swap( fp, arr );
fp = ffp(arr, fp-1)
}
return arr
}
// helper heapify functions
function findLow( i, arr ){
var li = i;
if( arr[ li ] < arr[ i*2 ] ) {
li = i + 2
}
if( arr[ li ] < arr[ i*2 + 1 ] ) {
li = 2*i + 1
}
return li
}
function ffp(arr, i){
while( arr[ 2*i ] == undefined || arr[ 2*i + 1] == undefined ) {
i--
}
return i
}
function swap(fp, arr){
var idx = findLow(fp,arr);
if ( fp != idx ){
var temp = arr[fp];
arr[fp] = arr[idx];
arr[idx] = temp;
if ( arr[2*(2*fp)] ) {
swap(2*(2*fp), arr)
}
else if( arr[ 2 * (2*fp + 1) ] ){
swap(2 * (2*fp + 1) + 1, arr)
} else {
swap( Math.floor(fp/2), arr);
}
} else {
return false
// swap( Math.floor(fp/2), arr );
}
}
function getEvenAndSort(arr){
// find lowest
var outputArray = [];
// before heapify we should make 0 element undefined
var onlyEvenArray = onlyEven(arr);
outputArray = heapify(onlyEvenArray);
return outputArray;
}
console.log( getEvenAndSort(notSorted) )
</script>
</body>
</html>