Skip to content

Instantly share code, notes, and snippets.

View ericnormand's full-sized avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 Rhyming scheme equivalence.md
Created December 27, 2021 16:09
456 PurelyFunctional.tv Newsletter

Rhyming scheme equivalence

Roses are red    A
Violets are blue B
Sugar is sweet   C
And so are you   B

English teachers like to represent the rhyming pattern of poems with letters. Each line rhymes with lines with the same letter. In this case, B lines rhyme together, but the A and C lines don't rhyme. We will call that a "ABCB" rhyming scheme. However, the choice of letters is arbitrary. We could have called it "IBPB" or "XALA".

@ericnormand
ericnormand / 00 Autocomplete.md
Last active June 4, 2022 18:54
455 PurelyFunctional.tv Newsletter

Autocomplete

I always liked flexible autocomplete where you can type a few letters, even skipping some letters, and it can find the file or word for you.

Your task is to write a function that determines if you can complete a sequence of characters to a given string.

Examples

(completes? "a" "autocomplete") ;=> true
@ericnormand
ericnormand / 00 Backspace jam.md
Created December 6, 2021 15:44
453 PurelyFunctional.tv Newlsetter

Backspace jam

Let's say we have users typing keys on the keyboard. We capture the characters they represent in strings. However, sometimes the user hits the backspace key, which removes the previous character. We will represent a backspace key press with a # character. Write a function that applies the behavior of backspace to the string.

Examples

(apply-bs "abc#") ;=> "ab"
(apply-bs "abc###") ;=> ""
(apply-bs "###abc") ;=> "abc"
@ericnormand
ericnormand / 00 Consecutive numbers.md
Created November 29, 2021 14:53
452 PurelyFunctional.tv Newsletter

Consecutive numbers

Write a function that determines whether a sequence of integers can be rearranged into a sequence of consecutive numbers without duplicates. The function should return the sequence of consecutive numbers or nil if it is not possible.

Examples

(consec []) ;=> () ;; trivially true
(consec [1]) ;=> (1) ;; ditto
(consec [3 1 2]) ;=> (1 2 3)
@ericnormand
ericnormand / 00 Ulam sequence.md
Created November 15, 2021 15:30
451 PurelyFunctional.tv Newsletter

Ulam sequence

The Ulam sequence is an interesting mathematical sequence of integers. It starts with [1 2]. At each step, the next element is:

  • not already in the sequence
  • a sum of two previous elements
  • the number must be produced by only one sum
  • the smallest in case there are multiple candidates

Let's walk through an example.

@ericnormand
ericnormand / 00 Phrase maximization.md
Created November 1, 2021 14:49
449 PurelyFunctional.tv Newsletter

Phrase maximization

Imagine we have the following sentence.

she sells sea shells by the sea shore

We want phrases that are as big as possible, yet no bigger than 10 characters long. A phrase must contain entire words and a space between them. And an instance of a word should only exist in the first phrase that matches.

We start at the beginning. "she sells" is <= 10 characters. We can't add the following "sea" because "she sells sea" is 13 characters. That means "she sells" is a maxmizing phrase. We now start at "sea" and continue.

@ericnormand
ericnormand / 00 Reverse-Polish calculator.md
Created October 27, 2021 15:06
448 PurelyFunctional.tv Newsletter

Reverse-Polish calculator

Reverse-Polish notation is a way to write mathematical expressions where the operator appears after the operands. For example:

1 2 +      => 1 + 2 = 3

In traditional parenthetical notation, that is equivalent to (1 + 2) and in Lisp (+ 1 2). If we assume there are four binary arithmetic operators (+, -, *, and /), we can write arbitrarily complex expressions with no need for parentheses.

@ericnormand
ericnormand / 00 Product of digits of sum.md
Created October 19, 2021 14:24
447 PurelyFunctional.tv Newsletter

Product of digits of sum

Write a function that takes one or more numbers as arguments. It should sum them, then multiply the digits. If the answer is one digit long, it's done. If it's more than one digit, repeat and multiply the digits again.

Examples

(sum-prod 4) ;=> 4
(sum-prod 10) ;=> 0 (since the sum is two digits, then 1 * 0)
(sum-prod 11 12) ;=&gt; 6
@ericnormand
ericnormand / 00 Longest Alternating Substring.md
Last active April 10, 2022 09:40
446 PurelyFunctional.tv Newsletter

Longest Alternating Substring

Write a function that takes a string of digits and returns the longest substring that contains only alternating odd/even digits. If two substrings have the same length, return the one that appears first.

Examples

(longest-alt-subs "") ;=> ""
(longest-alt-subs "1") ;=> "1"
(longest-alt-subs "123") ;=&gt; "123"
@ericnormand
ericnormand / 00 Dueling Knights.md
Last active October 6, 2021 16:20
445 PurelyFunctional.tv Newsletter

Dueling Knights

A chess board has nothing but knights ♞ and empty spaces. In this setup, the color of the knights doesn't matter. Each knight could potentially capture any other. Your job is to write a function to figure out which knights can capture each other, if any.

A board is represented as a vector of vectors, each 8 elements long. A 0 represents an empty square. A 1 represents a knight. Your function should return a collection of pairs. The pairs represent the positions of the two knights that can capture each other.

For reference, see this site for understanding how knights move and capture.

Examples