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

Recorded work from Monday.

parent 1514dac9
No related branches found
No related tags found
No related merge requests found
...@@ -270,32 +270,32 @@ function isBalanced(parentheses) { ...@@ -270,32 +270,32 @@ function isBalanced(parentheses) {
## Quotienting a Free Monoid by Relations ## Quotienting a Free Monoid by Relations
* The sequence `` cancels to the identity. * The sequence `()` cancels to the identity.
* If we simplify any input by repeatedly removing occurrences of ``, we eventually end up with a string of the form `` where `` is . * If we simplify any input by repeatedly removing occurrences of `()`, we eventually end up with a string of the form `)*(*` where `*` is **the Kleene star**.
## Parentheses Monoid ## Parentheses Monoid
* Monoid: * Monoid:
* `P = (…, ∙, …)` where `(a, b) ∙ (c, d) = (…, …)` * `P = (𝐍 × 𝐍, ∙, (0, 0))` where `(a, b) ∙ (c, d) = (a + c - min(b, c), b + d - min(b, c))`
## Divide-and-Conquer Design ## Divide-and-Conquer Design
``` ```
In: '()(())' In: '()(())'
Out: Out: 0, 0
/ \ / \
/ \ / \
/ \ / \
In: '()(' In: '())' In: '()(' In: '())'
Out: Out: Out: 0, 1 Out: 1, 0
/ \ / \ / \ / \
/ \ / \ / \ / \
In: '(' In: ')(' In: '(' In: '))' In: '(' In: ')(' In: '(' In: '))'
Out: Out: Out: Out: Out: 0, 1 Out: 1, 1 Out: 0, 1 Out: 2, 0
/ \ / \ / \ / \
/ \ / \ / \ / \
In: ')' In: '(' In: ')' In: ')' In: ')' In: '(' In: ')' In: ')'
Out: Out: Out: Out: Out: 1, 0 Out: 0, 1 Out: 1, 0 Out: 1, 0
``` ```
## New Iterative Solution ## New Iterative Solution
...@@ -303,22 +303,22 @@ function isBalanced(parentheses) { ...@@ -303,22 +303,22 @@ function isBalanced(parentheses) {
```js ```js
function encode(character) { function encode(character) {
if (character === '(') { if (character === '(') {
return [, ]; return [0, 1];
} }
if (character === ')') { if (character === ')') {
return [, ]; return [1, 0];
} }
return [0, 0]; return [0, 0];
} }
function combine([leftCloses, leftOpens], [rightCloses, rightOpens]) { function combine([leftCloses, leftOpens], [rightCloses, rightOpens]) {
const cancellations = ; const cancellations = Math.min(leftOpens, rightCloses);
return [, ]; return [leftCloses + rightCloses - cancellations, leftOpens + rightOpens - cancellations];
} }
function iterativeReduce(parentheses) { function iterativeReduce(parentheses) {
let result = ; let result = [0, 0];
for (const element of ) { for (const element of [...parentheses].map(encode)) {
result = combine(result, element); result = combine(result, element);
} }
return result; return result;
...@@ -337,7 +337,7 @@ async function parallelReduce(parentheses, threadCount) { ...@@ -337,7 +337,7 @@ async function parallelReduce(parentheses, threadCount) {
jobs.push(job); jobs.push(job);
} }
const [closes, opens] = iterativeReduce(await Promise.all(jobs)); const [closes, opens] = iterativeReduce(await Promise.all(jobs));
return ; return closes === 0 && opens === 0;
} }
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment