diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-23 16:59:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-23 16:59:39 +0100 |
| commit | 64d90deae0b1c6e8fe708937f775f0975f138a4f (patch) | |
| tree | cf2877cafb333f34246a86a5ab81a0abd8d07899 | |
| parent | d03259c86c349e8efbbd6622dfdfcaa3c5f527f2 (diff) | |
| parent | 1ab38e5de2d0bbab588c411e7764653217a9fc84 (diff) | |
| download | perlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.tar.gz perlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.tar.bz2 perlweeklychallenge-club-64d90deae0b1c6e8fe708937f775f0975f138a4f.zip | |
Merge pull request #4120 from Abigail/abigail/week-113
Abigail/week 113
43 files changed, 2625 insertions, 40 deletions
diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index 4a8667ee09..3a02755680 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -1,35 +1,20 @@ # 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 @@ -43,34 +28,47 @@ Output: "/a" * [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 112: Canonical Path](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-112-1.html) +[Perl Weekly Challenge 113: Represent +Integer](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-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) diff --git a/challenge-113/abigail/awk/ch-1.awk b/challenge-113/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..950a1052d0 --- /dev/null +++ b/challenge-113/abigail/awk/ch-1.awk @@ -0,0 +1,39 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# 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 ("1 2 1 2 5 2 1 2 1", gcds) +} + +{ + N = $1 + D = $2 + if (D == 0) { + print (N >= 100 || N % 10 == 0 ? 1 : 0) + next + } + if (N >= 10 * D) { + print 1 + next + } + for (i = 0; i < D / gcds [D]; i ++) { + T = N - 10 * i - D + if (T >= 0 && T % D == 0) { + print 1 + next + } + } + print 0 +} 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" +} diff --git a/challenge-113/abigail/bash/ch-1.sh b/challenge-113/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..d3e60eddf4 --- /dev/null +++ b/challenge-113/abigail/bash/ch-1.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < 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 +# + +gcds=(0 1 2 1 2 5 2 1 2 1) + +while read N D +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 = 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/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 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 diff --git a/challenge-113/abigail/c/ch-1.c b/challenge-113/abigail/c/ch-1.c new file mode 100644 index 0000000000..dac9106ef7 --- /dev/null +++ b/challenge-113/abigail/c/ch-1.c @@ -0,0 +1,52 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * See ../README.md + */ + +/* + * 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 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) { + 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 = 0; i < D / gcds [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 <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * 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); +} diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..4de70d106c --- /dev/null +++ b/challenge-113/abigail/lua/ch-1.lua @@ -0,0 +1,46 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < 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 +-- + +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) + if D == 0 + then if N >= 100 or N % 10 == 0 + then print (1) + else print (0) + end + goto end_loop + end + + if N >= D * 10 + 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 + + 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..5ba4c04ed9 --- /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 - tonumber (n)) + end + io . write ("\n") +end diff --git a/challenge-113/abigail/node/ch-1.js b/challenge-113/abigail/node/ch-1.js new file mode 100644 index 0000000000..e755524470 --- /dev/null +++ b/challenge-113/abigail/node/ch-1.js @@ -0,0 +1,38 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < 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 +// + +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 (_ => +_) + if (D == 0) { + console . log (N >= 100 || N % 10 == 0 ? 1 : 0) + return + } + if (N >= D * 10) { + console . log (1) + return + } + for (let i = 0; i < D / gcds [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 (" ")) +}) diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..78a0eb0947 --- /dev/null +++ b/challenge-113/abigail/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/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 a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + +my @gcds = (0, 1, 2, 1, 2, 5, 2, 1, 2, 1); + +MAIN: while (<>) { + 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 = 0; $i < $D / $gcds [$D]; $i ++) { + my $T = $N - 10 * $i - $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..7aabcc4b13 --- /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-2.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; +} diff --git a/challenge-113/abigail/python/ch-1.py b/challenge-113/abigail/python/ch-1.py new file mode 100644 index 0000000000..ebb8821ddc --- /dev/null +++ b/challenge-113/abigail/python/ch-1.py @@ -0,0 +1,40 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# 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 + +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) + 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 (0, D // gcds [D]): + 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))) diff --git a/challenge-113/abigail/ruby/ch-1.rb b/challenge-113/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..dadd1f1a9c --- /dev/null +++ b/challenge-113/abigail/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < 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 +# + +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 + 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 0 .. d / gcds [d] - 1 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 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 |
