Add lab 11 skeleton
This commit is contained in:
parent
ee767d8067
commit
869e89188d
9
journal/_src/posts/2022-10-19-lab-11.md
Normal file
9
journal/_src/posts/2022-10-19-lab-11.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
Title: Lab Eleven
|
||||||
|
Date: 2022-10-19T08:30:00
|
||||||
|
Tags: cs2613, lab, javascript
|
||||||
|
|
||||||
|
Sample description
|
||||||
|
<!-- more -->
|
||||||
|
|
||||||
|
## Sample Body
|
||||||
|
Sample Body
|
0
labs/L11/deep-equal.js
Normal file
0
labs/L11/deep-equal.js
Normal file
16
labs/L11/spec/deep-equal.spec.js
Normal file
16
labs/L11/spec/deep-equal.spec.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
describe("equal", function () {
|
||||||
|
let obj = {here: {is: "an"}, object: 2};
|
||||||
|
it("self", function () {
|
||||||
|
expect(deepEqual(obj,obj)).toBe(true);
|
||||||
|
});
|
||||||
|
it("null", function () {
|
||||||
|
expect(deepEqual(null,null)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("different", function () {
|
||||||
|
expect(deepEqual(obj, {here: 1, object: 2})).toBe(false);
|
||||||
|
});
|
||||||
|
it("equivalent", function () {
|
||||||
|
expect(deepEqual(obj, {here: {is: "an"}, object: 2})).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
13
labs/L11/spec/support/jasmine.json
Normal file
13
labs/L11/spec/support/jasmine.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"spec_dir": "spec",
|
||||||
|
"spec_files": [
|
||||||
|
"**/*[sS]pec.?(m)js"
|
||||||
|
],
|
||||||
|
"helpers": [
|
||||||
|
"helpers/**/*.?(m)js"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"stopSpecOnExpectationFailure": false,
|
||||||
|
"random": true
|
||||||
|
}
|
||||||
|
}
|
0
labs/L11/spec/village.spec.js
Normal file
0
labs/L11/spec/village.spec.js
Normal file
59
labs/L11/village.js
Normal file
59
labs/L11/village.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
const roads = [
|
||||||
|
"Alice's House-Bob's House", "Alice's House-Cabin",
|
||||||
|
"Alice's House-Post Office", "Bob's House-Town Hall",
|
||||||
|
"Daria's House-Ernie's House", "Daria's House-Town Hall",
|
||||||
|
"Ernie's House-Grete's House", "Grete's House-Farm",
|
||||||
|
"Grete's House-Shop", "Marketplace-Farm",
|
||||||
|
"Marketplace-Post Office", "Marketplace-Shop",
|
||||||
|
"Marketplace-Town Hall", "Shop-Town Hall"
|
||||||
|
];
|
||||||
|
|
||||||
|
function buildGraph(edges) {
|
||||||
|
let graph = Object.create(null);
|
||||||
|
function addEdge(from, to) {
|
||||||
|
if (graph[from] == null) {
|
||||||
|
graph[from] = [to];
|
||||||
|
} else {
|
||||||
|
graph[from].push(to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let [from, to] of edges.map(r => r.split("-"))) {
|
||||||
|
addEdge(from, to);
|
||||||
|
addEdge(to, from);
|
||||||
|
}
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
const roadGraph = buildGraph(roads);
|
||||||
|
|
||||||
|
class VillageState {
|
||||||
|
constructor(place, parcels) {
|
||||||
|
this.place = place;
|
||||||
|
this.parcels = parcels;
|
||||||
|
}
|
||||||
|
|
||||||
|
move(destination) {
|
||||||
|
if (!roadGraph[this.place].includes(destination)) {
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
let parcels = this.parcels.map(p => {
|
||||||
|
if (p.place != this.place) return p;
|
||||||
|
return { place: destination, address: p.address };
|
||||||
|
}).filter(p => p.place != p.address);
|
||||||
|
return new VillageState(destination, parcels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let first = new VillageState(
|
||||||
|
"Post Office",
|
||||||
|
[{ place: "Post Office", address: "Alice's House" }]
|
||||||
|
);
|
||||||
|
let next = first.move("Alice's House");
|
||||||
|
|
||||||
|
console.log(next.place);
|
||||||
|
// → Alice's House
|
||||||
|
console.log(next.parcels);
|
||||||
|
// → []
|
||||||
|
console.log(first.place);
|
||||||
|
// → Post Office
|
Loading…
Reference in New Issue
Block a user