Skip to main content
add ungolfed version
Source Link
Zeta
  • 701
  • 4
  • 12

Haskell, 252 231 bytes

let l=words;k=l"six seven eight nine";w=l"one two three four five"++k++l"ten eleven twelve"++((++"teen")<$>l"thir four fif"++k)++[n++"ty"++s|n<-l"twen thir for fif"++k,s<-"":['-':x|x<-take 9w]]in maybe 100id.flip lookup(zip w[1..])

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Ungolfed

-- k in the golfed variant
common = words "six seven eight nine" 

-- w in the golfed variant
numbers = words "one two three four five" ++ common
       ++ words "ten eleven twelve" ++ [p ++ "teen" | p <- words "thir four fif" ++ common]
       ++ [p ++ "ty" ++ s| p <- words "twen thir for fif" ++ common
                         , s <- "" : map ('-':) (take 9 numbers)]

-- part of the expression in the golfed variant
convert :: String -> Int
convert s = maybe 100 id $ lookup s $ zip numbers [1..]

Haskell, 252 231 bytes

let l=words;k=l"six seven eight nine";w=l"one two three four five"++k++l"ten eleven twelve"++((++"teen")<$>l"thir four fif"++k)++[n++"ty"++s|n<-l"twen thir for fif"++k,s<-"":['-':x|x<-take 9w]]in maybe 100id.flip lookup(zip w[1..])

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Haskell, 252 231 bytes

let l=words;k=l"six seven eight nine";w=l"one two three four five"++k++l"ten eleven twelve"++((++"teen")<$>l"thir four fif"++k)++[n++"ty"++s|n<-l"twen thir for fif"++k,s<-"":['-':x|x<-take 9w]]in maybe 100id.flip lookup(zip w[1..])

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Ungolfed

-- k in the golfed variant
common = words "six seven eight nine" 

-- w in the golfed variant
numbers = words "one two three four five" ++ common
       ++ words "ten eleven twelve" ++ [p ++ "teen" | p <- words "thir four fif" ++ common]
       ++ [p ++ "ty" ++ s| p <- words "twen thir for fif" ++ common
                         , s <- "" : map ('-':) (take 9 numbers)]

-- part of the expression in the golfed variant
convert :: String -> Int
convert s = maybe 100 id $ lookup s $ zip numbers [1..]
deleted 4 characters in body
Source Link
Zeta
  • 701
  • 4
  • 12

Haskell, 252252 231 bytes

let l=words;k=" sixl=words;k=l"six seven eight nine ";w=l("onenine";w=l"one two three four five"++k++"tenfive"++k++l"ten eleven twelve")++twelve"++(map(++"teen")$l$"thir<$>l"thir four fif"++k)++(concatMap(\n->((n++"ty")++)<$>"":((:)'++[n++"ty"++s|n<-'<$>take 9w))$l$"twenl"twen thir for fif"++k)in,s<-"":['-':x|x<-take 9w]]in maybe 100id. \k->lookupflip k$ziplookup(zip w[1..])

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Haskell, 252 bytes

let l=words;k=" six seven eight nine ";w=l("one two three four five"++k++"ten eleven twelve")++(map(++"teen")$l$"thir four fif"++k)++(concatMap(\n->((n++"ty")++)<$>"":((:)'-'<$>take 9w))$l$"twen thir for fif"++k)in maybe 100id. \k->lookup k$zip w[1..]

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Haskell, 252 231 bytes

let l=words;k=l"six seven eight nine";w=l"one two three four five"++k++l"ten eleven twelve"++((++"teen")<$>l"thir four fif"++k)++[n++"ty"++s|n<-l"twen thir for fif"++k,s<-"":['-':x|x<-take 9w]]in maybe 100id.flip lookup(zip w[1..])

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.

Source Link
Zeta
  • 701
  • 4
  • 12

Haskell, 252 bytes

let l=words;k=" six seven eight nine ";w=l("one two three four five"++k++"ten eleven twelve")++(map(++"teen")$l$"thir four fif"++k)++(concatMap(\n->((n++"ty")++)<$>"":((:)'-'<$>take 9w))$l$"twen thir for fif"++k)in maybe 100id. \k->lookup k$zip w[1..]

This creates a list of all English number names from "one" to "ninety-nine" and then looks the index of the input up. If it doesn't exist, we're in the edge case "one hundred", so it returns 100, otherwise it's going to return the index.