Skip to content

Instantly share code, notes, and snippets.

@mark
Forked from zobar/look_and_say.clj
Created March 17, 2014 21:02
Show Gist options
  • Save mark/9608297 to your computer and use it in GitHub Desktop.
Save mark/9608297 to your computer and use it in GitHub Desktop.
(ns look-and-say)
(defn- consecutive [string]
(map first (re-seq #"(.)\1*" string)))
(defn- say [chars]
[(count chars) (first chars)])
(defn- get-next [string]
(apply str (mapcat say (consecutive string))))
(defn look-and-say [string]
(iterate get-next string))
module LookAndSay
class << self
def look_and_say(string)
# Implement me!
end
private
def consecutive(string)
string.scan(/((.)\2*)/).map &:first
end
def say(chars)
[chars.length, chars[0]]
end
def get_next(string)
consecutive(string).map(&method(:say)).join ''
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment