Skip to content

Instantly share code, notes, and snippets.

@thunklife
Last active October 18, 2019 17:11
Show Gist options
  • Save thunklife/4cbb85d6a011a5a12638deb2875730b3 to your computer and use it in GitHub Desktop.
Save thunklife/4cbb85d6a011a5a12638deb2875730b3 to your computer and use it in GitHub Desktop.
October Katas

October 2019

Hutton's Razor

Hutton's Razor is a minimal interpreted language the consists of two things: literal integers and the ability to add the them together.

Problem

  1. Find a way to express the basic Abstract Syntax Tree of Hutton's Razor. For example, in Haskell we can use a recursive sum type:
data Razor
  = Lit Int
  | Add Razor Razor
  1. Create a function that intrepts the different parts that make up the Razor data type. Again, in Haskell:
interpret (Lit 2) 
-- returns 2

interpret (Add (Lit 1) (Lit 2)) 
-- returns 3
  1. Create another function that will return a string representation of the Razor it is given. More Haskell,
pretty (Lit 2)
-- returns "2"

pretty (Add (Lit 1) (Lit 2))
-- returns "( 1 + 2 )"

Just For Kicks

See if you can write this in an OOP way, interfaces, classes, etc. Once you have both, add the following:

  1. Using the existing AST for a Razor add some new behavior, a new function.
  2. Modify the AST to express multiplication.

How does each paradigm (FP and OOP) effect the adding of new behavior and the adding of a new "type?". This is the basic idea of The Expression Problem.

Pyramid Slide Down

This kata was taken from Code Wars.

Problem

Given a pyramid with this basic shape:

   3
  7 4 
 2 4 6 
8 5 9 3

Determine the "longest slide down" which is defined as

[The] sum of consecutive numbers from the top to the bottom of the pyramid.

So the pyramid above gives us this path as the "longest slide":

   /3/
  \7\ 4 
 2 \4\ 6 
8 5 \9\ 3

3 because, well that's the only choice. The next step in the path is 7 or 4 both are "connected" to 3 and 7 is the largest of the two. Next we have 2, 4 and 6 but 6 is not "connected" to the 7 we selected before so we are left to choose between 2 and 4. The final row follows the same pattern.

So, longestSlideDown([[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]) should return 23.

The challenge is to write a longestSlideDown function; you can assume that the nested list will be sorted...but it might be more fun to not make that assumption and sort the outer list yourself, IDK.

Word Wrap

This kata was taken from Coding Dojo who took it from Robert Martin.

Problem

Write a function, wrap that takes two arguments, a string, and a column number. The function returns the string, but with line breaks inserted in the right places based column number passed to the function. Make sure that no line is longer than the column number and only break lines at word boundaries.

Like a word processor, break the line by replacing the last space in a line with a newline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment