-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-katas.js
More file actions
180 lines (141 loc) · 4.15 KB
/
5-katas.js
File metadata and controls
180 lines (141 loc) · 4.15 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Directions Reduction
*/
function dirReduc(arr){
const directionMap = new Map([
['NORTH', 'SOUTH'],
['SOUTH', 'NORTH'],
['WEST', 'EAST'],
['EAST', 'WEST']
])
// Loops up to but doesn't include last direction
for (let i = 0; i < (arr.length - 1); i++) {
if (directionMap.get(arr[i]) === arr[i + 1]) {
arr.splice(i, 2)
i -= 2
}
}
return arr
}
/*
Rot13
*/
function rot13(message){
let cipher = []
for (let i = 0; i < message.length; i++) {
// Uppercase
if (message[i] >= 'A' && message[i] <= 'Z')
cipher.push((message.charCodeAt(i) - 65 + 13) % 26 + 65)
// Lowercase
else if (message[i] >= 'a' && message[i] <= 'z')
cipher.push((message.charCodeAt(i) - 97 + 13) % 26 + 97)
// Not a letter
else
cipher.push(message.charCodeAt(i))
}
return String.fromCharCode(...cipher)
}
/*
Extract the domain name from a URL
*/
function domainName(url){
url = url.replace('http://', '')
url = url.replace('https://', '')
url = url.replace('www.', '')
return url.split('.')[0]
}
/*
Find the unique string
*/
function findUniq(arr) {
// For every element in the arr, we reducing it to its basic characters
// BbBb => b
// silvia && vasili => ailsv
const simpleArr = arr.map(
x => x.toLowerCase()
.split('')
.filter((x, i, subArr) => subArr.indexOf(x) === i)
.sort()
.join('')
)
// All but one of the elements in the arr will be the same so we find the unique one
const uniqueValue = simpleArr.filter((x, i) => i === simpleArr.indexOf(x) && i === simpleArr.lastIndexOf(x))
.join('')
// Its position in the simpleArr will correlate with the position of the unique string in the primary array
const pos = simpleArr.findIndex(x => x === uniqueValue)
return arr[pos]
}
// This was a challenge. I decided to simplify the problem into steps. First I cleaned up the input, after cleaning it up the problem was a lot easier.
/*
Find the unique string
*/
function movingShift(s, shift) {
const encryptedStr = s.split('')
.map((x, i) => {
// Not a letter
if (x.toLowerCase() === x.toUpperCase())
return x
// Lower Cased
else if (x === x.toLowerCase()) {
return String.fromCharCode((x.charCodeAt() - 97 + shift + i) % 26 + 97)
}
// Upper Cased
else if (x === x.toUpperCase())
return String.fromCharCode((x.charCodeAt() - 65 + shift + i) % 26 + 65)
})
.join('')
const subLen = Math.ceil(encryptedStr.length / 5)
return [
encryptedStr.slice(0, subLen),
encryptedStr.slice(subLen, subLen * 2),
encryptedStr.slice(subLen * 2, subLen * 3),
encryptedStr.slice(subLen * 3, subLen * 4),
encryptedStr.slice(subLen * 4, subLen * 5),
]
}
function demovingShift(arr, shift) {
return arr.join('')
.split('')
.map((x, i) => {
// Not a letter
if (x.toLowerCase() === x.toUpperCase())
return x
// Lower Cased
else if (x === x.toLowerCase()) {
let parShifted = x.charCodeAt() - 97 - shift - i
while (parShifted < 0) {
parShifted += 26
}
return String.fromCharCode(parShifted % 26 + 97)
}
// Upper Cased
else if (x === x.toUpperCase()) {
let parShifted = x.charCodeAt() - 65 - shift - i
while (parShifted < 0) {
parShifted += 26
}
return String.fromCharCode(parShifted % 26 + 65)
}
})
.join('')
}
// This was a fun challenge. I think the code readability can be improved in the areas where we performt the shift by using an external shift function
/*
Primes in numbers
*/
function primeFactors(n){
const factors = new Map()
let divisor = 2
while (n >= 2) {
if (n % divisor === 0) {
factors.set(divisor, factors.get(divisor) + 1 || 1)
n /= divisor
} else {
divisor++
}
}
let factorString = ''
factors.forEach((count, divisor) => factorString += `(${divisor}${count > 1 ? `**${count}` : ''})`)
return factorString
}
// This was a fun challenge, I was able to use short term evaluation within a map set and use the foreach method, a reduce would have been ideal