Browse Source

14: Difference of squares.

Frédéric G. MARAND 2 weeks ago
parent
commit
c5d29954a9

+ 13 - 0
difference-of-squares/.eslintignore

@@ -0,0 +1,13 @@
+!.meta
+
+# Protected or generated
+.git
+.vscode
+
+# When using npm
+node_modules/*
+
+# Configuration files
+.eslintrc.cjs
+babel.config.cjs
+jest.config.cjs

+ 38 - 0
difference-of-squares/.eslintrc.cjs

@@ -0,0 +1,38 @@
+module.exports = {
+  root: true,
+  parserOptions: {
+    tsconfigRootDir: __dirname,
+    project: ['./tsconfig.json'],
+  },
+  overrides: [
+    // Student provided files
+    {
+      files: ['*.ts'],
+      excludedFiles: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
+      extends: '@exercism/eslint-config-typescript',
+    },
+    // Exercism given tests
+    {
+      files: ['*.test.ts'],
+      excludedFiles: ['custom.test.ts'],
+      env: {
+        jest: true,
+      },
+      extends: '@exercism/eslint-config-typescript/maintainers',
+    },
+    // Student provided tests
+    {
+      files: ['custom.test.ts'],
+      env: {
+        jest: true,
+      },
+      extends: '@exercism/eslint-config-typescript',
+    },
+    // Exercism provided files
+    {
+      files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
+      excludedFiles: ['custom.test.ts'],
+      extends: '@exercism/eslint-config-typescript/maintainers',
+    },
+  ],
+}

+ 22 - 0
difference-of-squares/.exercism/config.json

@@ -0,0 +1,22 @@
+{
+  "authors": [],
+  "contributors": [
+    "masters3d",
+    "Scientifica96",
+    "SleeplessByte"
+  ],
+  "files": {
+    "solution": [
+      "difference-of-squares.ts"
+    ],
+    "test": [
+      "difference-of-squares.test.ts"
+    ],
+    "example": [
+      ".meta/proof.ci.ts"
+    ]
+  },
+  "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": "https://projecteuler.net/problem=6"
+}

+ 1 - 0
difference-of-squares/.exercism/metadata.json

@@ -0,0 +1 @@
+{"track":"typescript","exercise":"difference-of-squares","id":"f8956bff576543d7b33cdc91216601d0","url":"https://exercism.org/tracks/typescript/exercises/difference-of-squares","handle":"Fairgame","is_requester":true,"auto_approve":false}

File diff suppressed because it is too large
+ 3 - 0
difference-of-squares/.yarn/releases/yarn-3.6.4.cjs


+ 45 - 0
difference-of-squares/HELP.md

@@ -0,0 +1,45 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ yarn test
+```
+
+## Skipped tests
+
+In the test suites all tests but the first have been skipped.
+
+Once you get a test passing, you can enable the next one by changing `xit` to
+`it`.
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit difference-of-squares.ts` 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 [TypeScript track's documentation](https://exercism.org/docs/tracks/typescript)
+- The [TypeScript track's programming category on the forum](https://forum.exercism.org/c/programming/typescript)
+- [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:
+
+- [TypeScript QuickStart](https://www.typescriptlang.org/docs/handbook/release-notes/overview.html)
+- [ECMAScript 2015 Language Specification](https://www.ecma-international.org/wp-content/uploads/ECMA-262_6th_edition_june_2015.pdf) (pdf)
+- [Mozilla JavaScript Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)
+- [/r/typescript](https://www.reddit.com/r/typescript) is the TypeScript subreddit.
+- [StackOverflow](https://stackoverflow.com/questions/tagged/typescript) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

+ 31 - 0
difference-of-squares/README.md

@@ -0,0 +1,31 @@
+# Difference of Squares
+
+Welcome to Difference of Squares on Exercism's TypeScript 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
+
+### Contributed to by
+
+- @masters3d
+- @Scientifica96
+- @SleeplessByte
+
+### Based on
+
+Problem 6 at Project Euler - https://projecteuler.net/problem=6

+ 4 - 0
difference-of-squares/babel.config.cjs

@@ -0,0 +1,4 @@
+module.exports = {
+  presets: ['@exercism/babel-preset-typescript'],
+  plugins: [],
+}

+ 53 - 0
difference-of-squares/difference-of-squares.test.ts

@@ -0,0 +1,53 @@
+import { Squares } from './difference-of-squares';
+
+// eslint-disable-next-line no-global-assign
+xit = it
+describe('Squares', () => {
+  describe('up to 5', () => {
+    const squares = new Squares(5)
+
+    it('gets the square of sum', () => {
+      expect(squares.squareOfSum).toBe(225)
+    })
+
+    xit('gets the sum of squares', () => {
+      expect(squares.sumOfSquares).toBe(55)
+    })
+
+    xit('gets the difference', () => {
+      expect(squares.difference).toBe(170)
+    })
+  })
+
+  describe('up to 10', () => {
+    const squares = new Squares(10)
+
+    xit('gets the square of sum', () => {
+      expect(squares.squareOfSum).toBe(3025)
+    })
+
+    xit('gets the sum of squares', () => {
+      expect(squares.sumOfSquares).toBe(385)
+    })
+
+    xit('gets the difference', () => {
+      expect(squares.difference).toBe(2640)
+    })
+  })
+
+  describe('up to 100', () => {
+    const squares = new Squares(100)
+
+    xit('gets the square of sum', () => {
+      expect(squares.squareOfSum).toBe(25502500)
+    })
+
+    xit('gets the sum of squares', () => {
+      expect(squares.sumOfSquares).toBe(338350)
+    })
+
+    xit('gets the difference', () => {
+      expect(squares.difference).toBe(25164150)
+    })
+  })
+})

+ 21 - 0
difference-of-squares/difference-of-squares.ts

@@ -0,0 +1,21 @@
+export class Squares {
+    constructor(private count: number) {
+    }
+
+    get sumOfSquares(): number {
+        let sum = 0;
+        for (let i = 0; i <= this.count; i++) {
+            sum += i * i;
+        }
+        return sum;
+    }
+
+    get squareOfSum(): number {
+        let sum = this.count * (this.count + 1) / 2;
+        return sum * sum;
+    }
+
+    get difference(): number {
+        return this.squareOfSum-this.sumOfSquares;
+    }
+}

+ 19 - 0
difference-of-squares/jest.config.cjs

@@ -0,0 +1,19 @@
+module.exports = {
+  verbose: true,
+  projects: ['<rootDir>'],
+  testMatch: [
+    '**/__tests__/**/*.[jt]s?(x)',
+    '**/test/**/*.[jt]s?(x)',
+    '**/?(*.)+(spec|test).[jt]s?(x)',
+  ],
+  testPathIgnorePatterns: [
+    '/(?:production_)?node_modules/',
+    '.d.ts$',
+    '<rootDir>/test/fixtures',
+    '<rootDir>/test/helpers',
+    '__mocks__',
+  ],
+  transform: {
+    '^.+\\.[jt]sx?$': 'babel-jest',
+  },
+}

+ 32 - 0
difference-of-squares/package.json

@@ -0,0 +1,32 @@
+{
+  "name": "@exercism/typescript-difference-of-squares",
+  "version": "1.0.0",
+  "description": "Exercism exercises in Typescript.",
+  "private": true,
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/exercism/typescript"
+  },
+  "type": "module",
+  "engines": {
+    "node": "^18.16.0 || >=20.0.0"
+  },
+  "devDependencies": {
+    "@exercism/babel-preset-typescript": "^0.4.0",
+    "@exercism/eslint-config-typescript": "^0.5.0",
+    "@types/jest": "^29.5.2",
+    "@types/node": "~18.16.16",
+    "babel-jest": "^29.5.0",
+    "core-js": "~3.30.2",
+    "eslint": "^8.42.0",
+    "jest": "^29.5.0",
+    "typescript": "~5.0.4"
+  },
+  "scripts": {
+    "test": "yarn lint:types && jest --no-cache",
+    "lint": "yarn lint:types && yarn lint:ci",
+    "lint:types": "yarn tsc --noEmit -p .",
+    "lint:ci": "eslint . --ext .tsx,.ts"
+  },
+  "packageManager": "yarn@3.6.4"
+}

+ 28 - 0
difference-of-squares/tsconfig.json

@@ -0,0 +1,28 @@
+{
+  "display": "Configuration for Exercism TypeScript Exercises",
+  "compilerOptions": {
+    // Allows you to use the newest syntax, and have access to console.log
+    // https://www.typescriptlang.org/tsconfig#lib
+    "lib": ["ESNEXT", "dom"],
+    // Make sure typescript is configured to output ESM
+    // https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
+    "module": "ES2020",
+    // Since this project is using babel, TypeScript may target something very
+    // high, and babel will make sure it runs on your local Node version.
+    // https://babeljs.io/docs/en/
+    "target": "ESNext", // ESLint doesn't support this yet: "es2022",
+
+    "strict": true,
+    "esModuleInterop": true,
+    "skipLibCheck": true,
+    "forceConsistentCasingInFileNames": true,
+
+    // Because we'll be using babel: ensure that Babel can safely transpile
+    // files in the TypeScript project.
+    //
+    // https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
+    "isolatedModules": true
+  },
+  "include": ["*.ts", "*.tsx", ".meta/*.ts", ".meta/*.tsx"],
+  "exclude": ["node_modules"]
+}

Some files were not shown because too many files changed in this diff