From b8da48633db0694c11649639c1a39b241c498a94 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 17 May 2021 12:35:32 +0200 Subject: README for week 113 --- challenge-113/abigail/README.md | 92 ++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 4a8667ee09..f877fbafe7 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -1,76 +1,56 @@ # Solutions by Abigail -## [Canonical Path](https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK1) +## [Represent Integer](https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK1) -> You are given a string path, starting with a slash `/`. +> You are given a positive integer `$N` and a digit `$D`. > -> Write a script to convert the given absolute path to the simplified -> canonical path. -> -> In a Unix-style file system: -> -> * A period `.` refers to the current directory. -> * A double period `..` refers to the directory up a level. -> * Multiple consecutive slashes (`//`) are treated as a single slash `/`. -> -> The canonical path format: -> -> * The path starts with a single slash `/`. -> * Any two directories are separated by a single slash `/`. -> * The path does not end with a trailing `/`. -> * The path only contains the directories on the path from the root -> directory to the target file or directory +> Write a script to check if `$N` can be represented as a sum +> of positive integers having `$D` at least once. If check passes +> print `1` otherwise `0`. ### Example ~~~~ -Input: "/a/" -Output: "/a" - -Input: "/a/b//c/" -Output: "/a/b/c" +Input: $N = 25, $D = 7 +Output: 0 as there are 2 numbers between 1 and 25 having the digit 7 + i.e. 7 and 17. If we add up both we don't get 25. -Input: "/a/b/c/../.." -Output: "/a" +Input: $N = 24, $D = 7 +Output: 1 ~~~~ ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [C](c/ch-1.c) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) -* [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 112: Canonical Path](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-112-1.html) -## [Climb Stairs](https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK2) +## [Recreate Binary Tree](https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2) -> You are given `$n` steps to climb -> -> Write a script to find out the distinct ways to climb to the top. -> You are allowed to climb either 1 or 2 steps at a time. +> You are given a Binary Tree. +> +> Write a script to replace each node of the tree with the sum of +> all the remaining nodes -### Notes -This is just finding the `$n + 1` Fibonacci number. +### Example +#### Input +~~~~ + 1 + / \ + 2 3 + / / \ + 4 5 6 + \ + 7 +~~~~ +#### Output +~~~~ + 27 + / \ + 26 25 + / / \ + 24 23 22 + \ + 21 +~~~~ ### Solutions -* [AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [Befunge-93](befunge-93/ch-2.bf93) -* [C](c/ch-2.c) -* [Go](go/ch-2.go) -* [Java](java/ch-2.java) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) -* [Perl](perl/ch-2.pl) -* [Pascal](pascal/ch-2.p) -* [Python](python/ch-2.py) -* [R](r/ch-2.r) -* [Ruby](ruby/ch-2.rb) -* [Scheme](scheme/ch-2.scm) ### Blog -[Perl Weekly Challenge 112: Climb Stairs](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-112-2.html) -- cgit From 7b17fd100ab0fa5e4d02b392b3bb1545a514b2d0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 17 May 2021 12:42:44 +0200 Subject: Tests for week 113, part 1 --- challenge-113/abigail/t/ctest.ini | 18 ++++++ challenge-113/abigail/t/input-1-1 | 2 + challenge-113/abigail/t/input-1-10 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-11 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-2 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-3 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-4 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-5 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-6 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-7 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-8 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-1-9 | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/input-2-1 | 1 + challenge-113/abigail/t/output-1-1.exp | 2 + challenge-113/abigail/t/output-1-10.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-11.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-2.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-3.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-4.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-5.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-6.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-7.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-8.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-1-9.exp | 100 ++++++++++++++++++++++++++++++++ challenge-113/abigail/t/output-2-1.exp | 1 + 25 files changed, 2024 insertions(+) create mode 100644 challenge-113/abigail/t/ctest.ini create mode 100644 challenge-113/abigail/t/input-1-1 create mode 100644 challenge-113/abigail/t/input-1-10 create mode 100644 challenge-113/abigail/t/input-1-11 create mode 100644 challenge-113/abigail/t/input-1-2 create mode 100644 challenge-113/abigail/t/input-1-3 create mode 100644 challenge-113/abigail/t/input-1-4 create mode 100644 challenge-113/abigail/t/input-1-5 create mode 100644 challenge-113/abigail/t/input-1-6 create mode 100644 challenge-113/abigail/t/input-1-7 create mode 100644 challenge-113/abigail/t/input-1-8 create mode 100644 challenge-113/abigail/t/input-1-9 create mode 100644 challenge-113/abigail/t/input-2-1 create mode 100644 challenge-113/abigail/t/output-1-1.exp create mode 100644 challenge-113/abigail/t/output-1-10.exp create mode 100644 challenge-113/abigail/t/output-1-11.exp create mode 100644 challenge-113/abigail/t/output-1-2.exp create mode 100644 challenge-113/abigail/t/output-1-3.exp create mode 100644 challenge-113/abigail/t/output-1-4.exp create mode 100644 challenge-113/abigail/t/output-1-5.exp create mode 100644 challenge-113/abigail/t/output-1-6.exp create mode 100644 challenge-113/abigail/t/output-1-7.exp create mode 100644 challenge-113/abigail/t/output-1-8.exp create mode 100644 challenge-113/abigail/t/output-1-9.exp create mode 100644 challenge-113/abigail/t/output-2-1.exp diff --git a/challenge-113/abigail/t/ctest.ini b/challenge-113/abigail/t/ctest.ini new file mode 100644 index 0000000000..fbd971f0e7 --- /dev/null +++ b/challenge-113/abigail/t/ctest.ini @@ -0,0 +1,18 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +1-2 = $D = 2 +1-3 = $D = 3 +1-4 = $D = 4 +1-5 = $D = 5 +1-6 = $D = 6 +1-7 = $D = 7 +1-8 = $D = 8 +1-9 = $D = 9 +1-10 = $D = 0 +1-11 = $D = 1 +2-1 = Given Example diff --git a/challenge-113/abigail/t/input-1-1 b/challenge-113/abigail/t/input-1-1 new file mode 100644 index 0000000000..e225c1a91d --- /dev/null +++ b/challenge-113/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +25 7 +24 7 diff --git a/challenge-113/abigail/t/input-1-10 b/challenge-113/abigail/t/input-1-10 new file mode 100644 index 0000000000..88d1b13563 --- /dev/null +++ b/challenge-113/abigail/t/input-1-10 @@ -0,0 +1,100 @@ +1 0 +2 0 +3 0 +4 0 +5 0 +6 0 +7 0 +8 0 +9 0 +10 0 +11 0 +12 0 +13 0 +14 0 +15 0 +16 0 +17 0 +18 0 +19 0 +20 0 +21 0 +22 0 +23 0 +24 0 +25 0 +26 0 +27 0 +28 0 +29 0 +30 0 +31 0 +32 0 +33 0 +34 0 +35 0 +36 0 +37 0 +38 0 +39 0 +40 0 +41 0 +42 0 +43 0 +44 0 +45 0 +46 0 +47 0 +48 0 +49 0 +50 0 +51 0 +52 0 +53 0 +54 0 +55 0 +56 0 +57 0 +58 0 +59 0 +60 0 +61 0 +62 0 +63 0 +64 0 +65 0 +66 0 +67 0 +68 0 +69 0 +70 0 +71 0 +72 0 +73 0 +74 0 +75 0 +76 0 +77 0 +78 0 +79 0 +80 0 +81 0 +82 0 +83 0 +84 0 +85 0 +86 0 +87 0 +88 0 +89 0 +90 0 +91 0 +92 0 +93 0 +94 0 +95 0 +96 0 +97 0 +98 0 +99 0 +100 0 diff --git a/challenge-113/abigail/t/input-1-11 b/challenge-113/abigail/t/input-1-11 new file mode 100644 index 0000000000..2ed36f80aa --- /dev/null +++ b/challenge-113/abigail/t/input-1-11 @@ -0,0 +1,100 @@ +1 1 +2 1 +3 1 +4 1 +5 1 +6 1 +7 1 +8 1 +9 1 +10 1 +11 1 +12 1 +13 1 +14 1 +15 1 +16 1 +17 1 +18 1 +19 1 +20 1 +21 1 +22 1 +23 1 +24 1 +25 1 +26 1 +27 1 +28 1 +29 1 +30 1 +31 1 +32 1 +33 1 +34 1 +35 1 +36 1 +37 1 +38 1 +39 1 +40 1 +41 1 +42 1 +43 1 +44 1 +45 1 +46 1 +47 1 +48 1 +49 1 +50 1 +51 1 +52 1 +53 1 +54 1 +55 1 +56 1 +57 1 +58 1 +59 1 +60 1 +61 1 +62 1 +63 1 +64 1 +65 1 +66 1 +67 1 +68 1 +69 1 +70 1 +71 1 +72 1 +73 1 +74 1 +75 1 +76 1 +77 1 +78 1 +79 1 +80 1 +81 1 +82 1 +83 1 +84 1 +85 1 +86 1 +87 1 +88 1 +89 1 +90 1 +91 1 +92 1 +93 1 +94 1 +95 1 +96 1 +97 1 +98 1 +99 1 +100 1 diff --git a/challenge-113/abigail/t/input-1-2 b/challenge-113/abigail/t/input-1-2 new file mode 100644 index 0000000000..4bda8f6560 --- /dev/null +++ b/challenge-113/abigail/t/input-1-2 @@ -0,0 +1,100 @@ +1 2 +2 2 +3 2 +4 2 +5 2 +6 2 +7 2 +8 2 +9 2 +10 2 +11 2 +12 2 +13 2 +14 2 +15 2 +16 2 +17 2 +18 2 +19 2 +20 2 +21 2 +22 2 +23 2 +24 2 +25 2 +26 2 +27 2 +28 2 +29 2 +30 2 +31 2 +32 2 +33 2 +34 2 +35 2 +36 2 +37 2 +38 2 +39 2 +40 2 +41 2 +42 2 +43 2 +44 2 +45 2 +46 2 +47 2 +48 2 +49 2 +50 2 +51 2 +52 2 +53 2 +54 2 +55 2 +56 2 +57 2 +58 2 +59 2 +60 2 +61 2 +62 2 +63 2 +64 2 +65 2 +66 2 +67 2 +68 2 +69 2 +70 2 +71 2 +72 2 +73 2 +74 2 +75 2 +76 2 +77 2 +78 2 +79 2 +80 2 +81 2 +82 2 +83 2 +84 2 +85 2 +86 2 +87 2 +88 2 +89 2 +90 2 +91 2 +92 2 +93 2 +94 2 +95 2 +96 2 +97 2 +98 2 +99 2 +100 2 diff --git a/challenge-113/abigail/t/input-1-3 b/challenge-113/abigail/t/input-1-3 new file mode 100644 index 0000000000..4caf9a0413 --- /dev/null +++ b/challenge-113/abigail/t/input-1-3 @@ -0,0 +1,100 @@ +1 3 +2 3 +3 3 +4 3 +5 3 +6 3 +7 3 +8 3 +9 3 +10 3 +11 3 +12 3 +13 3 +14 3 +15 3 +16 3 +17 3 +18 3 +19 3 +20 3 +21 3 +22 3 +23 3 +24 3 +25 3 +26 3 +27 3 +28 3 +29 3 +30 3 +31 3 +32 3 +33 3 +34 3 +35 3 +36 3 +37 3 +38 3 +39 3 +40 3 +41 3 +42 3 +43 3 +44 3 +45 3 +46 3 +47 3 +48 3 +49 3 +50 3 +51 3 +52 3 +53 3 +54 3 +55 3 +56 3 +57 3 +58 3 +59 3 +60 3 +61 3 +62 3 +63 3 +64 3 +65 3 +66 3 +67 3 +68 3 +69 3 +70 3 +71 3 +72 3 +73 3 +74 3 +75 3 +76 3 +77 3 +78 3 +79 3 +80 3 +81 3 +82 3 +83 3 +84 3 +85 3 +86 3 +87 3 +88 3 +89 3 +90 3 +91 3 +92 3 +93 3 +94 3 +95 3 +96 3 +97 3 +98 3 +99 3 +100 3 diff --git a/challenge-113/abigail/t/input-1-4 b/challenge-113/abigail/t/input-1-4 new file mode 100644 index 0000000000..fadea6a19c --- /dev/null +++ b/challenge-113/abigail/t/input-1-4 @@ -0,0 +1,100 @@ +1 4 +2 4 +3 4 +4 4 +5 4 +6 4 +7 4 +8 4 +9 4 +10 4 +11 4 +12 4 +13 4 +14 4 +15 4 +16 4 +17 4 +18 4 +19 4 +20 4 +21 4 +22 4 +23 4 +24 4 +25 4 +26 4 +27 4 +28 4 +29 4 +30 4 +31 4 +32 4 +33 4 +34 4 +35 4 +36 4 +37 4 +38 4 +39 4 +40 4 +41 4 +42 4 +43 4 +44 4 +45 4 +46 4 +47 4 +48 4 +49 4 +50 4 +51 4 +52 4 +53 4 +54 4 +55 4 +56 4 +57 4 +58 4 +59 4 +60 4 +61 4 +62 4 +63 4 +64 4 +65 4 +66 4 +67 4 +68 4 +69 4 +70 4 +71 4 +72 4 +73 4 +74 4 +75 4 +76 4 +77 4 +78 4 +79 4 +80 4 +81 4 +82 4 +83 4 +84 4 +85 4 +86 4 +87 4 +88 4 +89 4 +90 4 +91 4 +92 4 +93 4 +94 4 +95 4 +96 4 +97 4 +98 4 +99 4 +100 4 diff --git a/challenge-113/abigail/t/input-1-5 b/challenge-113/abigail/t/input-1-5 new file mode 100644 index 0000000000..1c36c85f6d --- /dev/null +++ b/challenge-113/abigail/t/input-1-5 @@ -0,0 +1,100 @@ +1 5 +2 5 +3 5 +4 5 +5 5 +6 5 +7 5 +8 5 +9 5 +10 5 +11 5 +12 5 +13 5 +14 5 +15 5 +16 5 +17 5 +18 5 +19 5 +20 5 +21 5 +22 5 +23 5 +24 5 +25 5 +26 5 +27 5 +28 5 +29 5 +30 5 +31 5 +32 5 +33 5 +34 5 +35 5 +36 5 +37 5 +38 5 +39 5 +40 5 +41 5 +42 5 +43 5 +44 5 +45 5 +46 5 +47 5 +48 5 +49 5 +50 5 +51 5 +52 5 +53 5 +54 5 +55 5 +56 5 +57 5 +58 5 +59 5 +60 5 +61 5 +62 5 +63 5 +64 5 +65 5 +66 5 +67 5 +68 5 +69 5 +70 5 +71 5 +72 5 +73 5 +74 5 +75 5 +76 5 +77 5 +78 5 +79 5 +80 5 +81 5 +82 5 +83 5 +84 5 +85 5 +86 5 +87 5 +88 5 +89 5 +90 5 +91 5 +92 5 +93 5 +94 5 +95 5 +96 5 +97 5 +98 5 +99 5 +100 5 diff --git a/challenge-113/abigail/t/input-1-6 b/challenge-113/abigail/t/input-1-6 new file mode 100644 index 0000000000..a5a30f7ccb --- /dev/null +++ b/challenge-113/abigail/t/input-1-6 @@ -0,0 +1,100 @@ +1 6 +2 6 +3 6 +4 6 +5 6 +6 6 +7 6 +8 6 +9 6 +10 6 +11 6 +12 6 +13 6 +14 6 +15 6 +16 6 +17 6 +18 6 +19 6 +20 6 +21 6 +22 6 +23 6 +24 6 +25 6 +26 6 +27 6 +28 6 +29 6 +30 6 +31 6 +32 6 +33 6 +34 6 +35 6 +36 6 +37 6 +38 6 +39 6 +40 6 +41 6 +42 6 +43 6 +44 6 +45 6 +46 6 +47 6 +48 6 +49 6 +50 6 +51 6 +52 6 +53 6 +54 6 +55 6 +56 6 +57 6 +58 6 +59 6 +60 6 +61 6 +62 6 +63 6 +64 6 +65 6 +66 6 +67 6 +68 6 +69 6 +70 6 +71 6 +72 6 +73 6 +74 6 +75 6 +76 6 +77 6 +78 6 +79 6 +80 6 +81 6 +82 6 +83 6 +84 6 +85 6 +86 6 +87 6 +88 6 +89 6 +90 6 +91 6 +92 6 +93 6 +94 6 +95 6 +96 6 +97 6 +98 6 +99 6 +100 6 diff --git a/challenge-113/abigail/t/input-1-7 b/challenge-113/abigail/t/input-1-7 new file mode 100644 index 0000000000..f5cfcfa5aa --- /dev/null +++ b/challenge-113/abigail/t/input-1-7 @@ -0,0 +1,100 @@ +1 7 +2 7 +3 7 +4 7 +5 7 +6 7 +7 7 +8 7 +9 7 +10 7 +11 7 +12 7 +13 7 +14 7 +15 7 +16 7 +17 7 +18 7 +19 7 +20 7 +21 7 +22 7 +23 7 +24 7 +25 7 +26 7 +27 7 +28 7 +29 7 +30 7 +31 7 +32 7 +33 7 +34 7 +35 7 +36 7 +37 7 +38 7 +39 7 +40 7 +41 7 +42 7 +43 7 +44 7 +45 7 +46 7 +47 7 +48 7 +49 7 +50 7 +51 7 +52 7 +53 7 +54 7 +55 7 +56 7 +57 7 +58 7 +59 7 +60 7 +61 7 +62 7 +63 7 +64 7 +65 7 +66 7 +67 7 +68 7 +69 7 +70 7 +71 7 +72 7 +73 7 +74 7 +75 7 +76 7 +77 7 +78 7 +79 7 +80 7 +81 7 +82 7 +83 7 +84 7 +85 7 +86 7 +87 7 +88 7 +89 7 +90 7 +91 7 +92 7 +93 7 +94 7 +95 7 +96 7 +97 7 +98 7 +99 7 +100 7 diff --git a/challenge-113/abigail/t/input-1-8 b/challenge-113/abigail/t/input-1-8 new file mode 100644 index 0000000000..4f0c00743a --- /dev/null +++ b/challenge-113/abigail/t/input-1-8 @@ -0,0 +1,100 @@ +1 8 +2 8 +3 8 +4 8 +5 8 +6 8 +7 8 +8 8 +9 8 +10 8 +11 8 +12 8 +13 8 +14 8 +15 8 +16 8 +17 8 +18 8 +19 8 +20 8 +21 8 +22 8 +23 8 +24 8 +25 8 +26 8 +27 8 +28 8 +29 8 +30 8 +31 8 +32 8 +33 8 +34 8 +35 8 +36 8 +37 8 +38 8 +39 8 +40 8 +41 8 +42 8 +43 8 +44 8 +45 8 +46 8 +47 8 +48 8 +49 8 +50 8 +51 8 +52 8 +53 8 +54 8 +55 8 +56 8 +57 8 +58 8 +59 8 +60 8 +61 8 +62 8 +63 8 +64 8 +65 8 +66 8 +67 8 +68 8 +69 8 +70 8 +71 8 +72 8 +73 8 +74 8 +75 8 +76 8 +77 8 +78 8 +79 8 +80 8 +81 8 +82 8 +83 8 +84 8 +85 8 +86 8 +87 8 +88 8 +89 8 +90 8 +91 8 +92 8 +93 8 +94 8 +95 8 +96 8 +97 8 +98 8 +99 8 +100 8 diff --git a/challenge-113/abigail/t/input-1-9 b/challenge-113/abigail/t/input-1-9 new file mode 100644 index 0000000000..ca1b1dbeba --- /dev/null +++ b/challenge-113/abigail/t/input-1-9 @@ -0,0 +1,100 @@ +1 9 +2 9 +3 9 +4 9 +5 9 +6 9 +7 9 +8 9 +9 9 +10 9 +11 9 +12 9 +13 9 +14 9 +15 9 +16 9 +17 9 +18 9 +19 9 +20 9 +21 9 +22 9 +23 9 +24 9 +25 9 +26 9 +27 9 +28 9 +29 9 +30 9 +31 9 +32 9 +33 9 +34 9 +35 9 +36 9 +37 9 +38 9 +39 9 +40 9 +41 9 +42 9 +43 9 +44 9 +45 9 +46 9 +47 9 +48 9 +49 9 +50 9 +51 9 +52 9 +53 9 +54 9 +55 9 +56 9 +57 9 +58 9 +59 9 +60 9 +61 9 +62 9 +63 9 +64 9 +65 9 +66 9 +67 9 +68 9 +69 9 +70 9 +71 9 +72 9 +73 9 +74 9 +75 9 +76 9 +77 9 +78 9 +79 9 +80 9 +81 9 +82 9 +83 9 +84 9 +85 9 +86 9 +87 9 +88 9 +89 9 +90 9 +91 9 +92 9 +93 9 +94 9 +95 9 +96 9 +97 9 +98 9 +99 9 +100 9 diff --git a/challenge-113/abigail/t/input-2-1 b/challenge-113/abigail/t/input-2-1 new file mode 100644 index 0000000000..b4457aae66 --- /dev/null +++ b/challenge-113/abigail/t/input-2-1 @@ -0,0 +1 @@ +1 2 3 4 5 6 7 diff --git a/challenge-113/abigail/t/output-1-1.exp b/challenge-113/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..0d66ea1aee --- /dev/null +++ b/challenge-113/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +0 +1 diff --git a/challenge-113/abigail/t/output-1-10.exp b/challenge-113/abigail/t/output-1-10.exp new file mode 100644 index 0000000000..08641993e2 --- /dev/null +++ b/challenge-113/abigail/t/output-1-10.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 diff --git a/challenge-113/abigail/t/output-1-11.exp b/challenge-113/abigail/t/output-1-11.exp new file mode 100644 index 0000000000..f6d8fb48fb --- /dev/null +++ b/challenge-113/abigail/t/output-1-11.exp @@ -0,0 +1,100 @@ +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-2.exp b/challenge-113/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..502fda31a5 --- /dev/null +++ b/challenge-113/abigail/t/output-1-2.exp @@ -0,0 +1,100 @@ +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-3.exp b/challenge-113/abigail/t/output-1-3.exp new file mode 100644 index 0000000000..91bea422dd --- /dev/null +++ b/challenge-113/abigail/t/output-1-3.exp @@ -0,0 +1,100 @@ +0 +0 +1 +0 +0 +1 +0 +0 +1 +0 +0 +1 +1 +0 +1 +1 +0 +1 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-4.exp b/challenge-113/abigail/t/output-1-4.exp new file mode 100644 index 0000000000..2c5e99fc60 --- /dev/null +++ b/challenge-113/abigail/t/output-1-4.exp @@ -0,0 +1,100 @@ +0 +0 +0 +1 +0 +0 +0 +1 +0 +0 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-5.exp b/challenge-113/abigail/t/output-1-5.exp new file mode 100644 index 0000000000..0c9279c40d --- /dev/null +++ b/challenge-113/abigail/t/output-1-5.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +0 +0 +0 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-6.exp b/challenge-113/abigail/t/output-1-6.exp new file mode 100644 index 0000000000..909266ca6d --- /dev/null +++ b/challenge-113/abigail/t/output-1-6.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +1 +0 +0 +0 +1 +0 +1 +0 +0 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-7.exp b/challenge-113/abigail/t/output-1-7.exp new file mode 100644 index 0000000000..b6096c7e99 --- /dev/null +++ b/challenge-113/abigail/t/output-1-7.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +1 +0 +0 +1 +0 +0 +0 +1 +0 +0 +1 +0 +0 +1 +1 +0 +0 +1 +0 +0 +1 +1 +0 +1 +1 +0 +0 +1 +1 +0 +1 +1 +0 +1 +1 +1 +0 +1 +1 +0 +1 +1 +1 +1 +1 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-8.exp b/challenge-113/abigail/t/output-1-8.exp new file mode 100644 index 0000000000..d8979080a5 --- /dev/null +++ b/challenge-113/abigail/t/output-1-8.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +1 +0 +1 +0 +0 +0 +0 +0 +1 +0 +1 +0 +1 +0 +0 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-1-9.exp b/challenge-113/abigail/t/output-1-9.exp new file mode 100644 index 0000000000..ad3f007418 --- /dev/null +++ b/challenge-113/abigail/t/output-1-9.exp @@ -0,0 +1,100 @@ +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +1 +0 +0 +0 +0 +0 +0 +0 +1 +1 +1 +0 +0 +0 +0 +0 +0 +1 +1 +1 +1 +0 +0 +0 +0 +0 +1 +1 +1 +1 +1 +0 +0 +0 +0 +1 +1 +1 +1 +1 +1 +0 +0 +0 +1 +1 +1 +1 +1 +1 +1 +0 +0 +1 +1 +1 +1 +1 +1 +1 +1 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 diff --git a/challenge-113/abigail/t/output-2-1.exp b/challenge-113/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..82b0932624 --- /dev/null +++ b/challenge-113/abigail/t/output-2-1.exp @@ -0,0 +1 @@ +27 26 25 24 23 22 21 -- cgit From ac8b7856830f87b4cf01236a35badbf98b002338 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 15:26:55 +0200 Subject: Perl solutions for week 113 --- challenge-113/abigail/README.md | 2 + challenge-113/abigail/perl/ch-1.pl | 91 ++++++++++++++++++++++++++++++++++++++ challenge-113/abigail/perl/ch-2.pl | 39 ++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 challenge-113/abigail/perl/ch-1.pl create mode 100644 challenge-113/abigail/perl/ch-2.pl diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index f877fbafe7..9e786677a6 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -18,6 +18,7 @@ Output: 1 ~~~~ ### Solutions +* [Perl](perl/ch-1.pl) ### Blog @@ -52,5 +53,6 @@ Output: 1 ### Solutions +* [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..bd82344f3d --- /dev/null +++ b/challenge-113/abigail/perl/ch-1.pl @@ -0,0 +1,91 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# For any of the digits $D, more numbers $N can be written as a sum +# of positive integers, each containing the digit $D at least once. +# +# Let $D10 = $D == 0 ? 100 : $D * 10. +# +# It should be obvious that if $D > 0 and $N >= $D10, we can write $N as a +# sum of integers each containing at least one $D: +# +# For $D > 0: +# Let $N = $D * k + i, k >= 10 and 0 <= i < $D, +# then $N = $D10 + i + (k - 10) * $D. +# +# $D10 + i is a number containing exactly one $D, so $N can be +# written as a sum of (k - 9) integers: 10 * $D + 1 and k - 10 $D's. +# +# For $D = 0: +# Let $N = 100 + i + k * 10, for some 0 <= i < 10. +# +# 100 + i will contain a 0, so $N can be written as a sum of +# integers each containing a 0. +# +# It should also be clear that for $D > 0, if $N % $D == 0, $N can be +# written as a sum of integers containing $D, as $N is then a multiple of $D. +# +# For $N < $D10, $N can be written as a sum of positive integers +# containing at least one $D, iff: +# +# 1) $N is divisible by $D (or $D10 if $D == 0) OR +# 2) $N - 10 * l is non-negative and divisible by $D, +# for some 0 < l < $D. +# +# In case 1), $N is the sum of $N/$D $Ds. +# In case 2), $N is the sum of 10 * l + $D, and ($N - 10 * l - $D)/$D $Ds. +# +# +# We can further restrict which ls we have to check for a given $D: +# +# $D | l +# -----+----- +# 0 | +# 1 | +# 2 | 1 +# 3 | 1, 2 +# 4 | 1 +# 5 | +# 6 | 1, 2 +# 7 | 1, 2, 3, 4, 5, 6 +# 8 | 1, 2, 3 +# 9 | 1, 2, 3, 4, 5, 6, 7, 8 +# +# These are all the ls < $D so that $D does not divide 10 * l. + +my @l = ([], [], [1], [1 .. 2], [1], + [], [1 .. 2], [1 .. 6], [1 .. 3], [1 .. 8]); + +MAIN: while (<>) { + my ($N, $D) = /[0-9]+/g; + my $D10 = $D == 0 ? 100 : 10 * $D; + if ($N >= $D10 || ($N % ($D || 10) == 0)) { + say 1; + next MAIN; + } + for my $l (@{$l [$D]}) { + my $T = $N - 10 * $l - $D; + if ($T >= 0 && $T % $D == 0) { + say 1; + next MAIN; + } + } + say 0; +} diff --git a/challenge-113/abigail/perl/ch-2.pl b/challenge-113/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..4d7eeece79 --- /dev/null +++ b/challenge-113/abigail/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# Note that the "binary tree" part of the challenge is a red herring. +# We just have a bunch of numbers, and we want to replace each number +# with the sum of all the numbers, minus the number itself. +# +# We decide to complete ignore the binary tree part. Just like in a +# previous challenge, it's absolutely not stated what the format of +# binary tree is -- and the maker of the challenge gave a simple +# example of a 7 node tree, one which would immediately break down +# if we added an 8th node without adding an additional layer. +# +# So, we will just take a row of numbers as input. +# + +use List::Util qw [sum]; + +while (<>) { + my $sum = sum my @num = /[0-9]+/g; + say join " " => map {$sum - $_} @num; +} -- cgit From fe3a870ec5d417066f620307c01411f42422c0a3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 15:30:13 +0200 Subject: AWK solution for week 113 --- challenge-113/abigail/README.md | 1 + challenge-113/abigail/awk/ch-1.awk | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge-113/abigail/awk/ch-1.awk diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 9e786677a6..ef1c2d0e6a 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -18,6 +18,7 @@ Output: 1 ~~~~ ### Solutions +* [AWK](awk/ch-1.awk) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-113/abigail/awk/ch-1.awk b/challenge-113/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..4dd17cbbe5 --- /dev/null +++ b/challenge-113/abigail/awk/ch-1.awk @@ -0,0 +1,31 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +BEGIN { + split ("0 1 2 1 0 2 6 3 8", l) +} + +{ + N = $1 + D = $2 + D10 = D == 0 ? 100 : D * 10 + if (N >= D10 || N % (D == 0 ? 10 : D) == 0) { + print 1 + next + } + for (i = 1; i <= l [D]; i ++) { + T = N - 10 * i - D + if ((T >= 0) && (T % D == 0)) { + print 1 + next + } + } + print 0 +} -- cgit From 47bbd47bcea6fe7126654cd517425b9c5777b2ed Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 15:40:54 +0200 Subject: AWK solution for week 113, part 2 --- challenge-113/abigail/README.md | 1 + challenge-113/abigail/awk/ch-2.awk | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 challenge-113/abigail/awk/ch-2.awk diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index ef1c2d0e6a..4c09a4016b 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -54,6 +54,7 @@ Output: 1 ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/awk/ch-2.awk b/challenge-113/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..2d9d9ab2a7 --- /dev/null +++ b/challenge-113/abigail/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + sum = 0 + for (i = 1; i <= NF; i ++) { + sum += $i + } + for (i = 1; i <= NF; i ++) { + if (i > 1) { + printf " " + } + printf "%d", sum - $i + } + printf "\n" +} -- cgit From 0fa43d3480c8734e1dd0c3bd1134351115a4d3ae Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 15:46:49 +0200 Subject: Bash solution for week 113, part 2 --- challenge-113/abigail/README.md | 1 + challenge-113/abigail/bash/ch-2.sh | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 challenge-113/abigail/bash/ch-2.sh diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 4c09a4016b..47f50ad7f3 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -55,6 +55,7 @@ Output: 1 ### Solutions * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/bash/ch-2.sh b/challenge-113/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..d4e4040c3f --- /dev/null +++ b/challenge-113/abigail/bash/ch-2.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +while read -a numbers +do sum=0 + for ((i = 0; i < ${#numbers[@]}; i ++)) + do ((sum += numbers[i])) + done + for ((i = 0; i < ${#numbers[@]}; i ++)) + do if ((i > 0)) + then printf " " + fi + printf "%d" $((sum - numbers[i])) + done + printf "\n" +done -- cgit From d15beb5dcb76d40a0c324a9be4da7d570976c911 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 21:10:07 +0200 Subject: Bash solution for week 113, part 1 --- challenge-113/abigail/README.md | 1 + challenge-113/abigail/bash/ch-1.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-113/abigail/bash/ch-1.sh diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 47f50ad7f3..b4469041da 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -19,6 +19,7 @@ Output: 1 ### Solutions * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-113/abigail/bash/ch-1.sh b/challenge-113/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..7183523485 --- /dev/null +++ b/challenge-113/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +tens=(0 0 1 2 1 0 2 6 3 8) + +while read N D +do ((D10 = D == 0 ? 100 : 10 * D)) + if ((N >= D10 || (N % (D == 0 ? 10 : D) == 0))) + then echo 1 + continue + fi + for ((i = 1; i <= ${tens[$D]}; i ++)) + do ((T = N - 10 * i - D)) + if ((T >= 0 && T % D == 0)) + then echo 1 + continue 2 + fi + done + echo 0 +done + + -- cgit From ebf6f4e9f1f3c685d7b06b5033fd82f355e4523d Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 21:13:47 +0200 Subject: Make AWK and Perl solutions more in line --- challenge-113/abigail/awk/ch-1.awk | 6 +++--- challenge-113/abigail/perl/ch-1.pl | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/challenge-113/abigail/awk/ch-1.awk b/challenge-113/abigail/awk/ch-1.awk index 4dd17cbbe5..91923aadb6 100644 --- a/challenge-113/abigail/awk/ch-1.awk +++ b/challenge-113/abigail/awk/ch-1.awk @@ -9,7 +9,7 @@ # BEGIN { - split ("0 1 2 1 0 2 6 3 8", l) + split ("0 1 2 1 0 2 6 3 8", tens) } { @@ -20,9 +20,9 @@ BEGIN { print 1 next } - for (i = 1; i <= l [D]; i ++) { + for (i = 1; i <= tens [D]; i ++) { T = N - 10 * i - D - if ((T >= 0) && (T % D == 0)) { + if (T >= 0 && T % D == 0) { print 1 next } diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl index bd82344f3d..51a4842116 100644 --- a/challenge-113/abigail/perl/ch-1.pl +++ b/challenge-113/abigail/perl/ch-1.pl @@ -72,6 +72,7 @@ use experimental 'lexical_subs'; my @l = ([], [], [1], [1 .. 2], [1], [], [1 .. 2], [1 .. 6], [1 .. 3], [1 .. 8]); +my @tens = (0, 0, 1, 2, 1, 0, 2, 6, 3, 8); MAIN: while (<>) { my ($N, $D) = /[0-9]+/g; @@ -80,8 +81,8 @@ MAIN: while (<>) { say 1; next MAIN; } - for my $l (@{$l [$D]}) { - my $T = $N - 10 * $l - $D; + for (my $i = 1; $i <= $tens [$D]; $i ++) { + my $T = $N - 10 * $i - $D; if ($T >= 0 && $T % $D == 0) { say 1; next MAIN; -- cgit From e37b3682edfb32852e05d41a231fb43e9c0254f8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 18 May 2021 21:43:02 +0200 Subject: C solutions for week 113 --- challenge-113/abigail/README.md | 2 ++ challenge-113/abigail/c/ch-1.c | 44 ++++++++++++++++++++++++++++++++++++ challenge-113/abigail/c/ch-2.c | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 challenge-113/abigail/c/ch-1.c create mode 100644 challenge-113/abigail/c/ch-2.c diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index b4469041da..a1e156b3c0 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -20,6 +20,7 @@ Output: 1 ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ### Blog @@ -57,6 +58,7 @@ Output: 1 ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/c/ch-1.c b/challenge-113/abigail/c/ch-1.c new file mode 100644 index 0000000000..e673fa7281 --- /dev/null +++ b/challenge-113/abigail/c/ch-1.c @@ -0,0 +1,44 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +typedef long long number; +typedef short digit; + +unsigned short tens [] = {0, 0, 1, 2, 1, 0, 2, 6, 3, 8}; + +int main (void) { + number N; + digit D; + + while (scanf ("%lld %hd", &N, &D) == 2) { + digit D10 = D == 0 ? 100 : 10 * D; + if (N >= D10 || (N % (D ? D : 10) == 0)) { + printf ("1\n"); + continue; + } + bool valid = false; + for (unsigned short i = 1; i <= tens [D]; i ++) { + number T = N - 10 * i - D; + if (T >= 0 && T % D == 0) { + printf ("1\n"); + valid = true; + break; + } + } + if (!valid) { + printf ("0\n"); + } + } + + return (0); +} diff --git a/challenge-113/abigail/c/ch-2.c b/challenge-113/abigail/c/ch-2.c new file mode 100644 index 0000000000..fa2f79c27a --- /dev/null +++ b/challenge-113/abigail/c/ch-2.c @@ -0,0 +1,49 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +typedef long long number; + +int main (void) { + char * line = NULL; + size_t len = 0; + + while (getline (&line, &len, stdin) != -1) { + size_t offset = 0; + int skip; + long long n, sum; + + /* + * Read the numbers, calculate the sum. + */ + sum = 0; + while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) { + sum += n; + offset += skip; + } + + /* + * Read the numbers again, write output. + */ + offset = 0; + while (sscanf (line + offset, "%lld%n", &n, &skip) == 1) { + if (offset) { + printf (" "); + } + printf ("%lld", sum - n); + offset += skip; + } + printf ("\n"); + } + free (line); + + return (0); +} -- cgit From 23b2fd7bb31c97cd99ae62dc522a7607fcc2a297 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 19 May 2021 18:44:56 +0200 Subject: Lua solutions for week 113 --- challenge-113/abigail/README.md | 4 +++- challenge-113/abigail/lua/ch-1.lua | 40 ++++++++++++++++++++++++++++++++++++++ challenge-113/abigail/lua/ch-2.lua | 25 ++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 challenge-113/abigail/lua/ch-1.lua create mode 100644 challenge-113/abigail/lua/ch-2.lua diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index a1e156b3c0..3d3183a0d1 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -21,6 +21,7 @@ Output: 1 * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) * [Perl](perl/ch-1.pl) ### Blog @@ -58,7 +59,8 @@ Output: 1 ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [C](c/ch-1.c) +* [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..36bd8e8afd --- /dev/null +++ b/challenge-113/abigail/lua/ch-1.lua @@ -0,0 +1,40 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local tens = {0, 1, 2, 1, 0, 2, 6, 3, 8} + +for line in io . lines () do + local _, _, N, D = line : find ("([0-9]+)%s+([0-9])") + N = tonumber (N) + D = tonumber (D) + local D10 = 10 * D + if D == 0 + then D10 = 100 + end + if (N >= D10) or (D == 0 and N % 10 == 0) + or (D > 0 and N % D == 0) + then print (1) + goto end_loop + end + + if D > 0 + then for i = 1, tens [D] + do local T = N - 10 * i - D + if T >= 0 and T % D == 0 + then print (1) + goto end_loop + end + end + end + + print (0) + + ::end_loop:: +end diff --git a/challenge-113/abigail/lua/ch-2.lua b/challenge-113/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..15aac1ac2d --- /dev/null +++ b/challenge-113/abigail/lua/ch-2.lua @@ -0,0 +1,25 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local sum = 0 + for n in line : gmatch ("[0-9]+") + do sum = sum + tonumber (n) + end + local c = 0 + for n in line : gmatch ("[0-9]+") + do if c > 0 + then io . write (" ") + end + c = c + 1 + io . write (sum - n) + end + io . write ("\n") +end -- cgit From 98f821f841409373f067e0ed04629090a08ebb1f Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 19 May 2021 19:28:01 +0200 Subject: Node.js solution for week 113 --- challenge-113/abigail/README.md | 2 ++ challenge-113/abigail/node/ch-1.js | 30 ++++++++++++++++++++++++++++++ challenge-113/abigail/node/ch-2.js | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 challenge-113/abigail/node/ch-1.js create mode 100644 challenge-113/abigail/node/ch-2.js diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 3d3183a0d1..34146af125 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -22,6 +22,7 @@ Output: 1 * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) ### Blog @@ -61,6 +62,7 @@ Output: 1 * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/node/ch-1.js b/challenge-113/abigail/node/ch-1.js new file mode 100644 index 0000000000..2ca4f6a0e1 --- /dev/null +++ b/challenge-113/abigail/node/ch-1.js @@ -0,0 +1,30 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +let tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8]; + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => { + let [N, D] = _ . split (/\s+/) . map (_ => +_) + let D10 = D == 0 ? 100 : 10 * D + if (N >= D10 || (N % (D == 0 ? 10 : D) == 0)) { + console . log (1) + return + } + for (let i = 1; i <= tens [D]; i ++) { + let T = N - 10 * i - D + if (T >= 0 && T % D == 0) { + console . log (1) + return + } + } + console . log (0) +}) diff --git a/challenge-113/abigail/node/ch-2.js b/challenge-113/abigail/node/ch-2.js new file mode 100644 index 0000000000..606e95d09d --- /dev/null +++ b/challenge-113/abigail/node/ch-2.js @@ -0,0 +1,17 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => { + let numbers = _ . split (/\s+/) . map (_ => +_) + let sum = numbers . reduce ((acc, val) => acc + val) + console . log (numbers . map (_ => sum - _) . join (" ")) +}) -- cgit From 2eedf165fbeb5289fea6c43fbe55b47a0d1ec8f8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 19 May 2021 19:30:25 +0200 Subject: Allow negative numbers for week 113, part 2 (Perl, Lua) --- challenge-113/abigail/lua/ch-2.lua | 6 +++--- challenge-113/abigail/perl/ch-1.pl | 2 -- challenge-113/abigail/perl/ch-2.pl | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/challenge-113/abigail/lua/ch-2.lua b/challenge-113/abigail/lua/ch-2.lua index 15aac1ac2d..5ba4c04ed9 100644 --- a/challenge-113/abigail/lua/ch-2.lua +++ b/challenge-113/abigail/lua/ch-2.lua @@ -10,16 +10,16 @@ for line in io . lines () do local sum = 0 - for n in line : gmatch ("[0-9]+") + for n in line : gmatch ("-?[0-9]+") do sum = sum + tonumber (n) end local c = 0 - for n in line : gmatch ("[0-9]+") + for n in line : gmatch ("-?[0-9]+") do if c > 0 then io . write (" ") end c = c + 1 - io . write (sum - n) + io . write (sum - tonumber (n)) end io . write ("\n") end diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl index 51a4842116..bb45261f33 100644 --- a/challenge-113/abigail/perl/ch-1.pl +++ b/challenge-113/abigail/perl/ch-1.pl @@ -70,8 +70,6 @@ use experimental 'lexical_subs'; # # These are all the ls < $D so that $D does not divide 10 * l. -my @l = ([], [], [1], [1 .. 2], [1], - [], [1 .. 2], [1 .. 6], [1 .. 3], [1 .. 8]); my @tens = (0, 0, 1, 2, 1, 0, 2, 6, 3, 8); MAIN: while (<>) { diff --git a/challenge-113/abigail/perl/ch-2.pl b/challenge-113/abigail/perl/ch-2.pl index 4d7eeece79..7aabcc4b13 100644 --- a/challenge-113/abigail/perl/ch-2.pl +++ b/challenge-113/abigail/perl/ch-2.pl @@ -14,7 +14,7 @@ use experimental 'lexical_subs'; # # -# Run as: perl ch-1.pl < input-file +# Run as: perl ch-2.pl < input-file # # @@ -34,6 +34,6 @@ use experimental 'lexical_subs'; use List::Util qw [sum]; while (<>) { - my $sum = sum my @num = /[0-9]+/g; + my $sum = sum my @num = /-?[0-9]+/g; say join " " => map {$sum - $_} @num; } -- cgit From 82d812d593954b31a8187c65277e29363225dd55 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 20 May 2021 15:18:52 +0200 Subject: Python solutions for week 113 --- challenge-113/abigail/README.md | 2 ++ challenge-113/abigail/python/ch-1.py | 31 +++++++++++++++++++++++++++++++ challenge-113/abigail/python/ch-2.py | 16 ++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 challenge-113/abigail/python/ch-1.py create mode 100644 challenge-113/abigail/python/ch-2.py diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 34146af125..85ced087a5 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -24,6 +24,7 @@ Output: 1 * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ### Blog @@ -64,5 +65,6 @@ Output: 1 * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) ### Blog diff --git a/challenge-113/abigail/python/ch-1.py b/challenge-113/abigail/python/ch-1.py new file mode 100644 index 0000000000..7bf1b0db11 --- /dev/null +++ b/challenge-113/abigail/python/ch-1.py @@ -0,0 +1,31 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] + +for line in fileinput . input (): + (N, D) = line . split (); + N = int (N) + D = int (D) + D10 = 100 if D == 0 else 10 * D + if N >= D10 or (D == 0 and N % 10 == 0) or (D > 0 and N % D == 0): + print (1) + continue + done = False + for i in range (1, tens [D] + 1): + T = N - 10 * i - D + if T >= 0 and T % D == 0: + print (1) + done = True + break + if not done: + print (0) diff --git a/challenge-113/abigail/python/ch-2.py b/challenge-113/abigail/python/ch-2.py new file mode 100644 index 0000000000..73b6b9bc8b --- /dev/null +++ b/challenge-113/abigail/python/ch-2.py @@ -0,0 +1,16 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + numbers = list (map (lambda _: int (_), line . split ())) + total = sum (numbers) + print (' ' . join (map (lambda _: str (total - _), numbers))) -- cgit From 3a71b3d606f83f0ab27be2b1e1413d0094a7e3e9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 20 May 2021 19:59:47 +0200 Subject: Ruby solutions for week 113 --- challenge-113/abigail/README.md | 2 ++ challenge-113/abigail/ruby/ch-1.rb | 35 +++++++++++++++++++++++++++++++++++ challenge-113/abigail/ruby/ch-2.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 challenge-113/abigail/ruby/ch-1.rb create mode 100644 challenge-113/abigail/ruby/ch-2.rb diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 85ced087a5..2a7beef550 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -25,6 +25,7 @@ Output: 1 * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog @@ -66,5 +67,6 @@ Output: 1 * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) ### Blog diff --git a/challenge-113/abigail/ruby/ch-1.rb b/challenge-113/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..4f44bf5130 --- /dev/null +++ b/challenge-113/abigail/ruby/ch-1.rb @@ -0,0 +1,35 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] + +ARGF . each_line do + | line | + n, d = line . split + n = n . to_i + d = d . to_i + d10 = d == 0 ? 100 : d * 10 + if n >= d10 || n % (d == 0 ? 10 : d) == 0 + then puts (1) + next + end + done = false + for i in 1 .. tens [d] do + t = n - 10 * i - d + if t >= 0 && t % d == 0 + then puts (1) + done = true + break + end + end + if not done + then puts (0) + end +end diff --git a/challenge-113/abigail/ruby/ch-2.rb b/challenge-113/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..fe48ec54a7 --- /dev/null +++ b/challenge-113/abigail/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do + | line | + numbers = line . split . map do + | _ | + _ . to_i + end + sum = 0 + numbers . map do + | n | + sum += n + end + + puts (numbers . map do + | n | + sum - n + end . join (" ")) +end -- cgit From 9043a1b2b6b9d2a1ca0bcf814210f84b0655aa45 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 22 May 2021 18:19:05 +0200 Subject: Slightly improved algorithm for week 113, part 1. Also added a reference to a page with a proof of the algorithm. --- challenge-113/abigail/awk/ch-1.awk | 16 ++++++--- challenge-113/abigail/bash/ch-1.sh | 31 +++++++++++------ challenge-113/abigail/c/ch-1.c | 16 ++++++--- challenge-113/abigail/lua/ch-1.lua | 30 ++++++++++------- challenge-113/abigail/node/ch-1.js | 16 ++++++--- challenge-113/abigail/perl/ch-1.pl | 65 ++++++------------------------------ challenge-113/abigail/python/ch-1.py | 17 +++++++--- challenge-113/abigail/ruby/ch-1.rb | 18 +++++++--- 8 files changed, 111 insertions(+), 98 deletions(-) diff --git a/challenge-113/abigail/awk/ch-1.awk b/challenge-113/abigail/awk/ch-1.awk index 91923aadb6..950a1052d0 100644 --- a/challenge-113/abigail/awk/ch-1.awk +++ b/challenge-113/abigail/awk/ch-1.awk @@ -8,19 +8,27 @@ # Run as: awk -f ch-1.awk < input-file # +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + BEGIN { - split ("0 1 2 1 0 2 6 3 8", tens) + split ("1 2 1 2 5 2 1 2 1", gcds) } { N = $1 D = $2 - D10 = D == 0 ? 100 : D * 10 - if (N >= D10 || N % (D == 0 ? 10 : D) == 0) { + if (D == 0) { + print (N >= 100 || N % 10 == 0 ? 1 : 0) + next + } + if (N >= 10 * D) { print 1 next } - for (i = 1; i <= tens [D]; i ++) { + for (i = 0; i < D / gcds [D]; i ++) { T = N - 10 * i - D if (T >= 0 && T % D == 0) { print 1 diff --git a/challenge-113/abigail/bash/ch-1.sh b/challenge-113/abigail/bash/ch-1.sh index 7183523485..d3e60eddf4 100644 --- a/challenge-113/abigail/bash/ch-1.sh +++ b/challenge-113/abigail/bash/ch-1.sh @@ -8,22 +8,31 @@ # Run as: bash ch-1.sh < input-file # -tens=(0 0 1 2 1 0 2 6 3 8) +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + +gcds=(0 1 2 1 2 5 2 1 2 1) while read N D -do ((D10 = D == 0 ? 100 : 10 * D)) - if ((N >= D10 || (N % (D == 0 ? 10 : D) == 0))) +do if ((D == 0)) + then if ((N >= 100 || N % 10 == 0)) + then echo 1 + else echo 0 + fi + continue + fi + if ((N >= D * 10)) then echo 1 continue fi - for ((i = 1; i <= ${tens[$D]}; i ++)) - do ((T = N - 10 * i - D)) - if ((T >= 0 && T % D == 0)) - then echo 1 - continue 2 - fi + for ((i = 0; i < D / gcds[D]; i ++)) + do ((T = N - 10 * i - D)) + if ((T >= 0 && T % D == 0)) + then echo 1 + continue 2 + fi done echo 0 done - - diff --git a/challenge-113/abigail/c/ch-1.c b/challenge-113/abigail/c/ch-1.c index e673fa7281..dac9106ef7 100644 --- a/challenge-113/abigail/c/ch-1.c +++ b/challenge-113/abigail/c/ch-1.c @@ -11,23 +11,31 @@ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file */ +/* + * For a description of the algorithm, and the proof why this is correct: + * https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html + */ + typedef long long number; typedef short digit; -unsigned short tens [] = {0, 0, 1, 2, 1, 0, 2, 6, 3, 8}; +unsigned short gcds [] = {0, 1, 2, 1, 2, 5, 2, 1, 2, 1}; int main (void) { number N; digit D; while (scanf ("%lld %hd", &N, &D) == 2) { - digit D10 = D == 0 ? 100 : 10 * D; - if (N >= D10 || (N % (D ? D : 10) == 0)) { + if (D == 0) { + printf ("%d\n", N >= 100 || N % 10 == 0 ? 1 : 0); + continue; + } + if (N >= D * 10) { printf ("1\n"); continue; } bool valid = false; - for (unsigned short i = 1; i <= tens [D]; i ++) { + for (unsigned short i = 0; i < D / gcds [D]; i ++) { number T = N - 10 * i - D; if (T >= 0 && T % D == 0) { printf ("1\n"); diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua index 36bd8e8afd..4de70d106c 100644 --- a/challenge-113/abigail/lua/ch-1.lua +++ b/challenge-113/abigail/lua/ch-1.lua @@ -8,29 +8,35 @@ -- Run as: lua ch-1.lua < input-file -- -local tens = {0, 1, 2, 1, 0, 2, 6, 3, 8} +-- +-- For a description of the algorithm, and the proof why this is correct: +-- https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +-- + +local gcds = {1, 2, 1, 2, 5, 2, 1, 2, 1} for line in io . lines () do local _, _, N, D = line : find ("([0-9]+)%s+([0-9])") N = tonumber (N) D = tonumber (D) - local D10 = 10 * D if D == 0 - then D10 = 100 + then if N >= 100 or N % 10 == 0 + then print (1) + else print (0) + end + goto end_loop end - if (N >= D10) or (D == 0 and N % 10 == 0) - or (D > 0 and N % D == 0) + + if N >= D * 10 then print (1) goto end_loop end - if D > 0 - then for i = 1, tens [D] - do local T = N - 10 * i - D - if T >= 0 and T % D == 0 - then print (1) - goto end_loop - end + for i = 0, D / gcds [D] - 1 + do local T = N - 10 * i - D + if T >= 0 and T % D == 0 + then print (1) + goto end_loop end end diff --git a/challenge-113/abigail/node/ch-1.js b/challenge-113/abigail/node/ch-1.js index 2ca4f6a0e1..e755524470 100644 --- a/challenge-113/abigail/node/ch-1.js +++ b/challenge-113/abigail/node/ch-1.js @@ -8,18 +8,26 @@ // Run as: node ch-1.js < input-file // -let tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8]; +// +// For a description of the algorithm, and the proof why this is correct: +// https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +// + +let gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1]; require ('readline') . createInterface ({input: process . stdin}) . on ('line', _ => { let [N, D] = _ . split (/\s+/) . map (_ => +_) - let D10 = D == 0 ? 100 : 10 * D - if (N >= D10 || (N % (D == 0 ? 10 : D) == 0)) { + if (D == 0) { + console . log (N >= 100 || N % 10 == 0 ? 1 : 0) + return + } + if (N >= D * 10) { console . log (1) return } - for (let i = 1; i <= tens [D]; i ++) { + for (let i = 0; i < D / gcds [D]; i ++) { let T = N - 10 * i - D if (T >= 0 && T % D == 0) { console . log (1) diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl index bb45261f33..78a0eb0947 100644 --- a/challenge-113/abigail/perl/ch-1.pl +++ b/challenge-113/abigail/perl/ch-1.pl @@ -18,68 +18,23 @@ use experimental 'lexical_subs'; # # -# For any of the digits $D, more numbers $N can be written as a sum -# of positive integers, each containing the digit $D at least once. +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html # -# Let $D10 = $D == 0 ? 100 : $D * 10. -# -# It should be obvious that if $D > 0 and $N >= $D10, we can write $N as a -# sum of integers each containing at least one $D: -# -# For $D > 0: -# Let $N = $D * k + i, k >= 10 and 0 <= i < $D, -# then $N = $D10 + i + (k - 10) * $D. -# -# $D10 + i is a number containing exactly one $D, so $N can be -# written as a sum of (k - 9) integers: 10 * $D + 1 and k - 10 $D's. -# -# For $D = 0: -# Let $N = 100 + i + k * 10, for some 0 <= i < 10. -# -# 100 + i will contain a 0, so $N can be written as a sum of -# integers each containing a 0. -# -# It should also be clear that for $D > 0, if $N % $D == 0, $N can be -# written as a sum of integers containing $D, as $N is then a multiple of $D. -# -# For $N < $D10, $N can be written as a sum of positive integers -# containing at least one $D, iff: -# -# 1) $N is divisible by $D (or $D10 if $D == 0) OR -# 2) $N - 10 * l is non-negative and divisible by $D, -# for some 0 < l < $D. -# -# In case 1), $N is the sum of $N/$D $Ds. -# In case 2), $N is the sum of 10 * l + $D, and ($N - 10 * l - $D)/$D $Ds. -# -# -# We can further restrict which ls we have to check for a given $D: -# -# $D | l -# -----+----- -# 0 | -# 1 | -# 2 | 1 -# 3 | 1, 2 -# 4 | 1 -# 5 | -# 6 | 1, 2 -# 7 | 1, 2, 3, 4, 5, 6 -# 8 | 1, 2, 3 -# 9 | 1, 2, 3, 4, 5, 6, 7, 8 -# -# These are all the ls < $D so that $D does not divide 10 * l. -my @tens = (0, 0, 1, 2, 1, 0, 2, 6, 3, 8); +my @gcds = (0, 1, 2, 1, 2, 5, 2, 1, 2, 1); MAIN: while (<>) { - my ($N, $D) = /[0-9]+/g; - my $D10 = $D == 0 ? 100 : 10 * $D; - if ($N >= $D10 || ($N % ($D || 10) == 0)) { + my ($N, $D) = split; + if ($D == 0) { + say $N >= 100 || $N % 10 == 0 ? 1 : 0; + next MAIN; + } + if ($N >= $D * 10) { say 1; next MAIN; } - for (my $i = 1; $i <= $tens [$D]; $i ++) { + for (my $i = 0; $i < $D / $gcds [$D]; $i ++) { my $T = $N - 10 * $i - $D; if ($T >= 0 && $T % $D == 0) { say 1; diff --git a/challenge-113/abigail/python/ch-1.py b/challenge-113/abigail/python/ch-1.py index 7bf1b0db11..ebb8821ddc 100644 --- a/challenge-113/abigail/python/ch-1.py +++ b/challenge-113/abigail/python/ch-1.py @@ -8,20 +8,29 @@ # Run as: python ch-1.py < input-file # +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + import fileinput -tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] +gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1] for line in fileinput . input (): (N, D) = line . split (); N = int (N) D = int (D) - D10 = 100 if D == 0 else 10 * D - if N >= D10 or (D == 0 and N % 10 == 0) or (D > 0 and N % D == 0): + if D == 0: + print (1 if N >= 100 or N % 10 == 0 else 0) + continue + + if N >= D * 10: print (1) continue + done = False - for i in range (1, tens [D] + 1): + for i in range (0, D // gcds [D]): T = N - 10 * i - D if T >= 0 and T % D == 0: print (1) diff --git a/challenge-113/abigail/ruby/ch-1.rb b/challenge-113/abigail/ruby/ch-1.rb index 4f44bf5130..dadd1f1a9c 100644 --- a/challenge-113/abigail/ruby/ch-1.rb +++ b/challenge-113/abigail/ruby/ch-1.rb @@ -8,20 +8,30 @@ # Run as: ruby ch-1.rb < input-file # -tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + +gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1] ARGF . each_line do | line | n, d = line . split n = n . to_i d = d . to_i - d10 = d == 0 ? 100 : d * 10 - if n >= d10 || n % (d == 0 ? 10 : d) == 0 + if d == 0 + puts (n >= 100 || n % 10 == 0 ? 1 : 0) + next + end + + if n >= d * 10 then puts (1) next end + done = false - for i in 1 .. tens [d] do + for i in 0 .. d / gcds [d] - 1 do t = n - 10 * i - d if t >= 0 && t % d == 0 then puts (1) -- cgit From 1ab38e5de2d0bbab588c411e7764653217a9fc84 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 22 May 2021 18:21:10 +0200 Subject: Link to blog for week 113, part 1. --- challenge-113/abigail/README.md | 2 ++ challenge-113/abigail/blog.txt | 1 + 2 files changed, 3 insertions(+) create mode 100644 challenge-113/abigail/blog.txt diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 2a7beef550..3a02755680 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -28,6 +28,8 @@ Output: 1 * [Ruby](ruby/ch-1.rb) ### Blog +[Perl Weekly Challenge 113: Represent +Integer](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html) ## [Recreate Binary Tree](https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2) diff --git a/challenge-113/abigail/blog.txt b/challenge-113/abigail/blog.txt new file mode 100644 index 0000000000..24a8e123d1 --- /dev/null +++ b/challenge-113/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html -- cgit