Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 Knight move.md
Created Apr 9, 2021
422 PurelyFunctional.tv Newsletter
View 00 Knight move.md

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

Examples

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

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.

Examples

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

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.

Examples

(uniques []) ;=> ()
(uniques [1 2 3]) ;=> (1 2 3)
(uniques [1 1 2 3]) ;=> (2 3)
@ericnormand
ericnormand / 00 String difference.md
Created Mar 22, 2021
419 PurleyFunctional.tv Newsletter
View 00 String difference.md

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.

Examples

(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 map.md

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.

Examples

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

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.

Examples

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

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.

Examples

(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
ericnormand / 00 Pattern matching.md
Created Feb 18, 2021
415 PurelyFunctional.tv Newsletter
View 00 Pattern matching.md

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
ericnormand / 00 Decimal to binary.md
Created Feb 13, 2021
414 PurelyFunctional.tv Newsletter
View 00 Decimal to binary.md

Decimal to binary

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

Examples

(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
ericnormand / 00 Minimum steps to palindrome.md
Created Feb 9, 2021
413 - PurelyFunctional.tv Newsletter
View 00 Minimum steps to palindrome.md

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.