Skip to content

Instantly share code, notes, and snippets.

Eric Normand ericnormand

Block or report user

Report or block ericnormand

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 00 gapful numbers.md

Gapful numbers

Some numbers are gapful, some aren't. 100 is gapful because it has at least three digits and 100 is divisible by 10 ((str 1 0), the concatenation of the first and last digit). That's the definition of gapful: it has at least three digits and is divisible by the number formed by concatenating the first and last digits.

Create a function that takes a number and finds the closest gapful number. The function should return its argument if the argument itself is gapful. And if there are two gapful numbers equidistant to the argument, return the lower one.

Thanks to this site for the challenge idea!

View 00 wheres waldo.md

Where's Waldo?

You are given a vector of vectors, representing a grid. The grid is filled with values. Your job is to find a given value and return the path into the grid that will get to it.

Here's an example:

(wheres-waldo :W ;; look for :W
 [[:A :B :C]
  [:D :E :F]
View 00 duplicate letters.md

words with duplicate letters

Some words have duplicate letters ("book"—two o's—or "that"—two t's). Many don't.

Your task is to write a predicate that returns true if the string contains any words with duplicate letters.

Examples:

(duplicate-letter? "Hello, my friend!") => true (because "hello" has two l's)

View 00 unmix string.md

unmix string

Oh no! My strings have been mixed up. Each pair of letters has been swapped.

Here are examples: "eHllo" should be "Hello". "lCjorue" should be "Clojure".

Write a function to unmix these strings. If the length of the string is odd, the last character is not altered.

(unmix "aHpp yeN weYra !eS eoy uni2 20 0):")
View 00 Dependency ordering.md

task dependencies

Let's say you're writing a project management app. A user will have a bunch of tasks to do and each task might depend on other tasks. Your challenge is to write a function that, given tasks and dependencies, gives at least one order for the tasks such that no task is done before its dependencies are done.

(def tasks [:clean-breakfast :shoes :socks :cook-breakfast :eat-breakfast])

(def dependencies [[:socks :shoes] ;; socks come before shoes; shoes depend on socks
                   [:cook-breakfast :eat-breakfast] ;; cooking comes before eating
                   [:eat-breakfast :clean-breakfast]])
View 00 Prime factorization.md

Prime factorization

Every natural number can be expressed as a product of primes. For instance, 10 = 2 x 5 and 24 = 2 x 2 x 2 x 3.

Your task is to write a function that takes a number n as an argument and returns a list of its prime factors.

View 00 A-Star Algorithm.md

A-Star Algorithm

A neat algorithm for finding a short path through a graph is A-Star. It's used in a lot of games for the characters to find paths through a map.

One thing that's fun about this algorithm is that it's traditionally formulated in terms of mutable data structures. What would this look like as a functional implementation?

Your task is to implement A-Star in Clojure. You can use mutable state if you want! The Wikipedia page has a good description of the algorithm.

View 00 Description.md

Levenshtein distance

The Levenshtein distance measures the edit distance between two strings. That is, how many one-character changes do you need to make between two strings to turn one into the other. The algorithm has a nice recursive definition on Wikipedia, which makes it easy to write in Clojure.

Your goal is to implement the Levenshtein distance as a function of two strings. It should return the edit distance.

Bonus: use memoization to make it more efficient, or use an iterative method.

View 00 Description.md

Write a program that outputs all possibilities to put a + or - or nothing between the numbers 1-9 (in order) such that the result is 100. For example:

1 + 2 + 34 - 5 + 67 - 8 + 9 = 100

The function should output a list of strings with these formulas.

Hint: this is a recursive problem. Use divide and conquer.

View 00 monoid.md

monoid pattern

It strikes me that you can generate functions that implement the monoid pattern knowing only two things: the identity value and the 2-arg case. The other two cases (n-arg and 1-arg) can be calculated for you.

Your task is to write a function that takes the identity and the 2-arg case and returns a new function that implements the monoid pattern.

(defn monoid [id fn-2]
You can’t perform that action at this time.