Skip to content

Instantly share code, notes, and snippets.

View ericnormand's full-sized avatar

Eric Normand ericnormand

View GitHub Profile
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 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 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 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 Simplifying
Created August 8, 2022 03:00
473 Eric Normand Newsletter

Simplifying fractions

A harder one for this week.

Fractions are often represented in simplified form, where the numerator and denominator share only the factor 1. Write a function simplify that takes two integers (representing the numerator and denominator) and simplifies the fraction they represent, returning the two numbers.


;; the fraction 10/10
ericnormand / 00
Created August 1, 2022 22:00
472 Eric Normand Newsletter


A futuristic robot is programmed to take in a sequence of numbers. Each number is the distance to travel in a cardinal direction (north, south, east, west). It starts facing north at (0, 0), travels straight ahead by the distance given in the first number, then turns 90 degrees clockwise, now facing east. Then it repeats with the next number. Your job is to calculate where it ends up at the end of the sequence.


ericnormand / 00 License
Last active August 1, 2022 21:50
471 Eric Normand Newsletter

License plates

When you cross the border in a car, you have to abide by the local license plate regulations. (This is not true, but let's play pretend!) The order of the numbers and letters stays the same. But the groupings change from country to country.

Write a function that takes a license plate code (letters, digits, and hyphens in a string) and a group size (integer). The function should return a new string with the characters regrouped with hyphens between groups. All groups should be of the given size, except for perhaps the first, if there aren't enough characters to fill the group.


(regroup "A5-GG-B88" 3) ;=&gt; "A-5GG-B88"
ericnormand / 00 Reverse
Last active June 7, 2022 17:49
470 Eric Normand Newsletter

Reverse words

Write a function that takes a string containing words (one or more sentences) and returns a string containing the words in reverse order.


(reverse-words "my name is Eric.") ;;=> "Eric. is my name"
(reverse-words "hello") ;;=> "hello"
(reverse-words "I love you") ;;=&gt; "you love I"
ericnormand / 00 Lazy Fibonacci
Last active May 24, 2022 05:05
469 Eric Normand Newsletter

Lazy Fibonacci Sequence

Ah, the ubiquitous example of recursive functions. Well, this is not your traditional fibonacci sequence exercise.

We all know that the fibonacci sequence is defined as:

(fib 0) ;=> 1
(fib 1) ;=> 1
(fib n) ;=&gt; (+ (fib (dec n)) (fib (dec (dec n))))
ericnormand / 00 Maxie and
Created May 10, 2022 11:00
468 Eric Normand Newsletter

Maxie and Minnie

The maxxie of a number n is the largest number you can achieve by swapping two of its digits (in decimal) (or choosing not to swap if it is already the largest possible). The minnie is the smallest with one swap (though you can't swap a zero digit into the most significant position).

Your task is to write a function that takes an integer and returns a tuple of the maxxie and minnie.


(swapmaxmin 213) ;=&gt; [312, 123]