Skip to content

Commit 9bdcee9

Browse files
committed
✨ Add episode 26 links and solutions
1 parent 19872e5 commit 9bdcee9

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

episode_026/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Episode 26 - June 26th, 2019
2+
3+
## 7 kyu
4+
5+
* https://www.codewars.com/kata/two-fighters-one-winner/train/javascript
6+
7+
## 6 kyu
8+
9+
* https://www.codewars.com/kata/find-the-unique-number-1/train/javascript
10+
11+
## 5 kyu
12+
13+
*
14+
15+
## 4 kyu
16+
17+
*

episode_026/compare-number.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// naive solution...
2+
// function compare(a,b){
3+
// const numA = +a;
4+
// const numB = +b;
5+
// if (numA < numB) return 'less';
6+
// if (numA == numB) return 'equal';
7+
// if (numA > numB) return 'greater';
8+
// }
9+
10+
function compare(a, b, flipResult = false) {
11+
if (a[0] === '-' && b[0] !== '-') {
12+
return 'less';
13+
} else if (b[0] === '-' && a[0] !== '-') {
14+
return 'greater';
15+
} else if (a[0] === '-' && b[0] === '-') {
16+
flipResult = true;
17+
a = a.slice(1);
18+
b = b.slice(1);
19+
}
20+
21+
while (a[0] === '0') {
22+
a = a.slice(1);
23+
}
24+
25+
while (b[0] === '0') {
26+
b = b.slice(1);
27+
}
28+
29+
let aDecimal;
30+
let bDecimal;
31+
[a, aDecimal = ''] = a.split('.');
32+
[b, bDecimal = ''] = b.split('.');
33+
34+
// if the length of a is longer than the length of b
35+
if (a.length > b.length) return flipResult ? 'less' : 'greater';
36+
37+
// if the length of a is the same as b
38+
if (a.length === b.length) {
39+
for (let i = 0; i < a.length; i++) {
40+
const digitA = a[i];
41+
const digitB = b[i];
42+
if (digitA > digitB) {
43+
return flipResult ? 'less' : 'greater';
44+
} else if (digitA < digitB) {
45+
return flipResult ? 'greater' : 'less';
46+
}
47+
}
48+
if (aDecimal || bDecimal) {
49+
while (aDecimal[aDecimal.length - 1] === '0') {
50+
aDecimal = aDecimal.slice(0, aDecimal.length - 1);
51+
}
52+
53+
while (bDecimal[bDecimal.length - 1] === '0') {
54+
bDecimal = bDecimal.slice(0, bDecimal.length - 1);
55+
}
56+
57+
while (aDecimal.length < bDecimal.length) {
58+
aDecimal += '0';
59+
}
60+
61+
while (bDecimal.length < aDecimal.length) {
62+
bDecimal += '0';
63+
}
64+
return compare(aDecimal, bDecimal, flipResult);
65+
}
66+
return 'equal';
67+
}
68+
69+
// if the length of a is shorter than the length of b
70+
if (a.length < b.length) return flipResult ? 'greater' : 'less';
71+
}
72+
73+
74+
console.log(compare('2','12'),'less');
75+
console.log(compare('2','132'),'less');
76+
console.log(compare('12','13'),'less');
77+
console.log(compare('875','799'),'greater');
78+
console.log(compare('1000','1000'),'equal');
79+
console.log(compare('999','1000'),'less');
80+
console.log(compare('123','122'),'greater');
81+
console.log(compare('1000000000000000000000000000000000','1000000000000000000000000000000001'),'less');
82+
console.log(compare('1000000000000000000000000000000002','1000000000000000000000000000000001'),'greater');
83+
console.log(compare('0000000000000000000000000000000010000000000000000000000000000000000','0000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000001'),'greater');
84+
console.log(compare('012','13'),'less');
85+
console.log(compare('0000000.12','-120'),'greater');
86+
console.log(compare('000000000000000876','000999'),'less');
87+
console.log(compare('1000.00','1000'),'equal');
88+
console.log(compare('999','-00000000000001000.00'),'greater');
89+
console.log(compare('123.09','123.08'),'greater');
90+
console.log(compare('-123.09','-123.08'),'less');
91+
console.log(compare( '1000000000000000000000000000000002.00009','-1000000000000000000000000000000003'),'greater');
92+
console.log(compare('2.40', '2.4'), 'equal');
93+
console.log(compare('13.810', '13.701'), 'greater');
94+
console.log(compare('13.000000001', '13.1000000000'), 'less');
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function findUniq(arr) {
2+
// a place to store the counts of the numbers
3+
const counts = {};
4+
// iterate over the array
5+
for (let i = 0; i < arr.length; i++) {
6+
const number = arr[i];
7+
// increment the count of the current number in the counts object
8+
counts[number] = counts[number] || 0;
9+
counts[number]++;
10+
// if there are 2 numbers in the counts object
11+
const entries = Object.keys(counts);
12+
if (entries.length === 2) {
13+
const [a, b] = entries;
14+
if (counts[a] === 1 && counts[b] > 1) {
15+
return a;
16+
} else if (counts[b] === 1 && counts[a] > 1) {
17+
return b;
18+
}
19+
// return the one with only 1 count
20+
}
21+
}
22+
}
23+
24+
// function difference(setA, setB) {
25+
// let _difference = new Set(setA);
26+
// for (let elem of setB) {
27+
// _difference.delete(elem);
28+
// }
29+
// return _difference;
30+
// }
31+
32+
function findUniq(array) {
33+
if (array[0] !== array[1]) {
34+
if (array[0] === array[2]) {
35+
return array[1];
36+
} else {
37+
return array[0];
38+
}
39+
}
40+
41+
for (let i = 0; i < array.length; i++) {
42+
if (array[i + 1] !== array[i]) {
43+
return array[i + 1];
44+
}
45+
}
46+
}
47+
48+
function findUniq(array) {
49+
array.sort((a, b) => a - b);
50+
if (array[0] !== array[1]) {
51+
return array[0];
52+
} else {
53+
return array[array.length - 1];
54+
}
55+
}
56+
57+
58+
console.log(findUniq([0, 1, 0]), 1);
59+
console.log(findUniq([1, 1, 1, 2, 1, 1]), 2);
60+
console.log(findUniq([3, 10, 3, 3, 3]), 10);
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// class Fighter {
2+
// constructor(name, health, damagePerAttack) {
3+
// this.name = name;
4+
// this.health = health;
5+
// this.damagePerAttack = damagePerAttack;
6+
// }
7+
8+
// attack(fighter) {
9+
// fighter.takeDamage(this.damagePerAttack);
10+
// }
11+
12+
// takeDamage(damage) {
13+
// this.health -= damage;
14+
// }
15+
16+
// isAlive() {
17+
// return this.health > 0;
18+
// }
19+
// }
20+
21+
function Fighter(name, health, damagePerAttack) {
22+
this.name = name;
23+
this.health = health;
24+
this.damagePerAttack = damagePerAttack;
25+
26+
this.toString = function() { return this.name; };
27+
28+
// this.attack = (fighter) => {
29+
// fighter.takeDamage(this.damagePerAttack);
30+
// };
31+
32+
// this.takeDamage = (damage) => {
33+
// this.health -= damage;
34+
// };
35+
36+
// this.isAlive = () => {
37+
// return this.health > 0;
38+
// };
39+
}
40+
41+
Fighter.prototype.attack = function(fighter) {
42+
fighter.takeDamage(this.damagePerAttack);
43+
};
44+
45+
Fighter.prototype.takeDamage = function(damage) {
46+
this.health -= damage;
47+
};
48+
49+
Fighter.prototype.isAlive = function() {
50+
return this.health > 0;
51+
};
52+
53+
function declareWinner(fighter1, fighter2, firstAttackerName) {
54+
let currentAttacker = fighter1;
55+
let currentDefender = fighter2;
56+
if (fighter1.name !== firstAttackerName) {
57+
currentAttacker = fighter2;
58+
currentDefender = fighter1;
59+
}
60+
61+
while(fighter1.health > 0 && fighter2.health > 0) {
62+
currentAttacker.attack(currentDefender);
63+
if (!currentDefender.isAlive()) return currentAttacker.name;
64+
[currentDefender, currentAttacker] = [currentAttacker, currentDefender];
65+
}
66+
}
67+
68+
console.log(declareWinner(new Fighter('Lew', 10, 2), new Fighter('Harry', 5, 4), 'Lew'), 'Lew');
69+
console.log(declareWinner(new Fighter('Lew', 10, 2), new Fighter('Harry', 5, 4), 'Harry'), 'Harry');
70+
console.log(declareWinner(new Fighter('Harald', 20, 5), new Fighter('Harry', 5, 4), 'Harry'), 'Harald');
71+
console.log(declareWinner(new Fighter('Harald', 20, 5), new Fighter('Harry', 5, 4), 'Harald'), 'Harald');
72+
console.log(declareWinner(new Fighter('Jerry', 30, 3), new Fighter('Harald', 20, 5), 'Jerry'), 'Harald');
73+
console.log(declareWinner(new Fighter('Jerry', 30, 3), new Fighter('Harald', 20, 5), 'Harald'), 'Harald');

0 commit comments

Comments
 (0)