summary refs log tree commit diff stats
path: root/raku/luhn
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-08-11 15:26:15 +0530
committerAndinus <andinus@nand.sh>2021-08-11 15:26:15 +0530
commit321825828ac918bad28d0597a8616c6dc9802c3c (patch)
tree0b8e9cb1012197750eb58e972736319b2a6abac2 /raku/luhn
parent2979ef790ac5b8f58495e0dd08cafd6a3a2e30a5 (diff)
downloadexercism-321825828ac918bad28d0597a8616c6dc9802c3c.tar.gz
Add solved exercises
Diffstat (limited to 'raku/luhn')
-rw-r--r--raku/luhn/Luhn.rakumod4
-rw-r--r--raku/luhn/README.md88
-rw-r--r--raku/luhn/luhn.rakutest165
3 files changed, 257 insertions, 0 deletions
diff --git a/raku/luhn/Luhn.rakumod b/raku/luhn/Luhn.rakumod
new file mode 100644
index 0000000..4b704b5
--- /dev/null
+++ b/raku/luhn/Luhn.rakumod
@@ -0,0 +1,4 @@
+unit module Luhn;
+
+sub is-luhn-valid(Str $input is copy --> Bool) is export {
+}
diff --git a/raku/luhn/README.md b/raku/luhn/README.md
new file mode 100644
index 0000000..fe6eb3a
--- /dev/null
+++ b/raku/luhn/README.md
@@ -0,0 +1,88 @@
+# Luhn
+
+Given a number determine whether or not it is valid per the Luhn formula.
+
+The [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) is
+a simple checksum formula used to validate a variety of identification
+numbers, such as credit card numbers and Canadian Social Insurance
+Numbers.
+
+The task is to check if a given string is valid.
+
+Validating a Number
+------
+
+Strings of length 1 or less are not valid. Spaces are allowed in the input,
+but they should be stripped before checking. All other non-digit characters
+are disallowed.
+
+## Example 1: valid credit card number
+
+```text
+4539 3195 0343 6467
+```
+
+The first step of the Luhn algorithm is to double every second digit,
+starting from the right. We will be doubling
+
+```text
+4_3_ 3_9_ 0_4_ 6_6_
+```
+
+If doubling the number results in a number greater than 9 then subtract 9
+from the product. The results of our doubling:
+
+```text
+8569 6195 0383 3437
+```
+
+Then sum all of the digits:
+
+```text
+8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
+```
+
+If the sum is evenly divisible by 10, then the number is valid. This number is valid!
+
+## Example 2: invalid credit card number
+
+```text
+8273 1232 7352 0569
+```
+
+Double the second digits, starting from the right
+
+```text
+7253 2262 5312 0539
+```
+
+Sum the digits
+
+```text
+7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
+```
+
+57 is not evenly divisible by 10, so this number is not valid.
+
+## Resources
+
+Remember to check out the Raku [documentation](https://docs.raku.org/) and
+[resources](https://raku.org/resources/) pages for information, tips, and
+examples if you get stuck.
+
+## Running the tests
+
+There is a test suite and module included with the exercise.
+The test suite (a file with the extension `.rakutest`) will attempt to run routines
+from the module (a file with the extension `.rakumod`).
+Add/modify routines in the module so that the tests will pass! You can view the
+test data by executing the command `raku --doc *.rakutest` (\* being the name of the
+test suite), and run the test suite for the exercise by executing the command
+`prove6 .` in the exercise directory.
+
+## Source
+
+The Luhn Algorithm on Wikipedia [http://en.wikipedia.org/wiki/Luhn_algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm)
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/raku/luhn/luhn.rakutest b/raku/luhn/luhn.rakutest
new file mode 100644
index 0000000..4bd0e63
--- /dev/null
+++ b/raku/luhn/luhn.rakutest
@@ -0,0 +1,165 @@
+#!/usr/bin/env raku
+use Test;
+use JSON::Fast;
+use lib $?FILE.IO.dirname;
+use Luhn;
+plan 18;
+
+my @test-cases = from-json($=pod.pop.contents).List;
+for @test-cases -> %case {
+  subtest %case<description>, {
+    plan 2;
+    isa-ok ( my $result := is-luhn-valid %case<input><value> ), Bool;
+    is-deeply $result, %case<expected>, 'Result matches expected';
+  }
+}
+
+=head2 Test Cases
+=begin code
+[
+  {
+    "description": "single digit strings can not be valid",
+    "expected": false,
+    "input": {
+      "value": "1"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "a single zero is invalid",
+    "expected": false,
+    "input": {
+      "value": "0"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "a simple valid SIN that remains valid if reversed",
+    "expected": true,
+    "input": {
+      "value": "059"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "a simple valid SIN that becomes invalid if reversed",
+    "expected": true,
+    "input": {
+      "value": "59"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "a valid Canadian SIN",
+    "expected": true,
+    "input": {
+      "value": "055 444 285"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "invalid Canadian SIN",
+    "expected": false,
+    "input": {
+      "value": "055 444 286"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "invalid credit card",
+    "expected": false,
+    "input": {
+      "value": "8273 1232 7352 0569"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "invalid long number with an even remainder",
+    "expected": false,
+    "input": {
+      "value": "1 2345 6789 1234 5678 9012"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "valid number with an even number of digits",
+    "expected": true,
+    "input": {
+      "value": "095 245 88"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "valid number with an odd number of spaces",
+    "expected": true,
+    "input": {
+      "value": "234 567 891 234"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "valid strings with a non-digit added at the end become invalid",
+    "expected": false,
+    "input": {
+      "value": "059a"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "valid strings with punctuation included become invalid",
+    "expected": false,
+    "input": {
+      "value": "055-444-285"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "valid strings with symbols included become invalid",
+    "expected": false,
+    "input": {
+      "value": "055# 444$ 285"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "single zero with space is invalid",
+    "expected": false,
+    "input": {
+      "value": " 0"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "more than a single zero is valid",
+    "expected": true,
+    "input": {
+      "value": "0000 0"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "input digit 9 is correctly converted to output digit 9",
+    "expected": true,
+    "input": {
+      "value": "091"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "using ascii value for non-doubled non-digit isn't allowed",
+    "expected": false,
+    "input": {
+      "value": "055b 444 285"
+    },
+    "property": "valid"
+  },
+  {
+    "description": "using ascii value for doubled non-digit isn't allowed",
+    "expected": false,
+    "input": {
+      "value": ":9"
+    },
+    "property": "valid"
+  }
+]
+=end code