diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-07-30 08:57:30 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-07-30 08:57:30 +0100 |
| commit | ccf0d4d21e8537f4d49a5361efa0f4604120d994 (patch) | |
| tree | 34cea29c10fec51c332be35357e34865bfbc537b | |
| parent | bd5b2f31679196043f9ac71ed8cb59aed316a818 (diff) | |
| parent | ca345f30cf5b12529f4df08b50b3d0f188974d36 (diff) | |
| download | perlweeklychallenge-club-ccf0d4d21e8537f4d49a5361efa0f4604120d994.tar.gz perlweeklychallenge-club-ccf0d4d21e8537f4d49a5361efa0f4604120d994.tar.bz2 perlweeklychallenge-club-ccf0d4d21e8537f4d49a5361efa0f4604120d994.zip | |
Merge remote-tracking branch 'upstream/master'
62 files changed, 3386 insertions, 2003 deletions
diff --git a/challenge-123/abigail/README.md b/challenge-123/abigail/README.md index fff6c14deb..e71bda7e0f 100644 --- a/challenge-123/abigail/README.md +++ b/challenge-123/abigail/README.md @@ -1,96 +1,84 @@ # Solutions by Abigail -## [Average of Stream][task1] +## [Ugly Numbers][task1] -> You are given a stream of numbers, `@N`. -> -> Write a script to print the average of the stream at every point. +> You are given an integer `$n >= 1`. +> +> Write a script to find the $nth element of Ugly Numbers. +> +> > Ugly numbers are those number whose prime factors are `2`, `3` or `5`. +> > For example, the first 10 Ugly Numbers are `1`, `2`, `3`, `4`, `5`, +> > `6`, `8`, `9`, `10`, `12`. -### Example -~~~~ -Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) -Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... +### Examples ~~~~ +Input: $n = 7 +Output: 8 -* Average of first number is `10`. -* Average of first 2 numbers `(10+20)/2 = 15`. -* Average of first 3 numbers `(10+20+30)/3 = 20`. -* Average of first 4 numbers `(10+20+30+40)/4 = 25` and so on. +Input: $n = 10 +Output: 12 +~~~~ ### 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.bf) * [C](c/ch-1.c) -* [Go](go/ch-1.go) -* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [Tcl](tcl/ch-1.tcl) ### Blog -[Average of Stream][blog1] +[Perl Weekly Challenge 123: Ugly Numbers][blog1] -## [Basketball Points][task2] +## [Square Points][task2] -> You are given a score `$S`. +> You are given coordinates of four points i.e. `(x1, y1)`, `(x2, y2)`, +> `(x3, y3)` and `(x4, y4)`. > -> You can win basketball points e.g. `1` point, `2` points and `3` points. -> -> Write a script to find out the different ways you can score `$S`. +> Write a script to find out if the given four points form a square. ### Examples ~~~~ -Input: $S = 4 -Output: 1 1 1 1 - 1 1 2 - 1 2 1 - 1 3 - 2 1 1 - 2 2 - 3 1 +Input: x1 = 10, y1 = 20 + x2 = 20, y2 = 20 + x3 = 20, y3 = 10 + x4 = 10, y4 = 10 +Output: 1 as the given coordinates form a square. ~~~~ ~~~~ -Input: $S = 5 -Output: 1 1 1 1 1 - 1 1 1 2 - 1 1 2 1 - 1 1 3 - 1 2 1 1 - 1 2 2 - 1 3 1 - 2 1 1 1 - 2 1 2 - 2 2 1 - 2 3 - 3 1 1 - 3 2 +Input: x1 = 12, y1 = 24 + x2 = 16, y2 = 10 + x3 = 20, y3 = 12 + x4 = 18, y4 = 16 +Output: 0 as the given coordinates doesn't form a square. ~~~~ ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) * [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) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) +* [Scheme](scheme/ch-2.scm) +* [Tcl](tcl/ch-2.tcl) ### Blog -[Basketball Points][blog2] +[Perl Weekly Challenge 123: Square Points][blog2] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK1 -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK2 -[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-1.html -[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-2.html +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-123/#TASK1 +[task2]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-123/#TASK2 +[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-123-1.html +[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-123-2.html diff --git a/challenge-123/abigail/awk/ch-1.awk b/challenge-123/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..92040bfcfe --- /dev/null +++ b/challenge-123/abigail/awk/ch-1.awk @@ -0,0 +1,39 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +BEGIN { + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + count = 1 +} + +{ + while (count < $1) { + c2 = 2 * ugly [next_2] + c3 = 3 * ugly [next_3] + c5 = 5 * ugly [next_5] + + ugly [count] = c2 < c3 ? c2 < c5 ? c2 : c5 \ + : c3 < c5 ? c3 : c5 + + if (2 * ugly [next_2] <= ugly [count]) {next_2 ++} + if (3 * ugly [next_3] <= ugly [count]) {next_3 ++} + if (5 * ugly [next_5] <= ugly [count]) {next_5 ++} + + count ++ + } + print ugly [$1 - 1] +} + + + + diff --git a/challenge-123/abigail/awk/ch-2.awk b/challenge-123/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..27709d6fd0 --- /dev/null +++ b/challenge-123/abigail/awk/ch-2.awk @@ -0,0 +1,20 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + e1 = ($1 - $3) ^ 2 + ($2 - $4) ^ 2 + e2 = ($3 - $5) ^ 2 + ($4 - $6) ^ 2 + e3 = ($5 - $7) ^ 2 + ($6 - $8) ^ 2 + e4 = ($7 - $1) ^ 2 + ($8 - $2) ^ 2 + d1 = ($1 - $5) ^ 2 + ($2 - $6) ^ 2 + d2 = ($3 - $7) ^ 2 + ($4 - $8) ^ 2 + + print ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) ? 1 : 0) +} diff --git a/challenge-123/abigail/bash/ch-1.sh b/challenge-123/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..af24ff46af --- /dev/null +++ b/challenge-123/abigail/bash/ch-1.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +declare -a ugly +ugly[0]=1 +((next_2 = 0)) +((next_3 = 0)) +((next_5 = 0)) +count=1 + +while read n +do while ((count < n)) + do ((c2 = 2 * ${ugly[next_2]})) + ((c3 = 3 * ${ugly[next_3]})) + ((c5 = 5 * ${ugly[next_5]})) + + if ((c2 <= c3 && c2 <= c5)); then ((ugly[count] = c2)); fi + if ((c3 <= c2 && c3 <= c5)); then ((ugly[count] = c3)); fi + if ((c5 <= c3 && c5 <= c2)); then ((ugly[count] = c5)); fi + + if ((2 * ${ugly[next_2]} <= ${ugly[count]})); then ((next_2 ++)); fi + if ((3 * ${ugly[next_3]} <= ${ugly[count]})); then ((next_3 ++)); fi + if ((5 * ${ugly[next_5]} <= ${ugly[count]})); then ((next_5 ++)); fi + + ((count ++)) + done + echo ${ugly[$((n - 1))]} +done diff --git a/challenge-123/abigail/bash/ch-2.sh b/challenge-123/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..fb12779c2e --- /dev/null +++ b/challenge-123/abigail/bash/ch-2.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +while read x1 y1 x2 y2 x3 y3 x4 y4 +do ((e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2)) + ((e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2)) + ((e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2)) + ((e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2)) + ((d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2)) + ((d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2)) + if ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2)) + then echo 1 + else echo 0 + fi +done diff --git a/challenge-123/abigail/bc/ch-2.bc b/challenge-123/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..2ea9169a08 --- /dev/null +++ b/challenge-123/abigail/bc/ch-2.bc @@ -0,0 +1,32 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc < input-file +# +# Input should be terminated with a line starting with a 0 +# + +while (1) { + a = read () + if (a == 0) {break} + b = read () + c = read () + d = read () + e = read () + f = read () + g = read () + h = read () + i = (a - c) ^ 2 + (b - d) ^ 2 + j = (c - e) ^ 2 + (d - f) ^ 2 + k = (e - g) ^ 2 + (f - h) ^ 2 + l = (g - a) ^ 2 + (h - b) ^ 2 + m = (a - e) ^ 2 + (b - f) ^ 2 + n = (c - g) ^ 2 + (d - h) ^ 2 + o = 0 + if (i == j && j == k && k == l && m == n) { + o = 1 + } + o +} diff --git a/challenge-123/abigail/c/ch-1.c b/challenge-123/abigail/c/ch-1.c new file mode 100644 index 0000000000..cc2f8e54d1 --- /dev/null +++ b/challenge-123/abigail/c/ch-1.c @@ -0,0 +1,53 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +typedef long long number; +int main (void) { + int n; + number * ugly = NULL; + size_t count = 0; + size_t next_2, next_3, next_5; + + + while (scanf ("%d", &n) == 1) { + if (n > count) { + if ((ugly = (number *) realloc (ugly, n * sizeof (number))) + == NULL) { + perror ("Realloc failed"); + exit (1); + } + } + if (count == 0) { + ugly [0] = 1; + count = 1; + next_2 = next_3 = next_5 = 0; + } + while (count < n) { + number c2 = 2 * ugly [next_2]; + number c3 = 3 * ugly [next_3]; + number c5 = 5 * ugly [next_5]; + + ugly [count] = c2 < c3 ? c2 < c5 ? c2 : c5 + : c3 < c5 ? c3 : c5; + + if (2 * ugly [next_2] <= ugly [count]) {next_2 ++;} + if (3 * ugly [next_3] <= ugly [count]) {next_3 ++;} + if (5 * ugly [next_5] <= ugly [count]) {next_5 ++;} + + count ++; + } + printf ("%lld\n", ugly [n - 1]); + } + free (ugly); + + return (0); +} diff --git a/challenge-123/abigail/c/ch-2.c b/challenge-123/abigail/c/ch-2.c new file mode 100644 index 0000000000..56f0b615d4 --- /dev/null +++ b/challenge-123/abigail/c/ch-2.c @@ -0,0 +1,29 @@ +# 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 + */ + +int main (void) { + int x1, y1, x2, y2, x3, y3, x4, y4; + + while (scanf ("%d %d %d %d %d %d %d %d", + &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) == 8) { + int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + int e2 = (x2 - x3) * (x2 - x4) + (y2 - y3) * (y2 - y3); + int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); + int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); + int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); + int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); + + printf ("%d\n", e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0); + } + + return (0); +} diff --git a/challenge-123/abigail/go/ch-2.go b/challenge-123/abigail/go/ch-2.go new file mode 100644 index 0000000000..37d275d5dc --- /dev/null +++ b/challenge-123/abigail/go/ch-2.go @@ -0,0 +1,36 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" +) + +func main () { + var x1, y1, x2, y2, x3, y3, x4, y4, e1, e2, e3, e4, d1, d2 int; + for { + var n, err = fmt . Scanf ("%d %d %d %d %d %d %d %d", + &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); + if (err != nil || n != 8) { + break; + } + e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); + e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); + e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); + d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); + d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); + + if (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) { + fmt . Printf ("%d\n", 1); + } else { // Seriously, go, else needs to be cuddled? + fmt . Printf ("%d\n", 0); + } + } +} diff --git a/challenge-123/abigail/java/ch-2.java b/challenge-123/abigail/java/ch-2.java new file mode 100644 index 0000000000..8d13bcf8bf --- /dev/null +++ b/challenge-123/abigail/java/ch-2.java @@ -0,0 +1,35 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + +public class ch2 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNext ()) { + int x1 = scanner . nextInt (); + int y1 = scanner . nextInt (); + int x2 = scanner . nextInt (); + int y2 = scanner . nextInt (); + int x3 = scanner . nextInt (); + int y3 = scanner . nextInt (); + int x4 = scanner . nextInt (); + int y4 = scanner . nextInt (); + + int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + int e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); + int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); + int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); + int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); + int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); + + System . out . println (e1 == e2 && e2 == e3 && + e3 == e4 && d1 == d2 ? 1 : 0); + } + } +} diff --git a/challenge-123/abigail/lua/ch-1.lua b/challenge-123/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..4feb237189 --- /dev/null +++ b/challenge-123/abigail/lua/ch-1.lua @@ -0,0 +1,48 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local ugly = {} +ugly [1] = 1 +local next_2 = 1 +local next_3 = 1 +local next_5 = 1 +local count = 1 + +for n in io . lines () do + n = tonumber (n) + while count < n do + local c2 = 2 * ugly [next_2] + local c3 = 3 * ugly [next_3] + local c5 = 5 * ugly [next_5] + + count = count + 1 + + if c2 <= c3 and c2 <= c5 then + ugly [count] = c2 + end + if c3 <= c2 and c3 <= c5 then + ugly [count] = c3 + end + if c5 <= c2 and c5 <= c3 then + ugly [count] = c5 + end + + if 2 * ugly [next_2] <= ugly [count] then + next_2 = next_2 + 1 + end + if 3 * ugly [next_3] <= ugly [count] then + next_3 = next_3 + 1 + end + if 5 * ugly [next_5] <= ugly [count] then + next_5 = next_5 + 1 + end + end + print (ugly [n]) +end diff --git a/challenge-123/abigail/lua/ch-2.lua b/challenge-123/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..b379f3078a --- /dev/null +++ b/challenge-123/abigail/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local pat = "(-?%d+)"; +for _ = 2, 8 do + pat = pat .. "%s+(-?%d+)"; +end + + +for line in io . lines () do + _, _, x1, y1, x2, y2, x3, y3, x4, y4 = line : find (pat) + local e1 = (x1 - x2) ^ 2 + (y1 - y2) ^ 2 + local e2 = (x2 - x3) ^ 2 + (y2 - y3) ^ 2 + local e3 = (x3 - x4) ^ 2 + (y3 - y4) ^ 2 + local e4 = (x4 - x1) ^ 2 + (y4 - y1) ^ 2 + local d1 = (x1 - x3) ^ 2 + (y1 - y3) ^ 2 + local d2 = (x2 - x4) ^ 2 + (y2 - y4) ^ 2 + if e1 == e2 and e2 == e3 and e3 == e4 and d1 == d2 then + print (1) + else + print (0) + end +end diff --git a/challenge-123/abigail/node/ch-1.js b/challenge-123/abigail/node/ch-1.js new file mode 100644 index 0000000000..18f9d98e99 --- /dev/null +++ b/challenge-123/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 ugly = [1] +let next_2 = 0 +let next_3 = 0 +let next_5 = 0 + + require ('readline') +. createInterface ({input: proce |
