Skip to content

Commit

Permalink
completed #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wilson committed Feb 4, 2011
1 parent e7d5bdc commit 4a7efee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
37 changes: 17 additions & 20 deletions 017.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ teenWords = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixt

tensConnect = ("", "-")
hundredsConnect = (" hundred", " and ")
thousandsConnect = (" thousand", " , ")
thousandsConnect = (" thousand", ", ")

digitsToWords :: (Int, Int, Int) -> (String, String, String)
digitsToWords (h, t, o) = (onesWords !! h, tensWords !! t, onesWords !! o)
-- Works up to 999,999. Wouldn't take much to extend it much beyond that.
sayIt :: Int -> String
sayIt n = connect first second thousandsConnect
where
first = list !! 0
second = list !! 1
list = thousandsList n

thousandsList :: Int -> [String]
thousandsList = map threeDigitsToWords . commas
where
commas n = quot n 1000 : [mod n 1000]
threeDigitsToWords = tripleToWords . makeTriple
makeTriple n = (quot n 100, (mod (quot n 10) 10, mod n 10))

doubleToWords :: (Int, Int) -> String
doubleToWords (t, o)
Expand All @@ -24,25 +36,10 @@ connect "" second connect = second
connect first "" connect = first ++ fst connect
connect first second connect = first ++ fst connect ++ snd connect ++ second

makeTriple :: Int -> (Int, (Int, Int))
makeTriple n = (quot n 100, (mod (quot n 10) 10, mod n 10))

threeDigitsToWords :: Int -> String
threeDigitsToWords = tripleToWords . makeTriple

commas :: Int -> [Int]
commas 0 = []
commas n = mod n 1000 : (commas $ quot n 1000)

sayIt :: Int -> String
sayIt n = "num-ber ,"

countTo :: Int -> [String]
countTo n = map sayIt [1..n]

totalLetters :: [String] -> Int
totalLetters words = sum $ map lettersInString words
where lettersInString = length . filter isLetter

euler17 :: Int -> Int
euler17 = totalLetters . countTo
euler17 = totalLetters . countTo
where countTo n = map sayIt [1..n]
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Here's the current status: (No status listed if no code is checked in.)
#14: Some exploratory work
#15: SOLVED
#16: SOLVED
#17: Nearly complete
#17: SOLVED
#19: SOLVED
#20: SOLVED

0 comments on commit 4a7efee

Please sign in to comment.