Skip to content

Instantly share code, notes, and snippets.

@flavianmissi
Created March 10, 2011 01:28
Show Gist options
  • Save flavianmissi/863402 to your computer and use it in GitHub Desktop.
Save flavianmissi/863402 to your computer and use it in GitHub Desktop.
Count and Say problem: http://www.rubyquiz.com/quiz138.html
class CountAndSay:
def say(self, word):
numbers = {1:"one", 2:"two", 3:"three"}
say = ""
while len(word) > 0:
it = iter(word)
letter = it.next()
if letter is not " ":
say += numbers[word.count(letter)] + " " + letter + " "
word = word.replace(letter, "")
return say
import unittest
from count_and_say import CountAndSay
from nose.tools import *
class CountAndSayTestCase(unittest.TestCase):
def test_if_look_retrieve_one_k_one_l_two_o(self):
word = CountAndSay()
assert_equals(word.say("look"), "one l two o one k ")
def test_if_look_and_say_does_not_count_blank_spaces(self):
word = CountAndSay()
assert_equals(word.say("look and say"), "one l two o one k two a one n one d one s one y ")
def test_if_an_randomic_phrase_return_the_expected(self):
word = CountAndSay()
assert_equals(word.say("an randomic phrase"), "three a two n two r one d one o one m one i one c one p one h one s one e ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment