diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..734eced5d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,26 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // validate input + if (!Array.isArray(list)) return null; + + // keep only finite numbers (ignore numeric strings and other types) + const numbers = list.filter( + (x) => typeof x === "number" && Number.isFinite(x) + ); + if (numbers.length === 0) return null; + + // sort numerically (we can sort the filtered array in-place) + numbers.sort((a, b) => a - b); + + const n = numbers.length; + const mid = Math.floor(n / 2); + + // odd length -> return middle element + if (n % 2 === 1) return numbers[mid]; + + // even length -> average of two middle values + return (numbers[mid - 1] + numbers[mid]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..cdb72a4a4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -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; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..c5859c9d4 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,28 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Given an empty array // When passed to the dedupe function // Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); +test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); + +test("array with no duplicates returns a copy", () => { + const arr = [1, 2, 3]; + const out = dedupe(arr); + expect(out).toEqual(arr); + expect(out).not.toBe(arr); // should be a new array +}); + +test("removes duplicates while preserving first occurrence order", () => { + const input = [5, 1, 1, 2, 3, 2, 5, 8]; + expect(dedupe(input)).toEqual([5, 1, 2, 3, 8]); +}); + +test("does not mutate the original array", () => { + const arr = ["a", "a", "b"]; + const copy = arr.slice(); + dedupe(arr); + expect(arr).toEqual(copy); +}); // Given an array with no duplicates // When passed to the dedupe function @@ -24,5 +45,5 @@ test.todo("given an empty array, it returns an empty array"); // Given an array of strings or numbers // When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..be0ef52b3 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -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; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..a35c61e1c 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -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); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..95a9c89d1 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -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; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..91b86f3bb 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -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); +}); // 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); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; + for (const element of list) { if (element === target) { return true; } diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..9263787c8 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -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);