This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PGroup { | |
// Your code here | |
constructor(values) { | |
this.values = values; | |
} | |
add(elem) { | |
if (this.has(elem)) { | |
return this; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here | |
function yourRobot({place, parcels}, route) { | |
if (route.length == 0) { | |
const routes = parcels.map(parcel => { | |
let route, pickingUp = false; | |
if (parcel.place != place) { | |
route = findRoute(roadGraph, place, parcel.place); | |
pickingUp = true; | |
} else { | |
route = findRoute(roadGraph, place, parcel.address); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
countSteps = (state, robot, memory) => { | |
for (let turn = 0;; turn++) { | |
if (state.parcels.length == 0) { | |
return turn; | |
} | |
let action = robot(state, memory); | |
state = state.move(action.direction); | |
memory = action.memory; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let map = {one: true, two: true, hasOwnProperty: true}; | |
// Fix this call | |
console.log(Object.prototype.hasOwnProperty.call(map, "one")); | |
// → true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here (and the code from the previous exercise) | |
class GroupIterator { | |
constructor(group) { | |
this.group = group; | |
this.i = 0; | |
} | |
next() { | |
if (this.i === this.group.values.length) { | |
return { done: true }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Group { | |
// Your code here. | |
constructor() { | |
this.values = []; | |
} | |
add(elem) { | |
if (!this.has(elem)) { | |
this.values.push(elem); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here. | |
class Vec { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
plus(parameter) { | |
return new Vec(this.x + parameter.x, this.y + parameter.y); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dominantDirection(text) { | |
// Your code here. | |
const directions = countBy(text, (char) => { | |
const script = characterScript(char.codePointAt(0)); | |
return script ? script.direction : 'none'; | |
}).filter(({ name }) => name != 'none'); | |
if (directions.length < 2) return directions[0].name; | |
return directions.reduce((a, b) => a.count > b.count ? a.name : b.name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
function every(array, test) { | |
// Your code here. | |
for (let elem of array) { | |
if (!test(elem)) return false; | |
} | |
return true; | |
} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here. | |
const loop = (init, test, next, body) => { | |
for (let i = init; test(i); i = next(i)) { | |
body(i); | |
} | |
}; | |
loop(3, n => n > 0, n => n - 1, console.log); | |
// → 3 | |
// → 2 |