This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const replaceRepeats = (input, number) => | |
| input.replaceAll( | |
| new RegExp(`${number}+`, "g"), | |
| x => x.length); | |
| // test | |
| [ | |
| replaceRepeats('1234500362000440', 0) === "1234523623441", | |
| replaceRepeats('000000000000', 0) == "12", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* solution */ | |
| const validate = ([y, z, ...rest]) => | |
| rest.length | |
| ? (rest.shift() === y && validate([z, y, ...rest])) | |
| : true; | |
| /* testing */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* solution */ | |
| function process([y, z, ...rest] = []) { | |
| while (rest.length) { | |
| if (rest.shift() !== y) return false; | |
| [z, y] = [y, z]; | |
| } | |
| return true; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const rotate = ([ head, ...tail]) => [...tail, head]; | |
| const explode = (num, acc = []) => | |
| num === 0 ? acc | |
| : explode(num - 1, [num, ...acc]) | |
| ; | |
| const build = (num, input, lines = []) => | |
| num === 0 ? lines | |
| : build(num - 1, rotate(input), [...lines, input]) | |
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // below here we can use functional structures again | |
| const suits = ["♠", "♥", "♣", "♦"]; | |
| const cards = ["A", "K", "Q", "J", "9", "8", "7", "6", "5", "4", "3", "2"]; | |
| function new_deck() { | |
| return suits.flatMap(suit => | |
| cards.map(card => `${card}${suit}`)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const maybeAdd = (prev, candidate) => | |
| prev.endsAt <= candidate[1] | |
| ? [prev, { | |
| chosen: [...prev.chosen, candidate[0]], | |
| endsAt: candidate[2] | |
| }] | |
| : [prev]; | |
| const combine = ts => | |
| ts.reduce( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const taskListEndsAt = ts => | |
| Math.max(...ts.map(t => t[2])); | |
| const maybeAdd = (ts, candidate) => | |
| (taskListEndsAt(ts) <= candidate[1]) | |
| ? [ts, [...ts, candidate]] | |
| : [ts] | |
| ; | |
| const combinations = ts => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function repeatedIntegers_0(n) { | |
| const ret = []; | |
| for(let i = 0; i <= n; i++) | |
| ret.push(...String(i).repeat(i).split("").map(i => Number(i))); | |
| return ret; | |
| } | |
| function* repeatedIntegers_1(n) { | |
| let i = 0; | |
| while(++i <= n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Set up: | |
| // - dotnet new console | |
| // - dotnet add package OneOf | |
| // Implement each of the methods below so that they all compile | |
| // There only one valid implementation for each function (apart from f5 & f6) | |
| using OneOf; | |
| A F1<A>(A a) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Cat { | |
| speak() { return "meow"; } | |
| } | |
| function process(cat) { | |
| return cat.speak(); | |
| } | |
| class Ball { | |
| } |
NewerOlder