Skip to content

Instantly share code, notes, and snippets.


Eric Normand ericnormand

View GitHub Profile
ericnormand / 00 Knight
Created Apr 9, 2021
422 Newsletter
View 00 Knight

Knight move

Write a function that takes a chess position as a tuple (such as [:A 4]). If there were a knight there, where would it be able to move? Have the function return a collection of all possible moves for that knight. Assume there are no other pieces on the board. Be sure the knight doesn't leave the board.

Knight Move diagram


(knight-move [:D 3]) ;=> [[:B 2] [:C 1] [:B 4] [:C 5] [:E 5] [:F 4] [:E 1] [:F 2]]
ericnormand / 00 Element
Created Apr 5, 2021
421 Newsletter
View 00 Element

Element promotion

Sometimes you have a list and you want to "promote" one element toward the front of the list. Essentially, you need to swap the element with its immediate predecessor (if it exists). Write a function promote that takes a predicate and a list. If the predicate is true, the element should be promoted.


(promote even? [1 3 5 6]) ;=> (1 3 6 5)
(promote even? []) ;=> ()
(promote even? [2 1]) ;=> (2 1)
ericnormand / 00 Unique
Created Mar 30, 2021
420 Newsletter
View 00 Unique

Unique elements

There's a function in Clojure called distinct that removes duplicates from a sequence. Your task is to write a function called uniques that removes elements that appear twice.


(uniques []) ;=> ()
(uniques [1 2 3]) ;=> (1 2 3)
(uniques [1 1 2 3]) ;=> (2 3)
ericnormand / 00 String
Created Mar 22, 2021
419 Newsletter
View 00 String

String difference

You are given two strings, a and b. We consider each letter to be unique, meaning duplicates are significant. Write a function that returns the count of letters in b which do not occur in a.


(strdiff "abc" "") ;=> {} ;; no characters in b don't occur in a
(strdiff "abc" "abc") ;=> {} ;; ditto
(strdiff "" "abc") ;=> {\a 1 \b 1 \c 1}
View 00 Index

Index map

A vector can be seen as a mapping of index to value. However, we want a mapping from value to indexes. Write a function that takes a sequence and returns a map where the elements of the sequence are the keys and the values are sets of the indexes where the value is found.


(index-map []) ;=> {}
(index-map [1 2 3]) ;=> {1 #{0} 2 #{1} 3 #{2}}
(index-map [1 1 1]) ;=> {1 #{0 1 2}}
ericnormand / 00 Character
Created Mar 8, 2021
417 Newsletter
View 00 Character

Character swap

Write a function that swaps every occurrence of one string with another, and vice versa. It is like clojure.string.replace but works both ways.


(swap "abc" "a" "b") ;=> "bac"
(swap "book" "k" "t") ;=> "boot"
(swap "Closure" "j" "s") ;=> "Clojure"
ericnormand / 00 Index
Created Mar 1, 2021
416 Newsletter
View 00 Index

Index filter

Clojure already has a function called filter. You pass it a predicate and it calls the predicate on each element. But what it doesn't have is a function called filter-index that calls a predicate on the indexes of the sequence to decide what to keep.


(filter-index even? "abcdefg") ;=> (\a \c \e \g) (keep even-indexed elements)
(filter-index prime? "abcdefghijk") ;=> (\c \d \f \h) (keep prime-number indexes)
ericnormand / 00 Pattern
Created Feb 18, 2021
415 Newsletter
View 00 Pattern

Pattern matching

When describing rhyming patterns, poets use letters to define patterns, like this:

Roses are red A
Violets are blue B
Sugar is sweet C
and so are you B

This poem has an ABCB rhyming structure because the B lines rhyme and A and C do not.

ericnormand / 00 Decimal to
Created Feb 13, 2021
414 Newsletter
View 00 Decimal to

Decimal to binary

Write a function that takes a string and replaces all occurrences of numbers with their decimal form.


(d->b "I have 2 arms.") ;=> "I have 10 arms."
(d->b "That costs 3 dollars. But I only have 1 dollar.") ;=> "That costs 11 dollars. But I only have 1 dollar."
(d->b "I was born in 1981.") ;=> "I was born in 11110111101."
ericnormand / 00 Minimum steps to
Created Feb 9, 2021
413 - Newsletter
View 00 Minimum steps to

Minimum steps to palindrome

A string is a palindrome if it is equal to its reverse.

(= "racecar" (str/reverse "racecar")) ;=> true, so palindrome

Your task is to write a function that adds a minimum of letters to the end of a string to make it a palindrome.