diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-05-23 12:11:39 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-05-23 12:11:39 -0500 |
| commit | af750f6fd9ce9389bd6c12b3aa92fb062451b813 (patch) | |
| tree | cbe4da14753c7ce57101773e0db98db7785ac890 | |
| parent | 088dbd3dc9664b07cdf8ab0872c68a903438a0e0 (diff) | |
| parent | 03eb3fa7435d497e68348362885148da5298156e (diff) | |
| download | perlweeklychallenge-club-af750f6fd9ce9389bd6c12b3aa92fb062451b813.tar.gz perlweeklychallenge-club-af750f6fd9ce9389bd6c12b3aa92fb062451b813.tar.bz2 perlweeklychallenge-club-af750f6fd9ce9389bd6c12b3aa92fb062451b813.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
83 files changed, 5420 insertions, 1559 deletions
diff --git a/challenge-112/wambash/raku/ch-2.raku b/challenge-112/wambash/raku/ch-2.raku index 81e8cd6460..78e407f6f7 100644 --- a/challenge-112/wambash/raku/ch-2.raku +++ b/challenge-112/wambash/raku/ch-2.raku @@ -9,7 +9,7 @@ multi MAIN ( $n ) { say climb-stairs $n; } -multi MAIN ( :test($) ) { +multi MAIN ( :test($)! ) { use Test; is climb-stairs(3),3; is climb-stairs(4),5; diff --git a/challenge-113/aaronreidsmith/blog.txt b/challenge-113/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..9617638f2d --- /dev/null +++ b/challenge-113/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-113/ diff --git a/challenge-113/aaronreidsmith/raku/ch-1.raku b/challenge-113/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..20f68521aa --- /dev/null +++ b/challenge-113/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +subset PositiveInt of Int where * > 0; + +sub challenge(PositiveInt $N, PositiveInt $D) returns Int { + my $output = (1..^$N) + .race + .grep(*.contains($D)) + .combinations(2..*) + .map(*.sum) + .any == $N; + $output.Bool.Int; +} + +multi sub MAIN(PositiveInt $N, PositiveInt $D) { + say challenge($N, $D); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (25, 7, 0), + (24, 7, 1), + (97, 4, 1) + ); + + for @tests -> ($N, $D, $expected) { + is(challenge($N, $D), $expected); + } + + done-testing; +} diff --git a/challenge-113/aaronreidsmith/raku/ch-2.raku b/challenge-113/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..52e58e8fab --- /dev/null +++ b/challenge-113/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,73 @@ +#!/usr/bin/env raku + +subset NodeValue of Str where { $_ ~~ /^<digit>$/ || $_ eq 'Nil' } + +class Node { + has Node $.left is rw = Nil; + has Node $.right is rw = Nil; + has Int $.value is rw = 0; +} + +# Adapted from https://rosettacode.org/wiki/Visualize_a_tree#Raku +sub format-tree( + Node $root, + Str :$indent = '', + :@mid = ('├─', '│ '), + :@end = ('└─', ' ') +) returns Str { + sub visit(Node $node, *@pre) { + with $node { + |gather { + take @pre[0] ~ $node.value; + my @children = ($node.right, $node.left).grep(*.defined); + my $end = @children.end; + for @children.kv -> $_, $child { + when $end { take visit($child, (@pre[1] X~ @end)) } + default { take visit($child, (@pre[1] X~ @mid)) } + } + } + } + } + visit($root, $indent xx 2).join("\n"); +} + +sub build-tree(@array, $root is copy = Nil, Int $i = 0) returns Node { + if $i < @array.elems && @array[$i] ne 'Nil' { + $root = Node.new(value => @array[$i].Int); + $root.left = build-tree(@array, $root.left, 2 * $i + 1); + $root.right = build-tree(@array, $root.right, 2 * $i + 2); + } + $root; +} + +sub challenge(Node $root is copy, @values = ()) returns Node { + sub extract-values(Node $root) returns Positional { + with $root { + ($root.value, |extract-values($root.left), |extract-values($root.right)); + } + } + + with $root { + my @node-values = @values.elems > 0 ?? @values !! extract-values($root); + $root.value = @node-values.grep(* != $root.value).sum; + challenge($root.left, @node-values); + challenge($root.right, @node-values); + } + $root; +} + +multi sub MAIN(*@nodes where all(@nodes) ~~ NodeValue) { + my $root = build-tree(@nodes); + say format-tree(challenge($root)); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my $input-tree = build-tree(<1 2 3 4 Nil 5 6 Nil 7 Nil Nil>); + my $expected-tree = build-tree(<27 26 25 24 Nil 23 22 Nil 21 Nil Nil>); + + is(format-tree(challenge($input-tree)), format-tree($expected-tree)); + + done-testing; +} 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 fil |
