Skip to content

Commit

Permalink
w14 challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanous committed Apr 7, 2021
1 parent 0f0986e commit f4c0b6c
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 3 deletions.
93 changes: 93 additions & 0 deletions 2021/w14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Week 14 challenge

## ButY evaluator

For this week's challenge you will implement an **evaluator** for **math expressions** in the imaginary programming language **ButY**.

### Example

Here's an example of a simple math expression in ButY:

```text
5 × ⸨ ⸨ 18 ÷ 2 ⸩ − 7 ⸩
```

Which... yeah. Luckily you won't have to write a lexer or parser, because your evaluator will be given an **Abstract Syntax Tree** as input:

```js
{
type: 'BinaryExpression',
operator: 'multiply',
left: {
type: 'Literal',
value: 5
},
right: {
type: 'BinaryExpression',
operator: 'subtract',
left: {
type: 'BinaryExpression',
operator: 'divide',
left: {
type: 'Literal',
value: 18
},
right: {
type: 'Literal',
value: 2
}
},
right: {
type: 'Literal',
value: 7
}
}
}
```

In this example the expected return value is `10`.

#### Examples in code

```
evaluate({ type: 'Literal', value: 42 });
// returns 42
evaluate({
type: 'BinaryExpression',
operator: 'add',
left: { type: 'Literal', value: 32 },
right: { type: 'Literal', value: 10 },
});
// returns 42
```

### AST interface

```
type Expression = Literal | BinaryExpression
type Literal = {
type: 'Literal',
value: number
}
type BinaryExpression = {
type: 'BinaryExpression',
operator: 'add', 'subtract', 'multiply', 'divide',
left: Expression,
right: Expression
}
```

### Other notes

You do not need to account for dividing by 0.

### Author

Peter-Paul

### Upload link

### Results
7 changes: 7 additions & 0 deletions 2021/w14/solutions/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const evaluate = (ast) => {
// your solution here

return undefined;
};

module.exports = evaluate;
1 change: 1 addition & 0 deletions 2021/w14/test-cases/spec.json

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions 2021/w14/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const fs = require("fs");
const path = require("path");
const spec = require("./test-cases/spec.json");
const solutions = fs.readdirSync(path.resolve(__dirname, "solutions"));

describe.each(solutions)("%s", (filename) => {
const evaluate = require(path.resolve(__dirname, "solutions", filename));

test.each([
["42", 42, { type: "Literal", value: 42 }],
[
"42 + 10",
42,
{
type: "BinaryExpression",
operator: "add",
left: { type: "Literal", value: 32 },
right: { type: "Literal", value: 10 },
},
],
[
"5 × ⸨ ⸨ 18 ÷ 2 ⸩ − 7 ⸩",
10,
{
type: "BinaryExpression",
operator: "multiply",
left: { type: "Literal", value: 5 },
right: {
type: "BinaryExpression",
operator: "subtract",
left: {
type: "BinaryExpression",
operator: "divide",
left: { type: "Literal", value: 18 },
right: { type: "Literal", value: 2 },
},
right: { type: "Literal", value: 7 },
},
},
],
])("%s = %d", (expression, result, input) => {
expect(evaluate(input)).toBe(result);
});

test.each(spec)("spec[%#]", ({ inputs, result }) => {
expect(evaluate(...inputs)).toBe(result);
});
});
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ Update `evaluate.js` for the right challenge and then run `npm run eval [numRuns
| | Jan Bart | 18 | | 18 |
| | Daniel | 18 | | 18 |
| | Aman | 18 | | 18 |
| 12. | Sander L. | | | |
| 13. | Bram | | | |


### March
Expand Down
2 changes: 1 addition & 1 deletion evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { table } = require('table');
const humanizeDuration = require('humanize-duration');

// update for challenge you wish to evaluate, what is the tie margin and amount of default runs
const CHALLENGE = '2021/w13';
const CHALLENGE = '2021/w14';
const PERCENT_MARGIN_FOR_TIE = 5;
const TIMES_TO_EVAL_EACH = parseInt(process.argv[2], 10) || 1000;
const LOG_PAD = 35;
Expand Down

0 comments on commit f4c0b6c

Please sign in to comment.