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

Recorded work from Thursday.

parent 8cfae20b
Branches
No related tags found
No related merge requests found
......@@ -144,16 +144,16 @@ Problem: Given a list of numbers, compute their total.
## Monoid Identification
* Interpretation of input elements as actions:
*
* An element `a` in the input is an instruction to "add `a` to the total".
* Combination of two actions:
*
* If we add `a` and then we add `b`, that is the same as adding `a + b`.
* Representation of actions:
*
* We can represent any action by storing how much to add to the total.
* Identity element:
*
* Solving `e + x = x + e = x` for `e`, we get `e = 0`.
* Monoid:
* `A = (, , )`
* `A = (𝐑, +, 0)`
--------------------------------------------------------------------------------
......@@ -164,16 +164,16 @@ Problem: Given a list of numbers, compute their product.
## Monoid Identification
* Interpretation of input elements as actions:
*
* An element `a` in the input is an instruction to "multiply the result by `a`".
* Combination of two actions:
*
* If we multiply by `a` and then multiply by `b`, altogether, we are multiplying by `a ⋅ b`.
* Representation of actions:
*
* We can represent any action by storing how much to multiply by.
* Identity element:
*
* Solving `e ⋅ x = x ⋅ e = x` gives us `e = 1`.
* Monoid:
* `A = (, , )`
* `A = (𝐑, , 1)`
--------------------------------------------------------------------------------
......@@ -184,16 +184,16 @@ Problem: Given a list of numbers, compute the minimum.
## Monoid Identification
* Interpretation of input elements as actions:
*
* An element `a` in the input is an instruction to "ensure that the result is no larger than `a`".
* Combination of two actions:
*
* If we ensure that the result is no larger than `a` and then we ensure that the result is no larger than `b`, altogether, we we ensure that the result is no larger than `min(a, b)`.
* Representation of actions:
*
* We can represent an action by storing the cap on the result.
* Identity element:
*
* Solving `min(e, x) = min(x, e) = x` gives us `e = ∞`.
* Monoid:
* `A = (…, …, )`
* `A = (𝐑 ∪ {∞}, min, )`
--------------------------------------------------------------------------------
......@@ -212,22 +212,22 @@ As another way to look at the problem, if we know the average of a left-hand sid
This is the same issue we saw in divide-and-conquer design: we must be asking the wrong question.
If we ask for …, deferring …, then we can find a monoid.
If we ask for a total *and* a count, deferring the division, then we can find a monoid.
## Monoid Identification
* Interpretation of input elements as actions:
*
* An element `a` in the input is an instruction to "add `a` to the total and then `1` to the count".
* Combination of two actions:
*
*
* Special case: If we add `a` and then `c` to the total as well as `1` and `1` to count, altogether we add `a + c` to the total and `2` to the count.
* General case: If we we add `a` and then `c` to the total as well as `b` and `d` to count, altogether we add `a + c` to the total and `b + d` to the count.
* Representation of actions:
*
* We can represent any action by storing an amount to add to the total (a real number) and an amount to add to the count (a natural number).
* Identity element:
*
* Solving `e ∙ x = x ∙ e = x` gives us `e = (0, 0)`.
* Monoid:
* `A = (…, ∙, …)` where `(a, b) ∙ (c, d) = (…, …)`
* `A = (𝐑 × 𝐍, ∙, (0, 0))` where `(a, b) ∙ (c, d) = (a + c, b + d)`
--------------------------------------------------------------------------------
......@@ -241,7 +241,7 @@ Problem: Given a string of parentheses, determine if the parentheses are balance
## Clues about the Monoid from an Iterative Design
```
```js
function isBalanced(parentheses) {
let nesting = 0;
for (const character of parentheses) {
......@@ -300,7 +300,7 @@ function isBalanced(parentheses) {
## New Iterative Solution
```
```js
function encode(character) {
if (character === '(') {
return [, ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment