ABC problem: Difference between revisions
Content added Content deleted
imported>Louisono m (renaming variables + reformatting for clearer solution) |
|||
Line 5,641: | Line 5,641: | ||
CONFUSE | true</pre> |
CONFUSE | true</pre> |
||
=={{header|Koka}}== |
|||
{{trans|Python}}with some Koka specific updates |
|||
<syntaxhighlight lang="koka"> |
|||
val blocks = [("B", "O"), |
|||
("X", "K"), |
|||
("D", "Q"), |
|||
("C", "P"), |
|||
("N", "A"), |
|||
("G", "T"), |
|||
("R", "E"), |
|||
("T", "G"), |
|||
("Q", "D"), |
|||
("F", "S"), |
|||
("J", "W"), |
|||
("H", "U"), |
|||
("V", "I"), |
|||
("A", "N"), |
|||
("O", "B"), |
|||
("E", "R"), |
|||
("F", "S"), |
|||
("L", "Y"), |
|||
("P", "C"), |
|||
("Z", "M")] |
|||
pub fun get-remove( xs : list<a>, pred : a -> bool, acc: ctx<list<a>>) : (maybe<a>, list<a>) |
|||
match xs |
|||
Cons(x,xx) -> if !pred(x) then xx.get-remove(pred, acc ++ ctx Cons(x, _)) else (Just(x), acc ++. xx) |
|||
Nil -> (Nothing, acc ++. Nil) |
|||
fun check-word(word: string, blocks: list<(string, string)>) |
|||
match word.head |
|||
"" -> True |
|||
x -> |
|||
val (a, l) = blocks.get-remove(fn(a) a.fst == x || a.snd == x, ctx _) |
|||
match a |
|||
Nothing -> False |
|||
Just(_) -> check-word(word.tail, l) |
|||
fun can-make-word(word, blocks: list<(string, string)>) |
|||
check-word(word.to-upper, blocks) |
|||
fun main() |
|||
val words = ["", "a", "baRk", "booK", "treat", "COMMON", "squad", "Confused"] |
|||
words.map(fn(a) (a, can-make-word(a, blocks))).foreach fn((w, b)) |
|||
println(w.show ++ " " ++ (if b then "can" else "cannot") ++ " be made") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>"": true |
|||
"" can be made |
|||
"a" can be made |
|||
"baRk" can be made |
|||
"booK" cannot be made |
|||
"treat" can be made |
|||
"COMMON" cannot be made |
|||
"squad" can be made |
|||
"Confused" can be made |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |