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

Designed exhaustive searches for the first two example problems.

parent 84f194d4
Branches
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:
``` ```
function filter(posts, tag) { function filter(posts, tag) {
// … const results = [];
for (const post of posts) { // "generate"
if (post.tags.has(tag)) { // "check"
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.)
``` ```
function color(graph) { function color(graph) {
// …
for (const coloring of mappings(graph.vertices, PALETTE)) { for (const coloring of mappings(graph.vertices, PALETTE)) {
if (isValidColoring(graph, coloring)) { 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.)
``` ```
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;
} }
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment