Skip to content

Instantly share code, notes, and snippets.

View ericnormand's full-sized avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 Unfriendly neighbors.md
Created May 3, 2022 18:08
467 Eric Normand Newsletter

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 21:18
466 Eric Normand Newsletter

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 15:26
465 Eric Normand Newsletter

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 March 2, 2022 22:22
463 PurelyFunctional.tv Newsletter

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 February 21, 2022 15:14
462 PurelyFunctional.tv 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.

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 February 5, 2022 21:51
461 PurelyFunctional.tv Newsletter

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 20:55
460 PurelyFunctional.tv Newsletter

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!"
@ericnormand
ericnormand / 00 Paul cipher.md
Created January 17, 2022 23:59
459 PurelyFunctional.tv Newsletter

Paul Cipher

Here's an interesting cipher.

  • Treat all letters as uppercase, and convert them to uppercase if needed.
  • The first alphabetical character of the string will not change.
  • All subsequent alphabetical characters are shifted toward Z by the alphabetical position of the preceding alphabetical character.
  • Non-alphabetical characters are left as-is.

Your task is to write an encoder and decoder for this cipher

@ericnormand
ericnormand / 00 String difference.md
Created January 10, 2022 14:03
458 PurelyFunctional.tv Newsletter

String difference

Imagine two strings, A and B. B is generated by shuffling the letters in A and adding a new random letter to it at a random position. Write a function that takes A and B and returns the new letter.

Examples

(diff "" "j") ;=> \j
(diff "a" "ab") ;=> \b
(diff "abc" "xcab") ;=> \x
@ericnormand
ericnormand / 00 Chess moves.md
Created January 3, 2022 22:34
457 PurelyFunctional.tv Newsletter

Chess moves

Write a function that determines if a chess piece, on an empty board, can move from one space to another in one move.

Examples

(can-move? :pawn "A2" "A3") ;=> true
(can-move? :queen "H1" "A8") ;=> true
(can-move? :knight "A4" "A5") ;=> false ;; (that's not how knights move)