diff options
| author | Scimon <simon.proctor@gmail.com> | 2021-08-16 09:09:50 +0100 |
|---|---|---|
| committer | Scimon <simon.proctor@gmail.com> | 2021-08-16 09:09:50 +0100 |
| commit | a40d5274a80b7dc86d043433b77928857d904698 (patch) | |
| tree | 839fa66142ee3a743bedfe86cc486a5b6573accc /challenge-125 | |
| parent | c976e26a845911cf0226844acfc1e4c0e497c8c1 (diff) | |
| parent | 2d08dfcd1f2742e1621be6950a42614b4dbea797 (diff) | |
| download | perlweeklychallenge-club-a40d5274a80b7dc86d043433b77928857d904698.tar.gz perlweeklychallenge-club-a40d5274a80b7dc86d043433b77928857d904698.tar.bz2 perlweeklychallenge-club-a40d5274a80b7dc86d043433b77928857d904698.zip | |
Merge branch 'master' of github.com:Scimon/perlweeklychallenge-club
Diffstat (limited to 'challenge-125')
99 files changed, 6273 insertions, 303 deletions
diff --git a/challenge-125/abigail/README.md b/challenge-125/abigail/README.md index c116d92523..677f80fccf 100644 --- a/challenge-125/abigail/README.md +++ b/challenge-125/abigail/README.md @@ -1,95 +1,93 @@ # Solutions by Abigail -## [Happy Women Day][task1] +## [Pythagorean Triples][task1] -> Write a script to print the Venus Symbol, international gender symbol -> for women. Please feel free to use any character. +> You are given a positive integer `$N`. +> +> Write a script to print all Pythagorean Triples containing $N as +> a member. Print `-1` if it can't be a member of any. +> +> Triples with the same set of elements are considered the same, +> i.e. if your script has already printed `(3, 4, 5)`, `(4, 3, 5)` should +> not be printed. +> +> > The famous Pythagorean theorem states that in a right angle +> > triangle, the length of the two shorter sides and the length of the +> > longest side are related by `a^2+b^2 = c^2`. +### Example ~~~~ - ^^^^^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^ ^ - ^^^^^ - ^ - ^ - ^ - ^^^^^ - ^ - ^ +Input: $N = 5 +Output: (3, 4, 5) + (5, 12, 13) + +Input: $N = 13 +Output: (5, 12, 13) + (13, 84, 85) + +Input: $N = 1 +Output: -1 ~~~~ ### Solutions * [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [Basic](basic/ch-1.bas) -* [bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) -* [Cobol](cobol/ch-1.cb) -* [Csh](csh/ch-1.csh) -* [Erlang](erlang/ch-1.erl) -* [Forth](forth/ch-1.fs) -* [Fortran](fortran/ch-1.f90) * [Go](go/ch-1.go) * [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) -* [m4](m4/ch-1.m4) -* [MMIX](mmix/ch-1.mms) * [Node.js](node/ch-1.js) -* [Ocaml](ocaml/ch-1.ml) * [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [Php](php/ch-1.php) -* [Postscript](postscript/ch-1.ps) * [Python](python/ch-1.py) * [R](r/ch-1.r) -* [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [SQL](sql/ch-1.sql) * [Tcl](tcl/ch-1.tcl) ### Blog -[Perl Weekly Challenge 124: Happy Women Day][blog1] +[Perl Weekly Challenge 125: Pythagorean Triples][blog1] + +## [Binary Tree Diameter][task2] -## [Tug of War][task2] +> You are given binary tree as below: -> You are given a set of $n integers `(n1, n2, n3, ...)`. +~~~~ + 1 + / \ + 2 5 + / \ / \ +3 4 6 7 + / \ + 8 10 + / + 9 +~~~~ + +> Write a script to find the diameter of the given binary tree. > -> Write a script to divide the set in two subsets of `n/2` sizes each -> so that the difference of the sum of two subsets is the least. If -> `$n` is even then each subset must be of size `$n/2` each. In case $n -> is odd then one subset must be `($n-1)/2` and other must be `($n+1)/2`. +> > The diameter of a binary tree is the length of the longest path +> > between any two nodes in a tree. It doesn't have to pass +> > through the root. + +For the above given binary tree, possible diameters (7) are: -### Examples ~~~~ -Input: Set = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) -Output: Subset 1 = (30, 40, 60, 70, 80) - Subset 2 = (10, 20, 50, 90, 100) +3, 2, 1, 5, 7, 8, 9 ~~~~ +or + ~~~~ -Input: Set = (10, -15, 20, 30, -25, 0, 5, 40, -5) - Subset 1 = (30, 0, 5, -5) - Subset 2 = (10, -15, 20, -25, 40) +4, 2, 1, 5, 7, 8, 9 ~~~~ ### Solutions * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) ### Blog -[Perl Weekly Challenge 124: Tug of War][blog2] +[Perl Weekly Challenge 125: Binary Tree Diameter][blog2] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-124/#TASK1 -[task2]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-124/#TASK2 -[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-124-1.html -[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-124-2.html +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-125/#TASK1 +[task2]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-125/#TASK2 +[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-1.html +[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-2.html diff --git a/challenge-125/abigail/awk/ch-1.awk b/challenge-125/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..1758b7d64d --- /dev/null +++ b/challenge-125/abigail/awk/ch-1.awk @@ -0,0 +1,44 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +function introot (square) { + return (int (.4 + sqrt (square))) +} + + +$1 <= 2 { + print (-1) + next +} + + +{ + n = $1 + n_sq = n * n + c = n + 1 + c_sq = n_sq + 2 * n + 1 + while (2 * c - 1 <= n_sq) { + b_sq = c_sq - n_sq + b = introot(b_sq) + if (b_sq == b * b) { + print n, b, c + } + c_sq += 2 * c ++ + 1 + } + + max_a = int (n / sqrt (2)) + for (a = 3; a <= max_a; a ++) { + b_sq = n_sq - a * a + b = introot(b_sq) + if (b_sq == b * b) { + print a, b, n + } + } +} diff --git a/challenge-125/abigail/blog.txt b/challenge-125/abigail/blog.txt new file mode 100644 index 0000000000..79e86ddc80 --- /dev/null +++ b/challenge-125/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-1.html diff --git a/challenge-125/abigail/blog1.txt b/challenge-125/abigail/blog1.txt new file mode 100644 index 0000000000..ce0adcab58 --- /dev/null +++ b/challenge-125/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-125-2.html diff --git a/challenge-125/abigail/c/ch-1.c b/challenge-125/abigail/c/ch-1.c new file mode 100644 index 0000000000..63b3a67db1 --- /dev/null +++ b/challenge-125/abigail/c/ch-1.c @@ -0,0 +1,52 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <math.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +typedef long long number; + +number introot (number square) { + return ((number) floorl (.4 + sqrt (square))); +} + +int main (void) { + number n; + + while (scanf ("%lld", &n) == 1) { + if (n <= 2) { + |
