summary refs log tree commit diff stats
path: root/raku/luhn
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-03 18:19:01 +0530
committerAndinus <andinus@nand.sh>2021-09-03 18:19:01 +0530
commit3e70da35ee173955ed9990e5dfd214eb8f693e61 (patch)
tree481800609b288577344008882488e1be27bc6a67 /raku/luhn
parent9cec9e1815f3da2da02173b5302b372a761df00f (diff)
downloadexercism-3e70da35ee173955ed9990e5dfd214eb8f693e61.tar.gz
Raku: Luhn: Add solution
Diffstat (limited to 'raku/luhn')
-rw-r--r--raku/luhn/Luhn.rakumod9
1 files changed, 8 insertions, 1 deletions
diff --git a/raku/luhn/Luhn.rakumod b/raku/luhn/Luhn.rakumod
index 4b704b5..8efafc0 100644
--- a/raku/luhn/Luhn.rakumod
+++ b/raku/luhn/Luhn.rakumod
@@ -1,4 +1,11 @@
 unit module Luhn;
 
-sub is-luhn-valid(Str $input is copy --> Bool) is export {
+sub is-luhn-valid($input is copy --> Bool) is export {
+    return False if $input.comb.grep(/\d|\s/).join ne $input;
+    $input = $input.comb.grep(/\d/)>>.Int;
+    return False if $input.elems < 2;
+
+    ([+] gather for $input.reverse.kv -> $k, $v {
+         take $k !%% 2 ?? ($_ > 9 ?? $_ - 9 !! $_) !! $v with $v * 2;
+     }) %% 10;
 }