Skip to content

Instantly share code, notes, and snippets.

View ericnormand's full-sized avatar

Eric Normand ericnormand

View GitHub Profile
ericnormand / 00_script.clj
Last active January 6, 2024 07:13
Boilerplate for running Clojure as a shebang script
#_DEPS is same format as deps.edn. Multiline is okay.
{:deps {clj-time {:mvn/version "0.14.2"}}}
#_You can put other options here
ericnormand / 00 Super
Created September 12, 2022 15:11
478 Eric Normand Newsletter

Super Digit

This is kind of a contrived problem, but it's the kind that breeds lots of interesting implementations and tests your understanding of lower-level details. So let's do it!

You're given an integer n and an integer k. There is an integer p that is k instances of the digits of n concatenated together. For example:

ericnormand / 00 Digit
Last active December 21, 2022 00:45
399 - Newsletter

Digit search

This is one of those weird programming puzzles with no real point except practice. But it's considered Very Hard in JavaScript. Let's see how we do in Clojure.

Write a function that takes a sequence of integers. You're trying to get all 10 digits by looking through the numbers sequentially. When you have found one instance of every decimal digit, return whatever number you were on when you found the last one. If you get to the end of the sequence without finding all the digits (for instance, maybe there was no 9), then just return nil.


(digit-search [5175 4538 2926 5057 6401 4376 2280 6137]) ;=> 5057
ericnormand / 00 How many
Last active December 5, 2022 12:05
476 Eric Normand Newsletter

How many digits?

Imagine you took all the integers between n and m (exclusive, n < m) and concatenated them together. How many digits would you have? Write a function that takes two numbers and returns how many digits. Note that the numbers can get very big, so it is not possible to build the string in the general case.


(num-digits 0 1) ;=> 0 (there are no integers between 0 and 1)
(num-digits 0 10) ;=> 9 (1, 2, 3, 4, 5, 6, 7, 8, 9)
(num-digits 9 100) ;=&gt; 180
ericnormand / 00
Last active November 4, 2022 14:36
320 - Newsletter - Puzzle - Remove nth element

remove the nth element from a list

Clojure's two main sequences, lists and vectors, do not easily let you remove an item from the collection when that item is in the middle of the sequence. Sometimes we need to do that. Write a function remove-at that removes the element at position n from a sequence.

(remove-at 3 [1 2 3 4 5 6])
; => (1 2 3 5 6)

Make this robust. You'll have to make some hard design decisions like how to handle the empty sequence, how to handle out-of-bounds n, and more.

ericnormand / 00 Box of
Created September 4, 2022 16:02
477 Eric Normand Newsletter

Box of chocolates

You work at a chocolate shop that makes two sizes of chocolates:

  • Small (2 grams each)
  • Large (5 grams each)

When someone orders a box of chocolates, they order by total mass. It's your job to figure out how to fulfill the order using a combination of small and large chocolates to exactly hit the total mass ordered.

ericnormand / 00 Least common
Created August 22, 2022 01:59
475 Eric Normand Newsletter

Least common multiple

Write a function that finds the least common multiple of a collection of numbers. Remember that the least common multiple is the smallest integer that is evenly divisible by all the numbers in the collection.


(lcm []) ;=> nil (undefined)
(lcm [10]) ;=> 10
(lcm [2 4]) ;=&gt; 4
ericnormand / 00 Vowel
Last active August 16, 2022 10:53
433 Newsletter

Vowel families

Given two words, we can determine if they have the same vowels, ignoring order and repetition. For instance, "Hello" and "Vowel" both have \e and \o, so they have the same vowels. Write a function that groups words into "families" that all have the same vowels. "Tree" and "tent" also belong to the same family because we ignore the repetition of \e.


(vowel-families ["hello" "vowel" "fox" "cot" "hat" "cat"]) ;=> [["hello" "vowel"]
                                                           ;    ["fox" "cot"]
 ; ["hat" "cat"]]
ericnormand / 00 Ulam
Created November 15, 2021 15:30
451 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 / Odd One
Created February 21, 2022 15:14
462 Newsletter

Odd One Out

Write a function that takes a list of words (Strings). The function should return true if exactly 1 word differs in length from the others. It should return false in all other cases.


(odd-one? ["a" "b" "c"]) ;=> false
(odd-one? ["a" "b" "cc"]) ;=> true
(odd-one? ["abc" "aa" "xyz" "jj"]) ;=&gt; false