Skip to content

Commit

Permalink
wasm/difference-of-squares: download exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Nov 13, 2023
1 parent f194684 commit da91157
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 0 deletions.
14 changes: 14 additions & 0 deletions wasm/difference-of-squares/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
"excludedFiles": ["custom.spec.js"],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
25 changes: 25 additions & 0 deletions wasm/difference-of-squares/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [
"bushidocodes"
],
"files": {
"solution": [
"difference-of-squares.wat"
],
"test": [
"difference-of-squares.spec.js"
],
"example": [
".meta/proof.ci.wat"
]
},
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
"source": "Problem 6 at Project Euler",
"source_url": "http://projecteuler.net/problem=6",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
1 change: 1 addition & 0 deletions wasm/difference-of-squares/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"wasm","exercise":"difference-of-squares","id":"55f47a3c3e314cf4a85121dff1a6e5ff","url":"https://exercism.org/tracks/wasm/exercises/difference-of-squares","handle":"vpayno","is_requester":true,"auto_approve":false}
1 change: 1 addition & 0 deletions wasm/difference-of-squares/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
72 changes: 72 additions & 0 deletions wasm/difference-of-squares/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Help

## Running the tests

## Setup

Go through the setup [instructions for WebAssembly][docs-exercism-wasm] to install the necessary dependencies.

## Requirements

Install assignment dependencies:

```shell
# Using npm
npm install

# Alternatively using yarn
yarn
```

## Making the test suite pass

All exercises come with a test suite to help you validate your solution before submitting.
You can execute these tests by opening a command prompt in the exercise's directory, and then running:

```bash
# Using npm
npm test

# Alternatively using yarn
yarn test
```

In some test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest` to `test`.

## Writing custom tests

If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file:

```shell
exercism submit numbers.wat custom.spec.js
```

[docs-exercism-wasm]: https://exercism.org/docs/tracks/wasm/installation

## Submitting your solution

You can submit your solution using the `exercism submit difference-of-squares.wat` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [WebAssembly track's documentation](https://exercism.org/docs/tracks/wasm)
- The [WebAssembly track's programming category on the forum](https://forum.exercism.org/c/programming/wasm)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [/r/WebAssembly](https://www.reddit.com/r/WebAssembly/) is the WebAssembly subreddit.
- [Github issue tracker](https://github.com/exercism/wasm/issues) is where we track our development and maintenance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here.
21 changes: 21 additions & 0 deletions wasm/difference-of-squares/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions wasm/difference-of-squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Difference of Squares

Welcome to Difference of Squares on Exercism's WebAssembly Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first
ten natural numbers and the sum of the squares of the first ten
natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from
first principles; research is allowed, indeed, encouraged. Finding the best
algorithm for the problem is a key skill in software engineering.

## Source

### Created by

- @bushidocodes

### Based on

Problem 6 at Project Euler - http://projecteuler.net/problem=6
4 changes: 4 additions & 0 deletions wasm/difference-of-squares/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
presets: ["@exercism/babel-preset-javascript"],
plugins: [],
};
74 changes: 74 additions & 0 deletions wasm/difference-of-squares/difference-of-squares.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { compileWat, WasmRunner } from "@exercism/wasm-lib";

let wasmModule;
let currentInstance;

beforeAll(async () => {
try {
const watPath = new URL("./difference-of-squares.wat", import.meta.url);
const { buffer } = await compileWat(watPath);
wasmModule = await WebAssembly.compile(buffer);
} catch (err) {
console.log(`Error compiling *.wat: ${err}`);
process.exit(1);
}
});

describe("difference-of-squares", () => {
beforeEach(async () => {
currentInstance = null;

if (!wasmModule) {
return Promise.reject();
}
try {
currentInstance = await new WasmRunner(wasmModule);
return Promise.resolve();
} catch (err) {
console.log(`Error instantiating WebAssembly module: ${err}`);
return Promise.reject();
}
});

describe("Square the sum of the numbers up to the given number", () => {
test("square of sum 1", () => {
expect(currentInstance.exports.squareOfSum(1)).toBe(1);
});

xtest("square of sum 5", () => {
expect(currentInstance.exports.squareOfSum(5)).toBe(225);
});

xtest("square of sum 100", () => {
expect(currentInstance.exports.squareOfSum(100)).toBe(25502500);
});
});

describe("Sum the squares of the numbers up to the given number", () => {
xtest("sum of squares 1", () => {
expect(currentInstance.exports.sumOfSquares(1)).toBe(1);
});

xtest("sum of squares 5", () => {
expect(currentInstance.exports.sumOfSquares(5)).toBe(55);
});

xtest("sum of squares 100", () => {
expect(currentInstance.exports.sumOfSquares(100)).toBe(338350);
});
});

describe("Subtract sum of squares from square of sums", () => {
xtest("difference of squares 1", () => {
expect(currentInstance.exports.difference(1)).toBe(0);
});

xtest("difference of squares 5", () => {
expect(currentInstance.exports.difference(5)).toBe(170);
});

xtest("difference of squares 100", () => {
expect(currentInstance.exports.difference(100)).toBe(25164150);
});
});
});
16 changes: 16 additions & 0 deletions wasm/difference-of-squares/difference-of-squares.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(module
;; The name prefixed with $ is used to internally refer to functions via the call instruction
;; The string in the export instruction is the name of the export made available to the
;; embedding environment (in this case, Node.js). This is used by our test runner Jest.
(func $squareOfSum (export "squareOfSum") (param $max i32) (result i32)
(i32.const 42)
)

(func $sumOfSquares (export "sumOfSquares") (param $max i32) (result i32)
(i32.const 42)
)

(func (export "difference") (param $max i32) (result i32)
(call $squareOfSum (i32.const 42))
)
)
35 changes: 35 additions & 0 deletions wasm/difference-of-squares/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@exercism/wasm-difference-of-squares",
"description": "Exercism exercises in WebAssembly.",
"author": "Sean McBride",
"type": "module",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/wasm",
"directory": "exercises/practice/difference-of-squares"
},
"jest": {
"maxWorkers": 1
},
"devDependencies": {
"@babel/core": "^7.20.12",
"@exercism/babel-preset-javascript": "^0.2.1",
"@exercism/eslint-config-javascript": "^0.6.0",
"@types/jest": "^29.4.0",
"@types/node": "^18.13.0",
"babel-jest": "^29.4.2",
"core-js": "^3.27.2",
"eslint": "^8.34.0",
"jest": "^29.4.2"
},
"dependencies": {
"@exercism/wasm-lib": "^0.1.0"
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
"lint": "eslint ."
}
}

0 comments on commit da91157

Please sign in to comment.