Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Normand ericnormand

View GitHub Profile
@ericnormand
ericnormand / 00 RGB color mixing.md
Created Oct 19, 2020
400 - PurelyFunctional.tv Newsletter
View 00 RGB color mixing.md

RGB Color Mixing

Here's an algorithm for mixing multiple RGB colors to create a single color.

  1. Separate the colors into Red, Green, and Blue components.
  2. Average all the Reds together, average all the Greens together, average all the Blues together.
  3. Put the average values back together into a resulting RGB.

Is this the right way to do it? I don't know! But it's the way we're going to implement in this problem. Your task is to take a collection of RGB strings of the form "#FF021A", mix them like the algorithm above, and return the resulting RGB string.

@ericnormand
ericnormand / 00 Digit search.md
Last active Oct 20, 2020
399 - PurelyFunctional.tv Newsletter
View 00 Digit search.md

Digit search

This is one of those weird programming puzzles with no real point except practice. But it's considered Very Hard in JavaScript. Let's see how we do in Clojure.

Write a function that takes a sequence of integers. You're trying to get all 10 digits by looking through the numbers sequentially. When you have found one instance of every decimal digit, return whatever number you were on when you found the last one. If you get to the end of the sequence without finding all the digits (for instance, maybe there was no 9), then just return nil.

Example

(digit-search   [5175 4538 2926 5057 6401 4376 2280 6137]) ;=> 5057
@ericnormand
ericnormand / 00 guitar frets.md
Created Oct 5, 2020
398 - PurelyFunctional.tv Newsletter
View 00 guitar frets.md

The well-tempered ergodox

This week, we're going to learn some guitar. Write a function that takes the string number and fret number and returns the note you get when plucking it. You can use the chart linked to at the bottom of this page as a guide.

;; third string, second fret
(note 3 2) => :A
;; first string, sixth fret
(note 1 6) => :A#
@ericnormand
ericnormand / 00 Wolf Sheep Cabbage.md
Created Sep 25, 2020
397 - PurelyFunctional.tv Newsletter
View 00 Wolf Sheep Cabbage.md

Wolf, sheep, cabbage

There's a really common problem called "Wolf, sheep, cabbage". (There are other names for it as well). The problem goes like this:

You own a wolf, a sheep, and a cabbage. You need to cross a river and you only have a boat big enough for you plus one other passenger (your wolf, sheep, or cabbage). The trouble is, the animals are hungry. If you leave the wolf with the sheep, the wolf will eat it. If you leave the sheep with the cabbage, it will eat it. The wolf, being a carnivore, will not eat the cabbage. How do you move them safely to the other side with nothing being eaten?

It's a fun problem and I suggest you give it a try on paper if you don't know the solution. If you can't figure it out, search for it online and you'll find solutions.

Your task is to write a function that generates solutions to this puzzle, in the form of boat crossings. Each crossing should state:

@ericnormand
ericnormand / 00 Set game.md
Last active Sep 26, 2020
396 - PurelyFunctional.tv Newsletter
View 00 Set game.md

Set Game

I used to play a game called Set. In it, you'd place 12 cards down on the table. Then the players would try to find "sets". If you found a set, you'd race to hit the cards with your hand to claim it. It was tons of fun and just mathy enough to appeal to someone like me.

In this task, you are going to take a function that judges whether three cards constitute a valid "set".

Each card has four properties:

  1. Color (red, purple, green)
  2. Number (1, 2, 3)
@ericnormand
ericnormand / 00 Seasons.md
Last active Sep 18, 2020
395 PurelyFunctional.tv Newsletter
View 00 Seasons.md

Seasons

Well, it's Hurricane Season here in the Gulf South. But that's not the kind of seasons I'm talking about now.

Your job is to take a month (keyword) day (number) and a hemisphere (:north or :south) and determine which season it is (return a keyword), according to this handy table.

Start       End         North  South
March 1     May 31      Spring Autumn
June 1      August 31   Summer Winter
View 00 FizzBuzz.md

FizzBuzz, but with no ifs

FizzBuzz is a classic interview exercise meant to filter out people who can't program. It tests that you can write conditionals, loops, and a basic modulo check for divisibility. While it seems kind of insulting to ask someone to solve such a trivial problem, imagine if the interviewee couldn't do it. It's a quick way to check for basic skill before wasting an hour in the interview.

This week, you're going to write FizzBuzz, but just to show off to the interviewer, you're not going to write any conditionals or loops. Just to be clear, that means no if, when, cond, or case expressions.

Here are the rules:

Write a function that is passed a sequence of integers.

View 00 Better filename sort.md

Better filename sort

Typically, strings are sorted by their unicode value. That means you get some weird behavior. We see this with naive sorts of filenames.

  • "Zoo.txt" is sorted before "academy.zip"
  • "12 Final chapter.txt" is sorted before "2 Second chapter.txt"

We should be able to find something a bit smarter. Let's try two rules:

  • Case insensitivity — a and A should be equivalent
View 00 binary search.md

Binary search

If I give you a sorted vector of integers, you can search through it quickly using binary search to know if it contains a given number n. Is n right in the middle? If yes, you're done. If not, then you either have to search the left half or the right half. Since the numbers are sorted, you can check if n is greater than or less than the middle number. You can then recurse down into the appropriate half. Your task is to write this function.

(binary-search 3 [1 2 3]) ;=> true
(binary-search 4 [1 2 5]) ;=> false
(binary-search 10 [1 2 4 5 9 10 11 12]) ;=> true
View 00 powers in range.md

Powers in range

Let's say you have an inclusive range of integers [a, b]. You also have an exponent n. What numbers k^n (integer k raised to the nth power) occur in that range? Write a function that takes a, b, and n and returns the numbers k^n.

Examples

;; n = 2, [a, b] = [49, 65]
(powers-in-range 2 49 65) ;=> [49 64] ;; that is, 7^2 and 8^2
;; n = 3, [a, b] = [1, 27]
You can’t perform that action at this time.