diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-28 17:10:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-28 17:10:30 +0100 |
| commit | 14082661b3c03381d5c832bbcb8dbe051864b40c (patch) | |
| tree | 6e261f12ba548ae6f2286537394b4128b44d43d4 | |
| parent | 9ef6fa4fd3ef943410209e98ba99b84921da987b (diff) | |
| parent | bd9797e1d52dc5b0853bc5400f04f477ffd6a4cb (diff) | |
| download | perlweeklychallenge-club-14082661b3c03381d5c832bbcb8dbe051864b40c.tar.gz perlweeklychallenge-club-14082661b3c03381d5c832bbcb8dbe051864b40c.tar.bz2 perlweeklychallenge-club-14082661b3c03381d5c832bbcb8dbe051864b40c.zip | |
Merge pull request #4623 from Abigail/abigail/week-123
Abigail/week 123
32 files changed, 873 insertions, 53 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: process . stdin}) +. on ('line', n => { + n =+ n + while (ugly . length < n) { + ugly . push (Math . min (2 * ugly [next_2], + 3 * ugly [next_3], + 5 * ugly [next_5])) + + if (2 * ugly [next_2] <= ugly [ugly . length - 1]) {next_2 ++} + if (3 * ugly [next_3] <= ugly [ugly . length - 1]) {next_3 ++} + if (5 * ugly [next_5] <= ugly [ugly . length - 1]) {next_5 ++} + } + console . log (ugly [n - 1]) +}) diff --git a/challenge-123/abigail/node/ch-2.js b/challenge-123/abigail/node/ch-2.js new file mode 100644 index 0000000000..85ec5ce186 --- /dev/null +++ b/challenge-123/abigail/node/ch-2.js @@ -0,0 +1,22 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [x1, y1, x2, y2, x3, y3, x4, y4] = line . split (/ +/) . map (_ => +_) + let e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2 + let e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2 + let e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2 + let e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2 + let d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2 + let d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2 + console . log (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0) +}) diff --git a/challenge-123/abigail/pascal/ch-2.p b/challenge-123/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..d71fa1c2f0 --- /dev/null +++ b/challenge-123/abigail/pascal/ch-2.p @@ -0,0 +1,31 @@ +Program IsSquare; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +var + x1, y1, x2, y2, x3, y3, x4, y4: integer; + e1, e2, e3, e4, d1, d2: integer; + +begin + while not eof () do begin + readln (x1, y1, x2, y2, x3, y3, x4, y4); + 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) and (e2 = e3) and (e3 = e4) and (d1 = d2) then begin + writeln (1); + end + else begin + writeln (0); + end + end +end. diff --git a/challenge-123/abigail/perl/ch-1.pl b/challenge-123/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..83fb0e30ec --- /dev/null +++ b/challenge-123/abigail/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/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 +# + +use List::Util qw [min]; +my @ugly = (1); +my @primes = (2, 3, 5); +my %next = map {$_ => 0} @primes; + +# +# We will maintain the following invariants: +# +# Foreach $p in @primes: +# $p * $ugly [$next {$p} - 1] <= $ugly [-1] < $p * $ugly [$next {$p}] +# +# And since every ugly number (except the first) is either twice an +# ugly number, three times an ugly number, or five times an ugly +# number, the next ugly number will be the minimum of +# (2 * $ugly [$next {2}], 3 * $ugly [$next {3}], 5 * $ugly [$next {5}]). +# +# We will spend O(1) time per generated ugly number, so our +# program will run in O(N) time, using O(N) memory. +# + +while (my $n = <>) { + while (@ugly < $n) { + # + # Calculate the next ugly number. + # + push @ugly => min map {$_ * $ugly [$next {$_}]} @primes; + + # + # Update pointers. It could be that more than one pointer needs + # updating. (This happens if the ugly number generated is + # divisible by 6, 10, 15, or 30). No pointer ever needs updating twice. + # + $_ * $ugly [$next {$_}] <= $ugly [-1] and $next {$_} ++ for @primes; + } + say $ugly [-1]; +} + + +__END__ diff --git a/challenge-123/abigail/perl/ch-2.pl b/challenge-123/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..7803ed57b0 --- /dev/null +++ b/challenge-123/abigail/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/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 +# +# If a quadrilateral has four equal sides, it's a rhombus. +# A rhombus in which the diagonals are equal is a square. +# +# So we check whether all edges are equal, and whether both diagonals are equal. +# + +while (<>) { + my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = split; + say + ($x1 - $x2) ** 2 + ($y1 - $y2) ** 2 == + ($x2 - $x3) ** 2 + ($y2 - $y3) ** 2 == + ($x3 - $x4) ** 2 + ($y3 - $y4) ** 2 == + ($x4 - $x1) ** 2 + ($y4 - $y1) ** 2 && + ($x1 - $x3) ** 2 + ($y1 - $y3) ** 2 == + ($x2 - $x4) ** 2 + ($y2 - $y4) ** 2 ? 1 : 0 +} diff --git a/challenge-123/abigail/python/ch-1.py b/challenge-123/abigail/python/ch-1.py new file mode 100644 index 0000000000..039d236609 --- /dev/null +++ b/challenge-123/abigail/python/ch-1.py @@ -0,0 +1,33 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +ugly = [1] +next_2 = 0 +next_3 = 0 +next_5 = 0 + + +for n in fileinput . input (): + n = int (n) + while len (ugly) < n: + ugly . append (min ([2 * ugly [next_2], + 3 * ugly [next_3], + 5 * ugly [next_5]])) + + if 2 * ugly [next_2] <= ugly [-1]: + next_2 = next_2 + 1 + if 3 * ugly [next_3] <= ugly [-1]: + next_3 = next_3 + 1 + if 5 * ugly [next_5] <= ugly [-1]: + next_5 = next_5 + 1 + + print (ugly [n - 1]) diff --git a/challenge-123/abigail/python/ch-2.py b/challenge-123/abigail/python/ch-2.py new file mode 100644 index 0000000000..e61a2cc0c5 --- /dev/null +++ b/challenge-123/abigail/python/ch-2.py @@ -0,0 +1,25 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + [x1, y1, x2, y2, x3, y3, x4, y4] = \ + map (lambda x: int (x), line . split (' ')) + if (x1 - x2) ** 2 + (y1 - y2) ** 2 == \ + (x2 - x3) ** 2 + (y2 - y3) ** 2 == \ + (x3 - x4) ** 2 + (y3 - y4) ** 2 == \ + (x4 - x1) ** 2 + (y4 - y1) ** 2 and \ + (x1 - x3) ** 2 + (y1 - y3) ** 2 == \ + (x2 - x4) ** 2 + (y2 - y4) ** 2: + print (1) + else: + print (0) + diff --git a/challenge-123/abigail/r/ch-1.r b/challenge-123/abigail/r/ch-1.r new file mode 100644 index 0000000000..208c5b4dde --- /dev/null +++ b/challenge-123/abigail/r/ch-1.r @@ -0,0 +1,33 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file |
