From e22ff8b6e686bd3b18acd328d39160997e25a88a Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Thu, 29 Feb 2024 14:24:33 -0500 Subject: [PATCH 1/9] sections 4 & 5 --- exercises/palindrome/index.js | 15 ++++++++++++++- exercises/reversestring/index.js | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/exercises/palindrome/index.js b/exercises/palindrome/index.js index 58115ed243..2397956ca8 100644 --- a/exercises/palindrome/index.js +++ b/exercises/palindrome/index.js @@ -7,6 +7,19 @@ // palindrome("abba") === true // palindrome("abcdefg") === false -function palindrome(str) {} +// not ideal solution +function palindrome(str) { + return str.split('').every((char, i) => { + return char === str[str.length - i - 1]; + }); +} module.exports = palindrome; + +// optimal solution + +// function palindrome(str) { +// const reversed = str.split('').reverse().join(''); + +// return str === reversed; +// } diff --git a/exercises/reversestring/index.js b/exercises/reversestring/index.js index fc909037d4..b2d6ab2af0 100644 --- a/exercises/reversestring/index.js +++ b/exercises/reversestring/index.js @@ -6,6 +6,23 @@ // reverse('hello') === 'olleh' // reverse('Greetings!') === '!sgniteerG' -function reverse(str) {} +function reverse(str) { + let reversed = ''; + for (let char of str) { + reversed = char + reversed; + } + + return reversed; +} module.exports = reverse; + +// function reverse(str) { +// return str.split('').reverse().join(''); +// } + +// function reverse(str) { +// return str.split('').reduce((reversed, char) => char + reversed, ''); +// } + +// reverse('asdf'); From 50aacd11a682d33fb05d803c31a92c5e4846d30a Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:32:08 -0500 Subject: [PATCH 2/9] Sections 5 + 6 --- exercises/maxchar/index.js | 24 +++++++++++++++++++++++- exercises/reverseint/index.js | 11 ++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/exercises/maxchar/index.js b/exercises/maxchar/index.js index 12f32ba998..3e741630a4 100644 --- a/exercises/maxchar/index.js +++ b/exercises/maxchar/index.js @@ -5,6 +5,28 @@ // maxChar("abcccccccd") === "c" // maxChar("apple 1231111") === "1" -function maxChar(str) {} +function maxChar(str) { + const charMap = {}; + let max = 0; + let maxChar = ''; + + // for of loop to iterate over strings and arrays + for (let char of str) { + if (charMap[char]) { + charMap[char]++; + } else { + charMap[char] = 1; + } + } + + // for in loop to iterate over objects + for (let char in charMap) { + if (charMap[char] > max) { + max = charMap[char]; + maxChar = char; + } + } + return maxChar; +} module.exports = maxChar; diff --git a/exercises/reverseint/index.js b/exercises/reverseint/index.js index 5db3bfdee1..7db980e7ee 100644 --- a/exercises/reverseint/index.js +++ b/exercises/reverseint/index.js @@ -8,6 +8,15 @@ // reverseInt(-15) === -51 // reverseInt(-90) === -9 -function reverseInt(n) {} +function reverseInt(n) { + const num = n.toString().split('').reverse().join(''); + + // Without using Math.sign() + // if (n < 0) { + // return parseInt(num) * -1; + // } + + return parseInt(num) * Math.sign(n); +} module.exports = reverseInt; From 90ea208d1b959f478082243dce37402d73b07ee4 Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Fri, 8 Mar 2024 11:10:26 -0500 Subject: [PATCH 3/9] sections 7 + 8 --- exercises/chunk/index.js | 43 ++++++++++++++++++++++++++++++++++++- exercises/fizzbuzz/index.js | 14 +++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/exercises/chunk/index.js b/exercises/chunk/index.js index 8e7a800cfc..8c488f2c83 100644 --- a/exercises/chunk/index.js +++ b/exercises/chunk/index.js @@ -8,6 +8,47 @@ // chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]] // chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]] -function chunk(array, size) {} +function chunk(array, size) { + const chunked = []; + let index = 0; + + while (index < array.length) { + chunked.push(array.slice(index, index + size)); + index += size; + } + + return chunked; +} + +//first solution +// function chunk(array, size) { +// const chunked = []; + +// for (let element of array) { +// const last = chunked[chunked.length - 1]; +// if (!last || last.length === size) { +// chunked.push([element]); +// } else { +// last.push(element); +// } +// } +// return chunked; +// } + +// My attempt +// const chunkArr = []; +// for (let i = 0; i < array.length - 1; i++) { +// let count = 0; +// let subArr = []; +// if (count < size) { +// count++; +// subArr.push(array[i]); +// } else if (count === size) { +// subArr.push(array[i]); +// chunkArr.push(subArr); +// } +// } +// console.log(chunkArr); +// return chunkArr; module.exports = chunk; diff --git a/exercises/fizzbuzz/index.js b/exercises/fizzbuzz/index.js index 0aeab8873b..f7cdcc981f 100644 --- a/exercises/fizzbuzz/index.js +++ b/exercises/fizzbuzz/index.js @@ -12,6 +12,18 @@ // 4 // buzz -function fizzBuzz(n) {} +function fizzBuzz(n) { + for (let i = 1; i <= n; i++) { + if (i % 3 === 0 && i % 5 === 0) { + console.log('fizzbuzz'); + } else if (i % 3 === 0) { + console.log('fizz'); + } else if (i % 5 === 0) { + console.log('buzz'); + } else { + console.log(i); + } + } +} module.exports = fizzBuzz; From ab5ead17c71c2c40164cd58a5b1e9620e8fe2099 Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Fri, 8 Mar 2024 11:14:13 -0500 Subject: [PATCH 4/9] branch attempt --- exercises/chunk/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/chunk/index.js b/exercises/chunk/index.js index 8c488f2c83..6a0b6d4cfc 100644 --- a/exercises/chunk/index.js +++ b/exercises/chunk/index.js @@ -16,7 +16,6 @@ function chunk(array, size) { chunked.push(array.slice(index, index + size)); index += size; } - return chunked; } From 33c2a60ee1d99e9ae7774e70cfe287a895bf7dc6 Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:07:54 -0400 Subject: [PATCH 5/9] section 9 --- exercises/anagrams/index.js | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/exercises/anagrams/index.js b/exercises/anagrams/index.js index 35cd037015..426610b8b0 100644 --- a/exercises/anagrams/index.js +++ b/exercises/anagrams/index.js @@ -8,6 +8,65 @@ // anagrams('RAIL! SAFETY!', 'fairy tales') --> True // anagrams('Hi there', 'Bye there') --> False -function anagrams(stringA, stringB) {} +function anagrams(stringA, stringB) { + return cleanString(stringA) === cleanString(stringB); +} + +function cleanString(str) { + return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join(''); +} module.exports = anagrams; + +// first solution +// function anagrams(stringA, stringB) { +// const aCharMap = buildCharMap(stringA); +// const bCharMap = buildCharMap(stringB); + +// if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) { +// return false; +// } + +// for (let char in aCharMap) { +// if (aCharMap[char] !== bCharMap[char]) { +// return false; +// } +// } +// return true; +// } + +// function buildCharMap(str) { +// const charMap = {}; +// for (let char of str.replace(/[^\w]/g, '').toLowerCase()) { +// charMap[char] = charMap[char] + 1 || 1; +// } + +// return charMap; +// } + +//my half of an attempt +// function anagrams(stringA, stringB) { +// const stringAMap = {} +// const stringBMap ={} +// stringA = stringA.replace(/[^\w]/g, "").toLowerCase(); +// stringB = stringB.replace(/[^\w]/g, "").toLowerCase(); +// if(stringA.length != stringB.length){ +// return false +// } else { +// for(char of stringA){ +// if(charAMap[char]){ +// charAMap[char]++ +// } else { +// charAMap[char] = 1 +// } +// } +// for(char of stringB){ +// if(charBMap[char]){ +// charBMap[char]++ +// } else { +// charBMap[char] = 1 +// } +// } + +// } +// } From d33ab0287fd6d96bbda90c6799bf4c71ea4382b6 Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:44:30 -0400 Subject: [PATCH 6/9] sections 10-13 --- exercises/capitalize/index.js | 22 +++++++++++++++++++- exercises/pyramid/index.js | 38 +++++++++++++++++++++++++++++++++- exercises/steps/index.js | 39 ++++++++++++++++++++++++++++++++++- exercises/vowels/index.js | 35 ++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 4 deletions(-) diff --git a/exercises/capitalize/index.js b/exercises/capitalize/index.js index ba9cb5988a..c53a5ef025 100644 --- a/exercises/capitalize/index.js +++ b/exercises/capitalize/index.js @@ -7,6 +7,26 @@ // capitalize('a lazy fox') --> 'A Lazy Fox' // capitalize('look, it is working!') --> 'Look, It Is Working!' -function capitalize(str) {} +function capitalize(str) { + let result = str[0].toUpperCase(); + for (let i = 1; i < str.length; i++) { + if (str[i - 1] === ' ') { + result += str[i].toUpperCase(); + } else { + result += str[i]; + } + } + return result; +} module.exports = capitalize; + +//first solution +// function capitalize(str) { +// const words = []; +// for (let word of str.split(' ')) { +// words.push(word[0].toUpperCase() + word.slice(1)); +// } + +// return words.join(' '); +// } diff --git a/exercises/pyramid/index.js b/exercises/pyramid/index.js index 627bab341d..0ed72f5ff7 100644 --- a/exercises/pyramid/index.js +++ b/exercises/pyramid/index.js @@ -14,6 +14,42 @@ // ' ### ' // '#####' -function pyramid(n) {} +// recursive solution +function pyramid(n, row = 0, level = '') { + if (n === row) { + return; + } + + if (level.length === 2 * n - 1) { + console.log(level); + return pyramid(n, row + 1); + } + + const midpoint = Math.floor((2 * n - 1) / 2); + let add; + if (midpoint - row <= level.length && midpoint + row >= level.length) { + add = '#'; + } else { + add = ' '; + } + + pyramid(n, row, level + add); +} module.exports = pyramid; + +// iterative first solution +// function pyramid(n) { +// const midpoint = Math.floor((2 * n - 1) / 2); +// for (let row = 0; row < n; row++) { +// let level = ''; +// for (let column = 0; column < 2 * n - 1; column++) { +// if (midpoint - row <= column && midpoint + row >= column) { +// level += '#'; +// } else { +// level += ' '; +// } +// } +// console.log(level); +// } +// } diff --git a/exercises/steps/index.js b/exercises/steps/index.js index 400adc9ac0..df405e98dd 100644 --- a/exercises/steps/index.js +++ b/exercises/steps/index.js @@ -17,6 +17,43 @@ // '### ' // '####' -function steps(n) {} +// recursive solution +function steps(n, row = 0, stair = '') { + if (n === row) { + return; + } + + if (n === stair.length) { + console.log(stair); + return steps(n, row + 1); + } + + // turnary refactor + // const add = stair.length <= row ? '#' : ' '; + // steps(n, row, stair + add); + + if (stair.length <= row) { + stair += '#'; + } else { + stair += ' '; + } + + return steps(n, row, stair); +} module.exports = steps; + +// iterative solution +// function steps(n) { +// for (let row = 0; row < n; row++) { +// let stair = ''; +// for (let column = 0; column < n; column++) { +// if (column <= row) { +// stair += '#'; +// } else { +// stair += ' '; +// } +// } +// console.log(stair); +// } +// } diff --git a/exercises/vowels/index.js b/exercises/vowels/index.js index ad3b67b250..b0678fd993 100644 --- a/exercises/vowels/index.js +++ b/exercises/vowels/index.js @@ -7,6 +7,39 @@ // vowels('Why do you ask?') --> 4 // vowels('Why?') --> 0 -function vowels(str) {} +function vowels(str) { + // regx solution + + // if matches() finds a match it returns an array with any matches found else it returns null + const matches = str.match(/[aeiou]/gi); + return matches ? matches.length : 0; +} module.exports = vowels; + +// solution 1 +// function vowels(str) { +// let count = 0; +// const checker = 'aeiou'; + +// for (let char of str.toLowerCase()) { +// if (checker.includes(char)) { +// count++; +// } +// } +// return count; +// } + +// my solution +// function vowels(str) { +// const vowelsArr = ['a', 'e', 'i', 'o', 'u']; +// let count = 0; +// for (let char of str.toLowerCase()) { +// for (let vowel of vowelsArr) { +// if (char === vowel) { +// count++; +// } +// } +// } +// return count; +// } From f03abf9818812e80448e6f1c0f10a8d1dab5d5f8 Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:20:11 -0400 Subject: [PATCH 7/9] Sections 14-18 --- exercises/fib/index.js | 49 +++++++++++++++++++- exercises/matrix/index.js | 94 ++++++++++++++++++++++++++++++++++++++- exercises/queue/index.js | 14 +++++- exercises/weave/index.js | 14 +++++- exercises/weave/queue.js | 4 ++ 5 files changed, 171 insertions(+), 4 deletions(-) diff --git a/exercises/fib/index.js b/exercises/fib/index.js index 9efe84faf2..09dc1bbcc6 100644 --- a/exercises/fib/index.js +++ b/exercises/fib/index.js @@ -8,6 +8,53 @@ // Example: // fib(4) === 3 -function fib(n) {} +// recursive solution +// memoization + +function memoize(fn) { + const cache = {}; + return function (...args) { + if (cache[args]) { + return cache[args]; + } + + const result = fn.apply(this, args); + cache[args] = result; + + return result; + }; +} + +function slowFib(n) { + if (n < 2) { + return n; + } + + return fib(n - 1) + fib(n - 2); +} + +const fib = memoize(slowFib); module.exports = fib; + +//my iterative solution +// liner runtime +// function fib(n) { +// const result = [0, 1]; + +// for (let i = 1; i < n; i++) { +// let sum = result[i] + result[i - 1]; +// result.push(sum); +// } +// return result[n]; +// } + +// recursive solution +// is actually exponential runtime (very bad) +// function fib(n) { +// if (n < 2) { +// return n; +// } + +// return fib(n - 1) + fib(n - 2); +// } diff --git a/exercises/matrix/index.js b/exercises/matrix/index.js index 13bdc31f82..b7e26ac826 100644 --- a/exercises/matrix/index.js +++ b/exercises/matrix/index.js @@ -15,6 +15,98 @@ // [11, 16, 15, 6], // [10, 9, 8, 7]] -function matrix(n) {} +function matrix(n) { + // first creates the array or sub arrays + const results = []; + + for (let i = 0; i < n; i++) { + results.push([]); + } + + //declare variables + let counter = 1; + let startColumn = 0; + let endColumn = n - 1; + let startRow = 0; + let endRow = n - 1; + + // loop that runs if start and end points are not equal + while (startColumn <= endColumn && startRow <= endRow) { + //top row + for (let i = startColumn; i <= endColumn; i++) { + results[startRow][i] = counter; + counter++; + } + startRow++; + + // right column + for (let i = startRow; i <= endRow; i++) { + results[i][endColumn] = counter; + counter++; + } + endColumn--; + + //bottom row + for (let i = endColumn; i >= startColumn; i--) { + results[endRow][i] = counter; + counter++; + } + endRow--; + + // left column + for (let i = endRow; i >= startRow; i--) { + results[i][startColumn] = counter; + counter++; + } + startColumn++; + } + return results; +} module.exports = matrix; + +// Solution +// function matrix(n) { +// const results = []; + +// for (let i = 0; i < n; i++) { +// results.push([]); +// } +// let counter = 1; +// let startColumn = 0; +// let endColumn = n - 1; +// let startRow = 0; +// let endRow = n - 1; + +// while (startColumn <= endColumn && startRow <= endRow) { +// // Top row +// for (let i = startColumn; i <= endColumn; i++) { +// results[startRow][i] = counter; +// counter++; +// } +// startRow++; + +// // Right column +// for (let i = startRow; i <= endRow; i++) { +// results[i][endColumn] = counter; +// counter++; +// } +// endColumn--; + +// // Bottom row +// for (let i = endColumn; i >= startColumn; i--) { +// results[endRow][i] = counter; +// counter++; +// } +// endRow--; + +// // start column +// for (let i = endRow; i >= startRow; i--) { +// results[i][startColumn] = counter; +// counter++; +// } +// startColumn++; +// } + +// return results; +// } diff --git a/exercises/queue/index.js b/exercises/queue/index.js index e11ff2a13e..e475d6e58b 100644 --- a/exercises/queue/index.js +++ b/exercises/queue/index.js @@ -8,6 +8,18 @@ // q.add(1); // q.remove(); // returns 1; -class Queue {} +class Queue { + constructor() { + this.data = []; + } + + add(record) { + this.data.unshift(record); + } + + remove() { + return this.data.pop(); + } +} module.exports = Queue; diff --git a/exercises/weave/index.js b/exercises/weave/index.js index 3d49df9694..b5154225c8 100644 --- a/exercises/weave/index.js +++ b/exercises/weave/index.js @@ -24,6 +24,18 @@ const Queue = require('./queue'); -function weave(sourceOne, sourceTwo) {} +function weave(sourceOne, sourceTwo) { + const result = new Queue(); + while (sourceOne.peek() || sourceTwo.peek()) { + if (sourceOne.peek()) { + result.add(sourceOne.remove()); + } + if (sourceTwo.peek()) { + result.add(sourceTwo.remove()); + } + } + + return result; +} module.exports = weave; diff --git a/exercises/weave/queue.js b/exercises/weave/queue.js index 451eb90223..20044c2ef3 100644 --- a/exercises/weave/queue.js +++ b/exercises/weave/queue.js @@ -16,6 +16,10 @@ class Queue { remove() { return this.data.pop(); } + + peek() { + return this.data[this.data.length - 1]; + } } module.exports = Queue; From a96d931b65646f2eff3ad3fa37fcb167e9c41bfe Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:23:21 -0400 Subject: [PATCH 8/9] Sections 16-21 --- exercises/linkedlist/index.js | 151 +++++++++++++++++++++++++++++++++- exercises/linkedlist/test.js | 30 +++---- exercises/qfroms/index.js | 36 +++++++- exercises/stack/index.js | 18 +++- 4 files changed, 216 insertions(+), 19 deletions(-) diff --git a/exercises/linkedlist/index.js b/exercises/linkedlist/index.js index c75cc872b0..ba975f1500 100644 --- a/exercises/linkedlist/index.js +++ b/exercises/linkedlist/index.js @@ -2,8 +2,155 @@ // Implement classes Node and Linked Lists // See 'directions' document -class Node {} +class Node { + constructor(data, next = null) { + this.data = data; + this.next = next; + } +} -class LinkedList {} +class LinkedList { + constructor() { + this.head = null; + } + + insertFirst(data) { + this.insertAt(data, 0); + } + + size() { + let counter = 0; + let node = this.head; + while (node) { + counter++; + node = node.next; + } + return counter; + } + + getFirst() { + return this.getAt(0); + } + + getLast() { + // if (!this.head) { + // return null; + // } + + // let node = this.head; + // while (node) { + // if (!node.next) { + // return node; + // } + // node = node.next; + // } + return this.getAt(this.size() - 1); + } + + clear() { + this.head = null; + } + + removeFirst() { + if (!this.head) { + return; + } + + this.head = this.head.next; + } + + removeLast() { + if (!this.head) { + return; + } + if (!this.head.next) { + this.head = null; + return; + } + let previous = this.head; + let node = this.head.next; + while (node.next) { + previous = node; + node = node.next; + } + previous.next = null; + } + + insertLast(data) { + const last = this.getLast(); + if (last) { + // There are some existing nodes in our chain + last.next = new Node(data); + } else { + // The chain is empty! + this.head = new Node(data); + } + } + + getAt(num) { + let node = this.head; + let counter = 0; + + while (node) { + if (counter === num) { + return node; + } + counter++; + node = node.next; + } + return null; + } + + removeAt(num) { + if (!this.head) { + return; + } + + if (num === 0) { + this.head = this.head.next; + return; + } + + const previous = this.getAt(num - 1); + if (!previous || !previous.next) { + return; + } + previous.next = previous.next.next; + } + + insertAt(data, num) { + if (!this.head) { + this.head = new Node(data); + return; + } + + if (num === 0) { + this.head = new Node(data, this.head); + return; + } + + const previous = this.getAt(num - 1) || this.getLast(); + const insertData = new Node(data, previous.next); + previous.next = insertData; + } + + forEach(fn) { + let node = this.head; + let counter = 0; + while (node) { + fn(node, counter); + node = node.next; + counter++; + } + } + + *[Symbol.iterator]() { + let node = this.head; + while (node) { + yield node; + node = node.next; + } + } +} module.exports = { Node, LinkedList }; diff --git a/exercises/linkedlist/test.js b/exercises/linkedlist/test.js index 18bdb64ea6..02d4390911 100644 --- a/exercises/linkedlist/test.js +++ b/exercises/linkedlist/test.js @@ -10,7 +10,7 @@ test('Node is a class', () => { expect(typeof Node.prototype.constructor).toEqual('function'); }); -describe.skip('A Node', () => { +describe('A Node', () => { test('has properties "data" and "next"', () => { const node = new Node('a', 'b'); expect(node.data).toEqual('a'); @@ -18,7 +18,7 @@ describe.skip('A Node', () => { }); }); -describe.skip('Insert First', () => { +describe('Insert First', () => { test('appends a node to the start of the list', () => { const l = new List(); l.insertFirst(1); @@ -28,7 +28,7 @@ describe.skip('Insert First', () => { }); }); -describe.skip('Size', () => { +describe('Size', () => { test('returns the number of items in the linked list', () => { const l = new List(); expect(l.size()).toEqual(0); @@ -40,7 +40,7 @@ describe.skip('Size', () => { }); }); -describe.skip('GetFirst', () => { +describe('GetFirst', () => { test('returns the first element', () => { const l = new List(); l.insertFirst(1); @@ -50,7 +50,7 @@ describe.skip('GetFirst', () => { }); }); -describe.skip('GetLast', () => { +describe('GetLast', () => { test('returns the last element', () => { const l = new List(); l.insertFirst(2); @@ -60,7 +60,7 @@ describe.skip('GetLast', () => { }); }); -describe.skip('Clear', () => { +describe('Clear', () => { test('empties out the list', () => { const l = new List(); expect(l.size()).toEqual(0); @@ -74,7 +74,7 @@ describe.skip('Clear', () => { }); }); -describe.skip('RemoveFirst', () => { +describe('RemoveFirst', () => { test('removes the first node when the list has a size of one', () => { const l = new List(); l.insertFirst('a'); @@ -97,7 +97,7 @@ describe.skip('RemoveFirst', () => { }); }); -describe.skip('RemoveLast', () => { +describe('RemoveLast', () => { test('RemoveLast removes the last node when list is empty', () => { const l = new List(); expect(() => { @@ -135,7 +135,7 @@ describe.skip('RemoveLast', () => { }); }); -describe.skip('InsertLast', () => { +describe('InsertLast', () => { test('adds to the end of the list', () => { const l = new List(); l.insertFirst('a'); @@ -147,7 +147,7 @@ describe.skip('InsertLast', () => { }); }); -describe.skip('GetAt', () => { +describe('GetAt', () => { test('returns the node at given index', () => { const l = new List(); expect(l.getAt(10)).toEqual(null); @@ -164,7 +164,7 @@ describe.skip('GetAt', () => { }); }); -describe.skip('RemoveAt', () => { +describe('RemoveAt', () => { test('removeAt doesnt crash on an empty list', () => { const l = new List(); expect(() => { @@ -217,7 +217,7 @@ describe.skip('RemoveAt', () => { }); }); -describe.skip('InsertAt', () => { +describe('InsertAt', () => { test('inserts a new node with data at the 0 index when the list is empty', () => { const l = new List(); l.insertAt('hi', 0); @@ -272,7 +272,7 @@ describe.skip('InsertAt', () => { }); }); -describe.skip('ForEach', () => { +describe('ForEach', () => { test('applies a transform to each node', () => { const l = new List(); @@ -281,7 +281,7 @@ describe.skip('ForEach', () => { l.insertLast(3); l.insertLast(4); - l.forEach(node => { + l.forEach((node) => { node.data += 10; }); @@ -292,7 +292,7 @@ describe.skip('ForEach', () => { }); }); -describe.skip('for...of loops', () => { +describe('for...of loops', () => { test('works with the linked list', () => { const l = new List(); diff --git a/exercises/qfroms/index.js b/exercises/qfroms/index.js index 93af154f18..614fcf4637 100644 --- a/exercises/qfroms/index.js +++ b/exercises/qfroms/index.js @@ -14,6 +14,40 @@ const Stack = require('./stack'); -class Queue {} +class Queue { + constructor() { + this.first = new Stack(); + this.second = new Stack(); + } + + add(record) { + this.first.push(record); + } + + remove() { + while (this.first.peek()) { + this.second.push(this.first.pop()); + } + const record = this.second.pop(); + + while (this.second.peek()) { + this.first.push(this.second.pop()); + } + return record; + } + + peek() { + while (this.first.peek()) { + this.second.push(this.first.pop()); + } + + const record = this.second.peek(); + + while (this.second.peek()) { + this.first.push(this.second.pop()); + } + return record; + } +} module.exports = Queue; diff --git a/exercises/stack/index.js b/exercises/stack/index.js index 5c7c4608ca..7e95441874 100644 --- a/exercises/stack/index.js +++ b/exercises/stack/index.js @@ -10,6 +10,22 @@ // s.pop(); // returns 2 // s.pop(); // returns 1 -class Stack {} +class Stack { + constructor() { + this.data = []; + } + + push(record) { + this.data.push(record); + } + + pop() { + return this.data.pop(); + } + + peek() { + return this.data[this.data.length - 1]; + } +} module.exports = Stack; From dd230634a2e8142362af6598313e3f01cc7ad43e Mon Sep 17 00:00:00 2001 From: Tyler Furgalack <104029862+tylerfurgalack@users.noreply.github.com> Date: Sun, 14 Apr 2024 12:51:03 -0400 Subject: [PATCH 9/9] Sections 22-24 --- exercises/circular/index.js | 15 ++++++++++++++- exercises/fromlast/index.js | 16 +++++++++++++++- exercises/midpoint/index.js | 11 ++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/exercises/circular/index.js b/exercises/circular/index.js index 6a1a9a9cd9..e38a60189f 100644 --- a/exercises/circular/index.js +++ b/exercises/circular/index.js @@ -12,6 +12,19 @@ // c.next = b; // circular(l) // true -function circular(list) {} +function circular(list) { + let slow = list.getFirst(); + let fast = list.getFirst(); + + while (fast.next && fast.next.next) { + slow = slow.next; + fast = fast.next.next; + + if (slow === fast) { + return true; + } + } + return false; +} module.exports = circular; diff --git a/exercises/fromlast/index.js b/exercises/fromlast/index.js index e5c9f31a58..48dbe55657 100644 --- a/exercises/fromlast/index.js +++ b/exercises/fromlast/index.js @@ -11,6 +11,20 @@ // list.insertLast('d'); // fromLast(list, 2).data // 'b' -function fromLast(list, n) {} +function fromLast(list, n) { + let fast = list.getFirst(); + let slow = list.getFirst(); + while (n > 0) { + fast = fast.next; + n--; + } + + while (fast.next) { + fast = fast.next; + slow = slow.next; + } + + return slow; +} module.exports = fromLast; diff --git a/exercises/midpoint/index.js b/exercises/midpoint/index.js index 8bf6894ac1..0e584ad893 100644 --- a/exercises/midpoint/index.js +++ b/exercises/midpoint/index.js @@ -12,6 +12,15 @@ // l.insertLast('c') // midpoint(l); // returns { data: 'b' } -function midpoint(list) {} +function midpoint(list) { + let slow = list.getFirst(); // or list.head + let fast = list.getFirst(); + + while (fast.next && fast.next.next) { + slow = slow.next; + fast = fast.next.next; + } + return slow; +} module.exports = midpoint;