aboutsummaryrefslogtreecommitdiff
path: root/challenge-125
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-08-16 15:30:40 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-08-16 15:30:40 -0400
commit637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3 (patch)
tree9809af6d10a767e998f569b80cabfdeb979ece95 /challenge-125
parent989dcde471a30935b8e51c557b971496963caffc (diff)
parentd9bbad705412903ffd55fb8c07bedcaeae734f85 (diff)
downloadperlweeklychallenge-club-637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3.tar.gz
perlweeklychallenge-club-637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3.tar.bz2
perlweeklychallenge-club-637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-125')
-rw-r--r--challenge-125/abigail/README.md112
-rw-r--r--challenge-125/abigail/awk/ch-1.awk44
-rw-r--r--challenge-125/abigail/blog.txt1
-rw-r--r--challenge-125/abigail/blog1.txt1
-rw-r--r--challenge-125/abigail/c/ch-1.c52
-rw-r--r--challenge-125/abigail/go/ch-1.go61
-rw-r--r--challenge-125/abigail/java/ch-1.java52
-rw-r--r--challenge-125/abigail/lua/ch-1.lua48
-rw-r--r--challenge-125/abigail/node/ch-1.js47
-rw-r--r--challenge-125/abigail/pascal/ch-1.p59
-rw-r--r--challenge-125/abigail/perl/ch-1.pl69
-rw-r--r--challenge-125/abigail/perl/ch-2.pl79
-rw-r--r--challenge-125/abigail/python/ch-1.py41
-rw-r--r--challenge-125/abigail/r/ch-1.r54
-rw-r--r--challenge-125/abigail/ruby/ch-1.rb45
-rw-r--r--challenge-125/abigail/t/ctest.ini10
-rw-r--r--challenge-125/abigail/t/input-1-13
-rw-r--r--challenge-125/abigail/t/input-1-22
-rw-r--r--challenge-125/abigail/t/input-1-398
-rw-r--r--challenge-125/abigail/t/input-1-4143
-rw-r--r--challenge-125/abigail/t/output-1-1.exp5
-rw-r--r--challenge-125/abigail/t/output-1-2.exp2
-rw-r--r--challenge-125/abigail/t/output-1-3.exp391
-rw-r--r--challenge-125/abigail/t/output-1-4.exp212
-rw-r--r--challenge-125/abigail/tcl/ch-1.tcl45
-rw-r--r--challenge-125/arne-sommer/blog.txt1
-rw-r--r--challenge-125/arne-sommer/raku/ch-1.raku32
-rw-r--r--challenge-125/arne-sommer/raku/ch-2.raku103
-rw-r--r--challenge-125/cheok-yin-fung/perl/ch-1.pl63
-rw-r--r--challenge-125/cheok-yin-fung/perl/ch-2.pl116
-rw-r--r--challenge-125/colin-crain/blog.txt1
-rw-r--r--challenge-125/colin-crain/perl/ch-1.pl220
-rw-r--r--challenge-125/colin-crain/perl/ch-2.pl380
-rw-r--r--challenge-125/colin-crain/raku/ch-1.raku46
-rw-r--r--challenge-125/colin-crain/raku/ch-2.raku195
-rw-r--r--challenge-125/duncan-c-white/README101
-rwxr-xr-xchallenge-125/duncan-c-white/perl/ch-1.pl81
-rwxr-xr-xchallenge-125/duncan-c-white/perl/ch-2.pl194
-rwxr-xr-xchallenge-125/e-choroba/perl/ch-1.pl78
-rwxr-xr-xchallenge-125/e-choroba/perl/ch-2.pl61
-rw-r--r--challenge-125/eric-cheung/excel-vba/Challenge_125.xlsmbin0 -> 33359 bytes
-rw-r--r--challenge-125/eric-cheung/excel-vba/ch-1.bas139
-rw-r--r--challenge-125/eric-cheung/python/ch-2.py66
-rw-r--r--challenge-125/james-smith/README.md23
-rw-r--r--challenge-125/james-smith/blog.txt1
-rwxr-xr-xchallenge-125/jo-37/perl/ch-1.pl134
-rwxr-xr-xchallenge-125/jo-37/perl/ch-2.pl79
-rwxr-xr-xchallenge-125/kai-burgdorf/perl/ch-1.pl50
-rw-r--r--challenge-125/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-125/laurent-rosenfeld/perl/ch-1.pl26
-rw-r--r--challenge-125/laurent-rosenfeld/raku/ch-1.raku20
-rw-r--r--challenge-125/mark-anderson/perl/ch-2.pl62
-rw-r--r--challenge-125/mark-anderson/raku/ch-1.raku56
-rwxr-xr-xchallenge-125/mattneleigh/perl/ch-1.pl124
-rwxr-xr-xchallenge-125/mattneleigh/perl/ch-2.pl308
-rw-r--r--challenge-125/mimosinnet/raku/ch-1.raku59
-rw-r--r--challenge-125/mimosinnet/raku/ch-2.raku68
-rw-r--r--challenge-125/pete-houston/perl/ch-1.pl62
-rw-r--r--challenge-125/peter-campbell-smith/perl/ch-1.pl55
-rw-r--r--challenge-125/polettix/blog.txt1
-rw-r--r--challenge-125/polettix/blog1.txt1
-rw-r--r--challenge-125/polettix/perl/ch-1.pl31
-rw-r--r--challenge-125/polettix/perl/ch-2.pl74
-rw-r--r--challenge-125/polettix/raku/ch-1.raku42
-rw-r--r--challenge-125/polettix/raku/ch-2.raku32
-rw-r--r--challenge-125/simon-proctor/raku/ch-1.raku28
-rw-r--r--challenge-125/simon-proctor/raku/ch-2.raku158
-rwxr-xr-xchallenge-125/stuart-little/haskell/ch-1.hs13
-rwxr-xr-xchallenge-125/stuart-little/haskell/ch-2.hs48
-rwxr-xr-xchallenge-125/stuart-little/lua/ch-1.lua27
-rwxr-xr-xchallenge-125/stuart-little/lua/ch-2.lua63
-rwxr-xr-xchallenge-125/stuart-little/node/ch-1.js27
-rwxr-xr-xchallenge-125/stuart-little/node/ch-2.js51
-rwxr-xr-xchallenge-125/stuart-little/perl/ch-1.pl25
-rwxr-xr-xchallenge-125/stuart-little/perl/ch-2.pl64
-rwxr-xr-xchallenge-125/stuart-little/python/ch-1.py18
-rwxr-xr-xchallenge-125/stuart-little/python/ch-2.py57
-rwxr-xr-xchallenge-125/stuart-little/raku/ch-1.raku21
-rwxr-xr-xchallenge-125/stuart-little/raku/ch-2.raku55
-rw-r--r--challenge-125/ulrich-rieke/cpp/ch-1.cpp43
-rw-r--r--challenge-125/ulrich-rieke/haskell/ch-1.hs10
-rw-r--r--challenge-125/ulrich-rieke/perl/ch-1.pl25
-rw-r--r--challenge-125/ulrich-rieke/raku/ch-1.raku23
-rw-r--r--challenge-125/wambash/raku/ch-1.raku33
-rw-r--r--challenge-125/wanderdoc/perl/ch-2.pl111
-rw-r--r--challenge-125/wlmb/blog.txt1
-rwxr-xr-xchallenge-125/wlmb/perl/ch-1.pl32
-rwxr-xr-xchallenge-125/wlmb/perl/ch-2.pl46
88 files changed, 5746 insertions, 95 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) {
+ printf ("-1\n");
+ continue;
+ }
+
+ number n_sq = n * n;
+ number c = n + 1;
+ number c_sq = n_sq + 2 * n + 1;
+ while (2 * c - 1 <= n_sq) {
+ number b_sq = c_sq - n_sq;
+ number b = introot (b_sq);
+ if (b_sq == b * b) {
+ printf ("%lld %lld %lld\n", n, b, c);
+ }
+ c_sq += 2 * c ++ + 1;
+ }
+
+ number max_a = (number) floorl (n / sqrt (2));
+ for (number a = 3; a <= max_a; a ++) {
+ number b_sq = n_sq - a * a;
+ number b = introot (b_sq);
+ if (b_sq == b * b) {
+ printf ("%lld %lld %lld\n", a, b, n);
+ }
+ }
+ }
+
+ return (0);
+}
diff --git a/challenge-125/abigail/go/ch-1.go b/challenge-125/abigail/go/ch-1.go
new file mode 100644
index 0000000000..f69846b7ed
--- /dev/null
+++ b/challenge-125/abigail/go/ch-1.go
@@ -0,0 +1,61 @@
+package main
+
+//
+// See ../README.md
+//
+
+//
+// Run as: go run ch-1.go
+//
+
+import (
+ "fmt"
+ "math"
+)
+
+func introot (square int) int {
+ return (int (math . Round (math . Sqrt (float64 (square)))));
+}
+
+func main () {
+ for {
+ var n, n_sq, a, max_a, b, b_sq, c, c_sq int;
+ var count, err = fmt . Scanf ("%d", &n);
+ if (err != nil || count != 1) {
+ break;
+ }
+
+ if (n <= 2) {
+ fmt . Println ("-1");
+ continue;
+ }
+
+ n_sq = n * n;
+ c = n + 1;
+ c_sq = n_sq + 2 * n + 1;
+
+ for 2 * c - 1 <= n_sq {
+ b_sq = c_sq - n_sq;
+ b = introot (b_sq);
+
+ if (b_sq == b * b) {
+ fmt . Printf ("%d %d %d\n", n, b, c);
+ }
+
+ c_sq += 2 * c + 1;
+ c += 1;
+ }
+
+ max_a = int (float64 (n) / math . Sqrt (float64 (2)));
+
+ a = 3;
+ for a <= max_a {
+ b_sq = n_sq - a * a
+ b = introot (b_sq)
+ if (b_sq == b * b) {
+ fmt . Printf ("%d %d %d\n", a, b, n);
+ }
+ a += 1;
+ }
+ }
+}
diff --git a/challenge-125/abigail/java/ch-1.java b/challenge-125/abigail/java/ch-1.java
new file mode 100644
index 0000000000..722b6d0f81
--- /dev/null
+++ b/challenge-125/abigail/java/ch-1.java
@@ -0,0 +1,52 @@
+//
+// See ../README.md
+//
+
+//
+// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file
+//
+
+import java.util.*;
+
+public class ch1 {
+ public static long i