Write a program that takes a list of lists of alternatives and print each choice combination possible
ie:
enter list: [[1,2,3], [4,5]]
[1,4]
[1,5]
[2,4]
[2,5]
[3,4]
[3,5]
Here's my code in Haskell, the pancreasless doggy:
mapM_ print . foldr (ap . map (:)) [[]] . read =<< getLine
Name:
Anonymous2008-05-28 22:03
english n = if n<0 then "negative " ++ english (-n)
else if a>0 then k 3 a ++ u
else g n
where
(a,b) = divMod n 1000
u = if b>0 then if b<100 then ", and " ++ g b else ", " ++ g b else ""
k i n | a>0 = k (i+3) a ++ if b>0 then ", " ++ g b ++ ' ' : tes i else ""
| otherwise = if b>0 then g b ++ ' ' : tes i else ""
where (a,b) = divMod n 1000
g n | n<20 = ones n
| n<100 = if b0>0 then tens a0 ++ '-' : ones b0 else tens a0
| b1>0 = ones a1 ++ " hundred and " ++ g b1
| otherwise = ones a1 ++ " hundred"
where
(a0,b0) = divMod n 10
(a1,b1) = divMod n 100
Name:
Anonymous2008-05-28 22:03
>>9
It'd be profoundly uninteresting homework. There's no real algorithm there, just typing.
tes 3 = "thousand"
tes 6 = "million"
tes 9 = "billion"
tes 12 = "trillion"
tes 15 = "quadrillion"
tes ... and so on. Maybe a nice little function here instead of quintillion and so on.
List-based
import Char
import Control.Arrow
import List
main = interact (unlines . map translate . lines)
translate ('-': num) = case translateAbs num of abs@"zero" -> abs
abs -> "minus " ++ abs
translate ('+': num) = translateAbs num
translate num = translateAbs num
translateAbs num
| length num > (3 * 22) = error "translate: Number too long"
| otherwise =
foldl1 ((++) . (++ ", ")) . checkZero . reverse . filter (not . null) . zipWith suffix suffixes
. map translateTrio . splitInTrios $ map digitToInt num
where suffix s [] = []
suffix [] htu = htu
suffix s htu = htu ++ " " ++ s
if (argc > 1) {
int i;
for (i = 1; i < argc; ++i) {
if (!well_formed(argv[i])) {
complain(argv[i], "malformed number");
status = EXIT_FAILURE;
continue;
}
say(argv[i]);
}
} else {
char *line;
while ((line = getline(stdin))) {
if (!well_formed(line)) {
complain(line, "malformed number");
status = EXIT_FAILURE;
free(line);
continue;
}
say(line);
free(line);
}
if (!feof(stdin)) {
status = EXIT_FAILURE;
}
}
return status;
}
Name:
Anonymous2008-05-29 16:10
>>25 i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2006
[1]> (format t "~r" (read))
-1
minus one
NIL
[2]>
Bye.
Name:
Anonymous2008-05-29 19:34
minus one is not a number. it's a binary operator and one operand.
Name:
Anonymous2008-05-29 19:52
>>28 *** - EVAL: utter nonsense
The following restarts are available:
RETRY :R1 Retry
ABORT :R2 ABORT
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy