-
-
Notifications
You must be signed in to change notification settings - Fork 272
London | 26-ITP-Jan | Zadri Abdule | Sprint 1 | Data - Groups #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3bdd828
28d0f37
6659b4b
cf5f740
b63ad42
bb53ea2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,20 @@ | ||
| function dedupe() {} | ||
| // Remove duplicate primitive values from an array while preserving | ||
| // the first occurrence order. Does not mutate the input array. | ||
| function dedupe(list) { | ||
| // Defensive: if input is not an array, return an empty array | ||
| if (!Array.isArray(list)) return []; | ||
|
|
||
| const seen = new Set(); | ||
| const out = []; | ||
|
|
||
| for (const item of list) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| out.push(item); | ||
| } | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return -Infinity; | ||
|
|
||
| // Keep only finite numbers | ||
| const numbers = elements.filter( | ||
| (x) => typeof x === "number" && Number.isFinite(x) | ||
| ); | ||
| if (numbers.length === 0) return -Infinity; | ||
|
|
||
| // Find max without mutating the input | ||
| let max = -Infinity; | ||
| for (const n of numbers) { | ||
| if (n > max) max = n; | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,47 @@ const findMax = require("./max.js"); | |
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given an array with both positive and negative numbers, returns the largest number", () => { | ||
| expect(findMax([-10, 0, 5, 3])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("given an array with just negative numbers, returns the closest one to zero", () => { | ||
| expect(findMax([-10, -5, -3])).toBe(-3); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
|
|
||
| test("given an array with decimal numbers, returns the largest decimal number", () => { | ||
| expect(findMax([1.5, 2.3, 0.7])).toBe(2.3); | ||
| }); | ||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| test("given an array with non-number values, returns the max and ignores non-numeric values", () => { | ||
| expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("given an array with only non-number values, returns -Infinity", () => { | ||
| expect(findMax(["hey", "hi", "there"])).toBe(-Infinity); | ||
| }); | ||
|
Comment on lines
+53
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a string representing a valid numeric literal (for example, To test if the function can correctly ignore non-numeric values,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'll add a test that includes a numeric-looking string("300") to ensure those strings are ignored, and implement findMax to only consider real number values. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
|
|
||
| let total = 0; | ||
| for (const el of elements) { | ||
| if (typeof el === "number") { | ||
| total += el; | ||
| } | ||
| } | ||
|
Comment on lines
+5
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you expect from the following function calls (on extreme cases)?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks - great question. I chose to ignore non finite values so the function only adds finite numbers. That means: |
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,46 @@ const sum = require("./sum.js"); | |
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(sum([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with multiple numbers | ||
| // When passed to the sum function | ||
| // Then it should return the total sum of those numbers | ||
| test("given an array with multiple numbers, returns the correct sum", () => { | ||
| expect(sum([10, 20, 30])).toBe(60); | ||
| }); | ||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| test("given an array with negative numbers, returns the correct sum", () => { | ||
| expect(sum([-10, 20, -5])).toBe(5); | ||
| }); | ||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given an array with decimal numbers, returns the correct sum", () => { | ||
| expect(sum([1.5, 2.5, 3.5])).toBe(7.5); | ||
| }); | ||
|
Comment on lines
+42
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of So the following could happen expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseCan you find a more appropriate way to test a value (that involves decimal number calculations) for equality? Suggestion: Look up
|
||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
| test("given an array containing non-number values, returns the correct sum", () => { | ||
| expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("given an array with only non-number values, returns 0", () => { | ||
| expect(sum(["hey", "hi", "there"])).toBe(0); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| // Read input relative to this file so script can be run from anywhere | ||
| const data = fs.readFileSync(path.join(__dirname, "input.txt"), "utf8"); | ||
| const changes = data | ||
| .split(/\r?\n/) | ||
| .map((s) => s.trim()) | ||
| .filter(Boolean) | ||
| .map(Number); | ||
|
|
||
| // Part 1: final frequency after one pass | ||
| const finalFrequency = changes.reduce((acc, change) => acc + change, 0); | ||
|
|
||
| // Part 2: first repeated cumulative frequency (iterate the list repeatedly) | ||
| function findFirstDuplicate(arr) { | ||
| const seen = new Set([0]); | ||
| let freq = 0; | ||
|
|
||
| while (true) { | ||
| for (const change of arr) { | ||
| freq += change; | ||
| if (seen.has(freq)) return freq; | ||
| seen.add(freq); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const firstDuplicate = findFirstDuplicate(changes); | ||
|
|
||
| console.log("Final frequency (part 1):", finalFrequency); | ||
| console.log("First repeated frequency (part 2):", firstDuplicate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a chance that, even though
outhas incorrect elements (for example,[]),the two tests on lines 27-28 could still pass. Can you figure out why, and then fix the tests accordingly?