Skip to content
Snippets Groups Projects
Commit e4376f3a authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Designed and implemented exhaustive searches.

parent 67188590
Branches solution
No related tags found
No related merge requests found
...@@ -9,8 +9,8 @@ with a given tag. ...@@ -9,8 +9,8 @@ with a given tag.
Signature: filterPosts(X: list<Post>, t: string) → R: list<Post> Signature: filterPosts(X: list<Post>, t: string) → R: list<Post>
Precondition: [none] Precondition: [none]
Postcondition: Postcondition: R ⊆ X
Postcondition: Postcondition: ∀x∈X. x ∈ R ↔ t ∈ tags(x)
### JavaScript ### JavaScript
...@@ -18,7 +18,13 @@ Postcondition: ...@@ -18,7 +18,13 @@ Postcondition:
```js ```js
function filter(posts, tag) { function filter(posts, tag) {
// … const results = [];
for (const post of posts) { // "generate" step
if (post.tags.has(tag)) { // "check" step
results.push(post);
}
}
return results;
} }
``` ```
...@@ -37,12 +43,12 @@ and blue.) ...@@ -37,12 +43,12 @@ and blue.)
Signature: color(G = (V, E): graph) → C: map<vertex, color> Signature: color(G = (V, E): graph) → C: map<vertex, color>
Happy path: Happy path:
Precondition: Precondition: ∃X∈P^V. isValidColoring(G, X)
Postcondition: Postcondition: C ∈ P^V
Postcondition: Postcondition: isValidColoring(G, C)
Sad path: Sad path:
Precondition: Precondition: ¬∃X∈P^V. isValidColoring(G, X)
Postcondition: Postcondition: C = ⊥
### JavaScript ### JavaScript
...@@ -53,13 +59,12 @@ green, and blue.) ...@@ -53,13 +59,12 @@ green, and blue.)
```js ```js
function color(graph) { function color(graph) {
// … for (const coloring of mappings(graph.vertices, PALETTE)) { // combinatorial enumeration
for (const coloring of mappings(graph.vertices, PALETTE)) { if (isValidColoring(graph, coloring)) { // helper function
if (isValidColoring(graph, coloring)) { return coloring;
// …
} }
} }
// … return undefined;
} }
``` ```
...@@ -74,8 +79,8 @@ color). ...@@ -74,8 +79,8 @@ color).
#### Contract #### Contract
Signature: isValidColoring(G = (V, E): graph, C: map<vertex, color>) → r: boolean Signature: isValidColoring(G = (V, E): graph, C: map<vertex, color>) → r: boolean
Precondition: Precondition: C ∈ P^V
Postcondition: Postcondition: r ↔ ¬∃(u, v)∈E. C[u] = C[v]
#### JavaScript #### JavaScript
...@@ -87,7 +92,12 @@ vertices to colors.) ...@@ -87,7 +92,12 @@ vertices to colors.)
```js ```js
function isValidColoring(graph, coloring) { function isValidColoring(graph, coloring) {
// … for (const [source, destination] of graph.edges) {
if (coloring.get(source) === coloring.get(destination)) {
return false;
}
}
return true;
} }
``` ```
...@@ -103,22 +113,24 @@ set of items not exceeding the weight limit. ...@@ -103,22 +113,24 @@ set of items not exceeding the weight limit.
Signature: knapsack(I: set<item>, w: set<item> → number, W: number, v: set<item> → number) → K: set<item> Signature: knapsack(I: set<item>, w: set<item> → number, W: number, v: set<item> → number) → K: set<item>
Happy path: Happy path:
Precondition: Precondition: |options(I, w, W)| > 0
Postcondition: Postcondition: K ∈ options(I, w, W)
Postcondition: Postcondition: ∀X∈options(I, w, W). v(K) ≥ v(X)
Sad path: Sad path:
Precondition: Precondition: |options(I, w, W)| = 0
Postcondition: Postcondition: K = ⊥
### JavaScript ### JavaScript
```js ```js
function knapsack(items, getWeight, weightLimit, getValue) { function knapsack(items, getWeight, weightLimit, getValue) {
// … const result = undefined;
for (const option of options(items, getWeight, weightLimit)) { for (const option of options(items, getWeight, weightLimit)) {
// … if (result === undefined || getValue(option) > getValue(result)) {
result = option;
} }
// … }
return result;
} }
``` ```
...@@ -134,13 +146,19 @@ of items not exceeding the weight limit. ...@@ -134,13 +146,19 @@ of items not exceeding the weight limit.
Signature: options(I: set<item>, w: set<item> → number, W: number) → O: set<set<item>> Signature: options(I: set<item>, w: set<item> → number, W: number) → O: set<set<item>>
Precondition: [none] Precondition: [none]
Postcondition: Postcondition: O ⊆ 𝒫(I)
Postcondition: Postcondition: ∀X∈𝒫(I). X ∈ O ↔ w(X) ≤ W
#### JavaScript #### JavaScript
```js ```js
function options(items, getWeight, weightLimit) { function options(items, getWeight, weightLimit) {
// … const results = new Set();
for (const candidate of powerset(items)) {
if (getWeight(candidate) <= weightLimit) {
results.add(candidate);
}
}
return results;
} }
``` ```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment