Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 Reverse words.md
Last active Jun 7, 2022
470 Eric Normand Newsletter
View 00 Reverse words.md

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.

Examples

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

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) ;=> (+ (fib (dec n)) (fib (dec (dec n))))
@ericnormand
ericnormand / 00 Maxie and Minnie.md
Created May 10, 2022
468 Eric Normand Newsletter
View 00 Maxie and Minnie.md

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.

Examples

(swapmaxmin 213) ;=> [312, 123]
View 00 Unfriendly neighbors.md

Unfriendly neighors

Let's say we have a sequence of integers:

[1 3 2 4 1]

There are 4 spots between numbers where we could insert a new number (represented by commas):

@ericnormand
ericnormand / 00 custom sort order.md
Last active May 26, 2022
466 Eric Normand Newsletter
View 00 custom sort order.md

Custom sort order

The sort function sorts a collection based on its "natural ordering." sort-by allows you to sort a collection based on the natural ordering of the result of some function applied to the elements. Your task is to write a function sort-with which takes a sequence of elements that define the sort order.

Example

           ; define ordering               ; collection to sort
(sort-with [:breakfast :lunch :dinner]     #{:lunch :breakfast :dinner}) ;=> (:breakfast :lunch :dinner)
(sort-with [2 3 4 :jack :queen :king :ace] [4 2 4 :king 2]) ;=> [2 2 4 4 :king]
@ericnormand
ericnormand / 00 Single letter swaps.md
Last active May 27, 2022
465 Eric Normand Newsletter
View 00 Single letter swaps.md

Single letter swaps

Write a function that takes a sequence of strings and a target string. For each string in the sequence, determine if it is equal to the target string after exactly one letter swap. Return the sequence of letter pairs that are swapped, or nil if it doesn't exist.

Example

(letter-swaps ["bacd" "abdc" "abcde" "abcc" "abcd"] "abcd")
  ;=> [#{\a \b} #{\c \d} nil nil nil]
@ericnormand
ericnormand / 00 Word search.md
Created Mar 2, 2022
463 PurelyFunctional.tv Newsletter
View 00 Word search.md

Word search

I'm fond of search that is forgiving of small mistakes. If you type a few letters from the word, even if you miss a letter, you should get the word.

Here's a simple, forgiving search function.

Write a function that takes two strings, a needle and a haystack. The function should return true if the needle is found in the haystack, with a few forgiving features:

  • If the needle is fully contained in the haystack, it should return true.
  • If the needle would be fully contained in the haystack, but for one or more missing letters, it should return true.
@ericnormand
ericnormand / Odd One Out.md
Created Feb 21, 2022
462 PurelyFunctional.tv Newsletter
View Odd One Out.md

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.

Examples

(odd-one? ["a" "b" "c"]) ;=> false
(odd-one? ["a" "b" "cc"]) ;=> true
(odd-one? ["abc" "aa" "xyz" "jj"]) ;=> false
@ericnormand
ericnormand / 00 Primes in a number.md
Created Feb 5, 2022
461 PurelyFunctional.tv Newsletter
View 00 Primes in a number.md

Primes in a number

Another contrived math problem. This one I think is actually pretty hard. It's got detecting primes, string manipulation, and combinations.

Your task is to write a function that takes an integer and finds all primes that are substrings of the decimal digits of that integer.

Examples

(find-primes 2) ;=> [2]
@ericnormand
ericnormand / 00 Rearrange sentence.md
Last active May 27, 2022
460 PurelyFunctional.tv Newsletter
View 00 Rearrange sentence.md

NOTE: If you're looking for issue 461 (Primes in a number), please go here: https://gist.github.com/ericnormand/d52d1fe0e80f06e4b517d45a082765a0

Rearrange sentence

Here's a neat, yet contrived, text processing problem. The words in your sentence have been mixed up. Luckily, there's a number embedded in each word that says its position in the sentence. Write a function that puts the words in the right order and removes the position digits.

Examples

(rearrange "World2! He1llo,") ;=> "Hello, World!"