Skip to content

Instantly share code, notes, and snippets.

View yitonghe00's full-sized avatar

YitongHe yitonghe00

View GitHub Profile
class PGroup {
// Your code here
constructor(values) {
this.values = values;
}
add(elem) {
if (this.has(elem)) {
return this;
} else {
// 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);
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;
}
}
let map = {one: true, two: true, hasOwnProperty: true};
// Fix this call
console.log(Object.prototype.hasOwnProperty.call(map, "one"));
// → true
// 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 };
class Group {
// Your code here.
constructor() {
this.values = [];
}
add(elem) {
if (!this.has(elem)) {
this.values.push(elem);
}
// 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);
}
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);
}
/*
function every(array, test) {
// Your code here.
for (let elem of array) {
if (!test(elem)) return false;
}
return true;
}
*/
// 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