Instantly share code, notes, and snippets.

# Eric Normand ericnormand

Last active May 18, 2024 08:30
Boilerplate for running Clojure as a shebang script
This file contains 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
 #!/bin/sh #_( #_DEPS is same format as deps.edn. Multiline is okay. DEPS=' {:deps {clj-time {:mvn/version "0.14.2"}}} ' #_You can put other options here OPTS='
Created September 12, 2022 15:11

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:

Last active December 21, 2022 00:45

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`.

Example

`(digit-search [5175 4538 2926 5057 6401 4376 2280 6137]) ;=&gt; 5057`
Last active December 5, 2022 12:05

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.

Examples:

```(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```
Last active November 4, 2022 14:36
320 - PurelyFunctional.tv 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.

Created September 4, 2022 16:02

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.

Created August 22, 2022 01:59

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.

Examples:

```(lcm []) ;=> nil (undefined)
(lcm [10]) ;=> 10
(lcm [2 4]) ;=&gt; 4```
Last active August 16, 2022 10:53

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`.

Examples

```(vowel-families ["hello" "vowel" "fox" "cot" "hat" "cat"]) ;=> [["hello" "vowel"]
;    ["fox" "cot"]
; ["hat" "cat"]]```
Created November 15, 2021 15:30

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.

Created February 21, 2022 15:14
```(odd-one? ["a" "b" "c"]) ;=> false