1
1
Fork 0
exercism/jq/luhn/luhn.jq
Christina Sørensen 7c39127802
feat(jq): luhn attempt 2
Signed-off-by: Christina Sørensen <christina@cafkafk.com>
2024-12-15 08:38:23 +01:00

26 lines
488 B
Text

def luhn:
if ("\(.)" / "" | map(select(test("\\d"))))|length <=1 then false
elif .|test("^(\\d|\\s)+$")|not then false
else
(
"\(.)" / ""
| map(
select(
test("\\d")
)
| tonumber
)
) as $s
| [range(0; $s|length)]
| map(
if . % 2 == 0 then (($s[.]) * 2 |(
if . > 9 then . - 9
end
))
else $s[.]
end
)
| add
| . % 10 == 0
end;
.|luhn