Skip to content

Commit 4cf04ec

Browse files
committedMay 7, 2020
✨ add latest code
1 parent dda38b9 commit 4cf04ec

5 files changed

+275
-0
lines changed
 

‎episode_46/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Episode 46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// potentially slow...
2+
// function getStrongness(value) {
3+
// let strongness = 0;
4+
// while (value % 2 === 0) {
5+
// value /= 2; // TODO: use docs speedup
6+
// strongness++;
7+
// }
8+
// return strongness;
9+
// }
10+
11+
// function getStrongness(value) {
12+
// let strongness = 0;
13+
// while ((value & 1) === 0) {
14+
// value = value>>1; // TODO: use docs speedup
15+
// strongness++;
16+
// }
17+
// return strongness;
18+
// }
19+
20+
// function strongestEven(n, m){
21+
// const largest = {
22+
// value: 0,
23+
// strongness: 0,
24+
// };
25+
// const calculated = {}
26+
// for (let value = n; value <= m; value++) {
27+
// let strongness;
28+
// if (calculated[value / 2]) {
29+
// strongness = calculated[value / 2] + 1;
30+
// calculated[value] = strongness;
31+
// } else {
32+
// strongness = getStrongness(value);
33+
// calculated[value] = strongness;
34+
// }
35+
// if (strongness > largest.strongness) {
36+
// largest.value = value;
37+
// largest.strongness = strongness;
38+
// }
39+
// }
40+
// return largest.value;
41+
// }
42+
43+
// const lookup = [
44+
// 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17,
45+
// 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
46+
// ];
47+
48+
// function getStrongness(value) {
49+
// return lookup[(value & -value) % 37];
50+
// }
51+
52+
// const clz = Math.clz32;
53+
// function getStrongness(integer){ // count trailing zeros
54+
// // 1. fill in all the higher bits after the first one
55+
// integer |= integer << 16;
56+
// integer |= integer << 8;
57+
// integer |= integer << 4;
58+
// integer |= integer << 2;
59+
// integer |= integer << 1;
60+
// // 2. Now, inversing the bits reveals the lowest bits
61+
// return 32 - clz(~integer) |0; // `|0` ensures integer coercion
62+
// }
63+
64+
// function strongestEven(n, m){
65+
// const largest = {
66+
// value: 0,
67+
// strongness: 0,
68+
// };
69+
// let start = n % 2 === 0 ? n : n + 1;
70+
// for (let value = start; value <= m; value+=2) {
71+
// let strongness = getStrongness(value);
72+
// if (strongness > largest.strongness) {
73+
// largest.value = value;
74+
// largest.strongness = strongness;
75+
// }
76+
// }
77+
// return largest.value;
78+
// }
79+
80+
const strongestEven = (m, n) => {
81+
const N=BigInt(n), M=BigInt(m);
82+
for (let b=1n<<53n, x; b>1n; b>>=1n)
83+
if ((x=(N+b-1n)/b*b)<=M) return Number(x);
84+
return n;
85+
}
86+
87+
// thanks to doc
88+
89+
// x>>1 divides by 2 pretty quickly
90+
91+
// idea: search odd multiples of powers of two from large to small that fit in the range, so multiples of 2^31 then 2^30 then 2**29 ...
92+
93+
// instead of searching all numbers, searching from big powers of two -> small powers of two (all odd multiples of them) will be more fasterer. There's probably some even faster number theory way using prime decomposition?
94+
95+
// completely unhelpful comment: fastest way to check if number is divisible by 2^N -> does the binary representation of the number in memory end in N or more zeroes (bit scan / count trailing zeroes x86 instruction) - in JS you could do toString(2) and count the zeroes but that will be slower
96+
97+
// just search for odd multiples of 2^31 in the range, then 2^30, then 2^29...
98+
99+
// Thank you mertcane
100+
// Consider this : The (strongness of 2N) is the (strongness of N) + 1, if you cache the strongness of N, you don't need to calculate it again
101+
102+
103+
// ❣️ Thank you larry
104+
// sorry, just coming in, isn't this just the number of trailing 0s in the binary
105+
106+
//and in that case, isn't it just the smallest even number >= some power of 2
107+
108+
// I think you can just count from powers of 2s, if not found, then the smallest even number between n and m, I think
109+
110+
// console.log(getStrongness(12), 2);
111+
// console.log(getStrongness(16), 4);
112+
// console.log(getStrongness(3), 0);
113+
114+
115+
// console.log(strongestEven(1, 2), 2);
116+
// console.log(strongestEven(5, 10), 8);
117+
// console.log(strongestEven(48, 56), 48);
118+
// console.log(strongestEven(129, 193), 192);
119+
120+
console.log(strongestEven(1180381085, 1590463313), 1342177280)
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function abbrevName(name) {
2+
// a place to store the first initial - initialized to the first letter in the name
3+
const firstInitial = name[0];
4+
// a place to store the second initial - initialized to the empty string
5+
let secondInitial = '';
6+
// iterate over the characters in the string - starting at the 2nd character
7+
for (let i = 1; i < name.length; i++) {
8+
const letter = name[i];
9+
// if the current character is a space
10+
if (letter === ' ') {
11+
// set the second initial to the next character
12+
secondInitial = name[i + 1];
13+
break;
14+
}
15+
}
16+
17+
// return the first initial concatenated with a period concatenated with the second initial
18+
return firstInitial.toUpperCase() + '.' + secondInitial.toUpperCase();
19+
}
20+
21+
function abbrevName(name) {
22+
const firstNameLastName = name.split(' ');
23+
const firstName = firstNameLastName[0];
24+
const lastName = firstNameLastName[1];
25+
const firstInitial = firstName[0];
26+
const lastInitial = lastName[0];
27+
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
28+
}
29+
30+
function abbrevName(name) {
31+
const firstNameLastName = name.split(' ');
32+
return firstNameLastName[0][0].toUpperCase() + '.' + firstNameLastName[1][0].toUpperCase();
33+
}
34+
35+
function abbrevName(name) {
36+
return name.split(' ')[0][0].toUpperCase() + '.' + name.split(' ')[1][0].toUpperCase();
37+
}
38+
39+
function abbrevName(name) {
40+
const [firstName, lastName] = name.split(' ');
41+
const firstInitial = firstName[0];
42+
const lastInitial = lastName[0];
43+
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
44+
}
45+
46+
function abbrevName(name) {
47+
const [{
48+
0: firstInitial
49+
}, {
50+
0: lastInitial
51+
}] = name.split(' ');
52+
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
53+
}
54+
55+
function abbrevName(name) {
56+
const [[firstInitial], [lastInitial]] = name.split(' ');
57+
return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
58+
}
59+
60+
function abbrevName(name) {
61+
const firstNameLastName = name.split(' ');
62+
const firstName = firstNameLastName[0];
63+
const lastName = firstNameLastName[1];
64+
const firstInitial = firstName[0];
65+
const lastInitial = lastName[0];
66+
// return firstInitial.toUpperCase() + '.' + lastInitial.toUpperCase();
67+
// return `${firstInitial}.${lastInitial}`.toUpperCase();
68+
return (firstInitial + '.' + lastInitial).toUpperCase();
69+
}
70+
71+
function abbrevName(name) {
72+
return name.replace(/([a-z])[a-z]* ([a-z])[a-z]*/i, '$1.$2').toUpperCase();
73+
}
74+
75+
console.log(abbrevName('Sam Harris'), 'S.H');
76+
console.log(abbrevName('Patrick Feenan'), 'P.F');
77+
console.log(abbrevName('Evan Cole'), 'E.C');
78+
console.log(abbrevName('P Favuzzi'), 'P.F');
79+
console.log(abbrevName('David Mendieta'), 'D.M');
80+
console.log(abbrevName('david mendieta'), 'D.M');
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// TODO: rename this file to match slug
2+
3+
function isDivisible(n, x, y) {
4+
const isDivisibleByX = n % x === 0;
5+
const isDivisibleByY = n % y === 0;
6+
return isDivisibleByX && isDivisibleByY;
7+
}
8+
9+
function isDivisible(n, x, y) {
10+
return n % x === 0 && n % y === 0;
11+
}
12+
13+
function isDivisible(n, x, y) {
14+
return n % x == 0 && n % y == 0;
15+
}
16+
17+
function isDivisible(n, x, y) {
18+
const isDivisibleByX = n % x === 0;
19+
const isDivisibleByY = n % y === 0;
20+
if (isDivisibleByX && isDivisibleByY) {
21+
return true;
22+
} else {
23+
return false;
24+
}
25+
}
26+
27+
function isDivisible(n, x, y) {
28+
const isDivisibleByX = n % x === 0;
29+
const isDivisibleByY = n % y === 0;
30+
if (isDivisibleByX && isDivisibleByY) {
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
function isDivisible(n, x, y) {
37+
const isDivisibleByX = n % x === 0;
38+
const isDivisibleByY = n % y === 0;
39+
if (isDivisibleByX && isDivisibleByY) return true;
40+
return false;
41+
}
42+
43+
function isDivisible(n, x, y) {
44+
const isDivisibleByX = n % x === 0;
45+
const isDivisibleByY = n % y === 0;
46+
if (isDivisibleByX && isDivisibleByY) return true;
47+
else return false;
48+
}
49+
50+
function isDivisible(n, x, y) {
51+
const remainderOfDivisionByX = n % x;
52+
const remainderOfDivisionByY = n % y;
53+
if (!remainderOfDivisionByX && !remainderOfDivisionByY) return true;
54+
return false;
55+
}
56+
57+
console.log(isDivisible(3,3,4), false);
58+
console.log(isDivisible(12,3,4), true);
59+
console.log(isDivisible(8,3,4), false);
60+
console.log(isDivisible(48,3,4), true);

‎episode_47/multiply.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function multiply(a, b) {
2+
return a * b;
3+
}
4+
5+
// const multiply = (a, b) => {
6+
// return a * b;
7+
// };
8+
9+
// const multiply = (a, b) => a * b;
10+
11+
const result = multiply(21, 2);
12+
console.log(result);
13+
const result2 = multiply(13, 5);
14+
console.log(result2);

0 commit comments

Comments
 (0)
Please sign in to comment.