From 2f83bdd6822c7c68c32ec6985b309e7f3612cf18 Mon Sep 17 00:00:00 2001 From: boblied Date: Wed, 3 Nov 2021 09:46:59 -0500 Subject: Challenge 1 solution, nested loops --- challenge-003/bob-lied/perl/ch-1.pl | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 challenge-003/bob-lied/perl/ch-1.pl (limited to 'challenge-003') diff --git a/challenge-003/bob-lied/perl/ch-1.pl b/challenge-003/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..024fbed8f1 --- /dev/null +++ b/challenge-003/bob-lied/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly challenge Week 3, Challenge #1 +# Create a script to generate 5-smooth numbers, whose prime divisors are less +# or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +# information, please check this wikipedia. +# "... numbers are called 5-smooth, because they can be characterized as +# having only 2, 3, or 5 as prime factors. ..." +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +my $Max = shift; +$Max //= 100; + +sub fiveSmooth($max) +{ + my @smoothNumbers; + for ( my $mult2 = 1; $mult2 <= $max ; $mult2 *= 2 ) + { + for ( my $mult3 = $mult2 ; $mult3 <= $max ; $mult3 *= 3 ) + { + for ( my $mult5 = $mult3 ; $mult5 <= $max ; $mult5 *= 5 ) + { + push @smoothNumbers, $mult5 unless $mult5 == 1; # Not in order + } + } + } + return \@smoothNumbers; +} +exit(!runTest()) if $DoTest; +my $smoothList = fiveSmooth($Max); +say $_ foreach sort { $a <=> $b} @$smoothList; + +sub runTest +{ + use Test::More; + + is_deeply( fiveSmooth( 2), [ 2 ], "max = 2"); + is_deeply( fiveSmooth( 3), [ 2, 3 ], "max = 3"); + is_deeply( fiveSmooth( 5), [ 2, 3, 4, 5 ], "max = 5"); + is_deeply( fiveSmooth(10), [ 2, 3, 4, 5, 6, 8, 9, 10 ], "max = 10"); + is_deeply( fiveSmooth(20), [ 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20 ], "max = 20"); + + done_testing; +} + -- cgit From 17fd72285fae20e3f79d53225cf5336394c4f69b Mon Sep 17 00:00:00 2001 From: boblied Date: Sun, 14 Nov 2021 19:27:38 -0600 Subject: PWC 003 Task #2 --- challenge-003/bob-lied/perl/ch-2.pl | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 challenge-003/bob-lied/perl/ch-2.pl (limited to 'challenge-003') diff --git a/challenge-003/bob-lied/perl/ch-2.pl b/challenge-003/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..c30ed80b37 --- /dev/null +++ b/challenge-003/bob-lied/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge Week 3, Challenge 2 +# Create a script that generates Pascal Triangle. Accept number of rows from +# the command line. The Pascal Triangle should have at least 3 rows. For more +# information about Pascal Triangle, check this wikipedia page. +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +my $NumRows = shift; +die "Usage: $0 N, where N >= 1" unless $NumRows > 0; + +sub pascalTriangle($n) +{ + say "1"; + my $prevRow = [ 1, 1 ]; + say "@$prevRow" if $n >= 2; + + while ( --$n > 1 ) + { + my $nextRow = [ 1 ]; + for ( my $c = 0 ; $c < scalar(@$prevRow)-1; $c++ ) + { + push @$nextRow, $prevRow->[$c] + $prevRow->[$c+1]; + } + push @$nextRow, 1; + say "@$nextRow"; + $prevRow = $nextRow; + } + +} + +pascalTriangle($NumRows); -- cgit From ada8b466f3fe8efcf3b85f22495c744517df42ca Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 5 Jan 2022 20:35:32 +0100 Subject: Week 3: copied solutions from week 123. Week 123 has exactly the same challenge. Since I prefer that solution, I copied those results. Tests adjusted accordingly. --- challenge-003/abigail/README.md | 32 +--- challenge-003/abigail/awk/ch-1.awk | 44 +++-- challenge-003/abigail/bash/ch-1.sh | 32 +++- challenge-003/abigail/c/ch-1.c | 48 +++-- challenge-003/abigail/lua/ch-1.lua | 54 ++++-- challenge-003/abigail/node/ch-1.js | 29 +-- challenge-003/abigail/perl/ch-1.pl | 55 ++++-- challenge-003/abigail/python/ch-1.py | 40 +++-- challenge-003/abigail/r/ch-1.r | 33 ++++ challenge-003/abigail/ruby/ch-1.rb | 35 ++-- challenge-003/abigail/t/ctest.ini | 8 +- challenge-003/abigail/t/input-1-1 | 3 +- challenge-003/abigail/t/input-1-2 | 5 +- challenge-003/abigail/t/output-1-1.exp | 60 ------- challenge-003/abigail/t/output-1-2.exp | 313 +-------------------------------- 15 files changed, 269 insertions(+), 522 deletions(-) create mode 100644 challenge-003/abigail/r/ch-1.r (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 586b7ee14e..365945625a 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -1,25 +1,6 @@ -# Solution by Abigail +# Solution by Abigail, week 3 -## [Challenge #1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/#challenge-1) -Create a script to generate `5`-smooth numbers, whose prime divisors -are less or equal to `5`. They are also called Hamming/Regular/Ugly -numbers. For more information, please check this -[wikipedia](https://en.wikipedia.org/wiki/Regular_number). - -### Notes -We are going to generate all numbers `n = 2^i * 3^j * 5^k`, with -`i >= 0`, `j >= 0`, `k >= 0`, and `n <= MAX`, where `MAX` is read -from `STDIN`. No other numbers are generated. - -We are *not* going to generate the numbers `n` in -numerical order. Instead, if we have two numbers `n1 = 2^i1 * 3^j1 * 5^k1`, -and `n2 = 2^i2 * 3^j2 * 5^k2`, then `n1` is generated before `n2`, iff - - i1 < i2 || - i1 == i2 && j1 < j2 || - i1 == i2 && j1 == j2 && k1 < k2 - -### Solutions +### Part 1 * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) @@ -29,15 +10,10 @@ and `n2 = 2^i2 * 3^j2 * 5^k2`, then `n1` is generated before `n2`, iff * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [Ruby](ruby/ch-1.rb) +* [R](r/ch-1.r) -## [Challenge #2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/#challenge-2) -Create a script that generates Pascal Triangle. Accept number of -rows from the command line. The Pascal Triangle should have at least -3 rows. For more information about Pascal Triangle, check this -[wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle) page. - -### Solutions +### Part 2 * [AWK](awk/ch-2.awk) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) diff --git a/challenge-003/abigail/awk/ch-1.awk b/challenge-003/abigail/awk/ch-1.awk index 706014ed91..c6fd5a3e55 100644 --- a/challenge-003/abigail/awk/ch-1.awk +++ b/challenge-003/abigail/awk/ch-1.awk @@ -1,29 +1,39 @@ #!/usr/bin/awk # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # # Run as: awk -f ch-1.awk < input-file # -# -# Generate the 5-smooth numbers up to $0. -# This does *NOT* generate the numbers is order. It does, however, -# generate all of them, and no other numbers. -# -# -# base2 is of the form 2^i; i >= 0 -# base3 if of the form 2^i * 3^j; i, j >= 0 -# base5 is of the form 2^i * 3^j * 5^k; i, j, k >= 0 -# +BEGIN { + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + count = 1 +} + { - for (base2 = 1; base2 <= $0; base2 *= 2) { - for (base3 = base2; base3 <= $0; base3 *= 3) { - for (base5 = base3; base5 <= $0; base5 *= 5) { - print base5 - } - } + 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-003/abigail/bash/ch-1.sh b/challenge-003/abigail/bash/ch-1.sh index 76029e700b..b2f98abec9 100644 --- a/challenge-003/abigail/bash/ch-1.sh +++ b/challenge-003/abigail/bash/ch-1.sh @@ -1,7 +1,7 @@ #!/bin/sh # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # @@ -10,12 +10,28 @@ set -f -while read max -do for ((base2 = 1; $base2 <= $max; base2 *= 2)) - do for ((base3 = $base2; $base3 <= $max; base3 *= 3)) - do for ((base5 = $base3; $base5 <= $max; base5 *= 5)) - do echo $base5 - done - done +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-003/abigail/c/ch-1.c b/challenge-003/abigail/c/ch-1.c index eb6a053705..a5a503cb32 100644 --- a/challenge-003/abigail/c/ch-1.c +++ b/challenge-003/abigail/c/ch-1.c @@ -3,29 +3,51 @@ # include /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 */ /* * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file */ +typedef long long number; int main (void) { - char * line = NULL; - size_t len = 0; - size_t strlen; - - while ((strlen = getline (&line, &len, stdin)) != -1) { - long long max = atoll (line); - for (long long base2 = 1; base2 <= max; base2 *= 2) { - for (long long base3 = base2; base3 <= max; base3 *= 3) { - for (long long base5 = base3; base5 <= max; base5 *= 5) { - printf ("%lld\n", base5); - } + 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 (line); + free (ugly); return (0); } diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua index 70de9b1923..3f0bf5b9fe 100644 --- a/challenge-003/abigail/lua/ch-1.lua +++ b/challenge-003/abigail/lua/ch-1.lua @@ -1,30 +1,48 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- -- Run as: lua ch-1.lua < input-file -- -for max in io . lines () do - local max = tonumber (max) - local base2 = 1 - -- - -- Lua doesn't have a for (expr; expr; expr) syntax. - -- This is missed here. - -- - while base2 <= max do - local base3 = base2 - while base3 <= max do - local base5 = base3 - while base5 <= max do - print (base5) - base5 = base5 * 5 - end - base3 = base3 * 3 +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 - base2 = base2 * 2 end + print (ugly [n]) end diff --git a/challenge-003/abigail/node/ch-1.js b/challenge-003/abigail/node/ch-1.js index a1ffe969ac..e44318f34c 100644 --- a/challenge-003/abigail/node/ch-1.js +++ b/challenge-003/abigail/node/ch-1.js @@ -1,21 +1,30 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 // // // Run as: node ch-1.js < input-file // -require ('readline') +let ugly = [1] +let next_2 = 0 +let next_3 = 0 +let next_5 = 0 + + require ('readline') . createInterface ({input: process . stdin}) -. on ('line', max => { - for (let base2 = 1; base2 <= max; base2 *= 2) { - for (let base3 = base2; base3 <= max; base3 *= 3) { - for (let base5 = base3; base5 <= max; base5 *= 5) { - console . log (base5) - } - } +. 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-003/abigail/perl/ch-1.pl b/challenge-003/abigail/perl/ch-1.pl index 352466e41c..2b9787b3c9 100644 --- a/challenge-003/abigail/perl/ch-1.pl +++ b/challenge-003/abigail/perl/ch-1.pl @@ -10,32 +10,55 @@ use experimental 'signatures'; use experimental 'lexical_subs'; # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # # Run as: perl ch-1.pl < input-file # -# -# Read the maximum number from STDIN -# -chomp (my $MAX = <>); +use List::Util qw [min]; +my @ugly = (1); +my $next_2 = 0; +my $next_3 = 0; +my $next_5 = 0; # -# Generate the 5-smooth numbers up to $MAX. -# This does *NOT* generate the numbers is order. It does, however, -# generate all of them, and no other numbers. +# We will maintain the following invariants: # +# 2 * $ugly [$next_2 - 1] <= $ugly [-1] < 2 * $ugly [$next_2] +# 3 * $ugly [$next_3 - 1] <= $ugly [-1] < 3 * $ugly [$next_2] +# 5 * $ugly [$next_5 - 1] <= $ugly [-1] < 5 * $ugly [$next_2] # -# $base2 is of the form 2^i; i >= 0 -# $base3 if of the form 2^i * 3^j; i, j >= 0 -# $base5 is of the form 2^i * 3^j * 5^k; i, j, k >= 0 +# 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]). # -for (my $base2 = 1; $base2 <= $MAX; $base2 *= 2) { - for (my $base3 = $base2; $base3 <= $MAX; $base3 *= 3) { - for (my $base5 = $base3; $base5 <= $MAX; $base5 *= 5) { - say $base5; - } +# 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 2 * $ugly [$next_2], + 3 * $ugly [$next_3], + 5 * $ugly [$next_5]; + + # + # 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. + # + $next_2 ++ if 2 * $ugly [$next_2] <= $ugly [-1]; + $next_3 ++ if 3 * $ugly [$next_3] <= $ugly [-1]; + $next_5 ++ if 5 * $ugly [$next_5] <= $ugly [-1]; } + say $ugly [-1]; } + + +__END__ diff --git a/challenge-003/abigail/python/ch-1.py b/challenge-003/abigail/python/ch-1.py index 30b41dd8fe..8b3ac1e035 100644 --- a/challenge-003/abigail/python/ch-1.py +++ b/challenge-003/abigail/python/ch-1.py @@ -1,27 +1,33 @@ #!/opt/local/bin/python # -# See ../READ.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # -# Run as python ch-1.py < input-file +# Run as: python ch-1.py < input-file # import fileinput -for line in fileinput . input (): - max = int (line) - # - # Python does not have a for (;;) style loop - # - base2 = 1 - while base2 <= max: - base3 = base2 - while base3 <= max: - base5 = base3 - while base5 <= max: - print base5 - base5 *= 5 - base3 *= 3 - base2 *= 2 +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-003/abigail/r/ch-1.r b/challenge-003/abigail/r/ch-1.r new file mode 100644 index 0000000000..208c5b4dde --- /dev/null +++ b/challenge-003/abigail/r/ch-1.r @@ -0,0 +1,33 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +ugly <- c (1) +next_2 <- 1 +next_3 <- 1 +next_5 <- 1 +count <- 1 + +stdin <- file ('stdin', 'r') +repeat { + n <- readLines (stdin, n = 1) + if (length (n) == 0) { + break + } + n = as.integer (n) + + while (count < n) { + count = count + 1 + ugly [count] = min (c (2 * ugly [next_2], 3 * ugly [next_3], + 5 * ugly [next_5])) + + if (2 * ugly [next_2] <= ugly [count]) {next_2 <- next_2 + 1} + if (3 * ugly [next_3] <= ugly [count]) {next_3 <- next_3 + 1} + if (5 * ugly [next_5] <= ugly [count]) {next_5 <- next_5 + 1} + } + cat (format (ugly [n], digits = 15), "\n") +} diff --git a/challenge-003/abigail/ruby/ch-1.rb b/challenge-003/abigail/ruby/ch-1.rb index c0a22397b0..d07d858bda 100644 --- a/challenge-003/abigail/ruby/ch-1.rb +++ b/challenge-003/abigail/ruby/ch-1.rb @@ -1,26 +1,29 @@ #!/usr/bin/ruby # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # # Run as: ruby ch-1.rb < input-file # -ARGF . each_line do |_| - max = _ . to_i - base2 = 1 - while base2 <= max - base3 = base2 - while base3 <= max - base5 = base3 - while base5 <= max - puts base5 - base5 *= 5 - end - base3 *= 3 - end - base2 *= 2 - end +ugly = [1] +next_2 = 0 +next_3 = 0 +next_5 = 0 + +ARGF . each_line do + |n| + n = n . to_i + while ugly . length < n do + ugly . push ([2 * ugly [next_2], + 3 * ugly [next_3], + 5 * ugly [next_5]] . min) + + next_2 += 1 if 2 * ugly [next_2] <= ugly [-1] + next_3 += 1 if 3 * ugly [next_3] <= ugly [-1] + next_5 += 1 if 5 * ugly [next_5] <= ugly [-1] + end + puts (ugly [n - 1]) end diff --git a/challenge-003/abigail/t/ctest.ini b/challenge-003/abigail/t/ctest.ini index 959e85ac42..a3434f63d2 100644 --- a/challenge-003/abigail/t/ctest.ini +++ b/challenge-003/abigail/t/ctest.ini @@ -4,11 +4,7 @@ # [names] -1-1 = OEIS Example -1-2 = Large input +1-1 = Given Examples from week 123 +1-2 = Larger Numbers 2-1 = Minimal triangle 2-2 = 15 row triangle - - -[1-1,1-2] -sort = numeric diff --git a/challenge-003/abigail/t/input-1-1 b/challenge-003/abigail/t/input-1-1 index ec8785ec99..1831e2e66c 100644 --- a/challenge-003/abigail/t/input-1-1 +++ b/challenge-003/abigail/t/input-1-1 @@ -1 +1,2 @@ -405 +7 +10 diff --git a/challenge-003/abigail/t/input-1-2 b/challenge-003/abigail/t/input-1-2 index f7393e847d..d59e6110af 100644 --- a/challenge-003/abigail/t/input-1-2 +++ b/challenge-003/abigail/t/input-1-2 @@ -1 +1,4 @@ -100000 +10 +100 +1000 +5000 diff --git a/challenge-003/abigail/t/output-1-1.exp b/challenge-003/abigail/t/output-1-1.exp index 04fd1a4f4b..60d24f7251 100644 --- a/challenge-003/abigail/t/output-1-1.exp +++ b/challenge-003/abigail/t/output-1-1.exp @@ -1,62 +1,2 @@ -1 -2 -3 -4 -5 -6 8 -9 -10 12 -15 -16 -18 -20 -24 -25 -27 -30 -32 -36 -40 -45 -48 -50 -54 -60 -64 -72 -75 -80 -81 -90 -96 -100 -108 -120 -125 -128 -135 -144 -150 -160 -162 -180 -192 -200 -216 -225 -240 -243 -250 -256 -270 -288 -300 -320 -324 -360 -375 -384 -400 -405 diff --git a/challenge-003/abigail/t/output-1-2.exp b/challenge-003/abigail/t/output-1-2.exp index 69a24cd064..8ba1659605 100644 --- a/challenge-003/abigail/t/output-1-2.exp +++ b/challenge-003/abigail/t/output-1-2.exp @@ -1,313 +1,4 @@ -1 -2 -3 -4 -5 -6 -8 -9 -10 12 -15 -16 -18 -20 -24 -25 -27 -30 -32 -36 -40 -45 -48 -50 -54 -60 -64 -72 -75 -80 -81 -90 -96 -100 -108 -120 -125 -128 -135 -144 -150 -160 -162 -180 -192 -200 -216 -225 -240 -243 -250 -256 -270 -288 -300 -320 -324 -360 -375 -384 -400 -405 -432 -450 -480 -486 -500 -512 -540 -576 -600 -625 -640 -648 -675 -720 -729 -750 -768 -800 -810 -864 -900 -960 -972 -1000 -1024 -1080 -1125 -1152 -1200 -1215 -1250 -1280 -1296 -1350 -1440 -1458 -1500 1536 -1600 -1620 -1728 -1800 -1875 -1920 -1944 -2000 -2025 -2048 -2160 -2187 -2250 -2304 -2400 -2430 -2500 -2560 -2592 -2700 -2880 -2916 -3000 -3072 -3125 -3200 -3240 -3375 -3456 -3600 -3645 -3750 -3840 -3888 -4000 -4050 -4096 -4320 -4374 -4500 -4608 -4800 -4860 -5000 -5120 -5184 -5400 -5625 -5760 -5832 -6000 -6075 -6144 -6250 -6400 -6480 -6561 -6750 -6912 -7200 -7290 -7500 -7680 -7776 -8000 -8100 -8192 -8640 -8748 -9000 -9216 -9375 -9600 -9720 -10000 -10125 -10240 -10368 -10800 -10935 -11250 -11520 -11664 -12000 -12150 -12288 -12500 -12800 -12960 -13122 -13500 -13824 -14400 -14580 -15000 -15360 -15552 -15625 -16000 -16200 -16384 -16875 -17280 -17496 -18000 -18225 -18432 -18750 -19200 -19440 -19683 -20000 -20250 -20480 -20736 -21600 -21870 -22500 -23040 -23328 -24000 -24300 -24576 -25000 -25600 -25920 -26244 -27000 -27648 -28125 -28800 -29160 -30000 -30375 -30720 -31104 -31250 -32000 -32400 -32768 -32805 -33750 -34560 -34992 -36000 -36450 -36864 -37500 -38400 -38880 -39366 -40000 -40500 -40960 -41472 -43200 -43740 -45000 -46080 -46656 -46875 -48000 -48600 -49152 -50000 -50625 -51200 -51840 -52488 -54000 -54675 -55296 -56250 -57600 -58320 -59049 -60000 -60750 -61440 -62208 -62500 -64000 -64800 -65536 -65610 -67500 -69120 -69984 -72000 -72900 -73728 -75000 -76800 -77760 -78125 -78732 -80000 -81000 -81920 -82944 -84375 -86400 -87480 -90000 -91125 -92160 -93312 -93750 -96000 -97200 -98304 -98415 -100000 +51200000 +50837316566580 -- cgit From bbc66927a06a44a3e4c4223ef4d159b597734a5b Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 5 Jan 2022 21:01:57 +0100 Subject: Week 3, part 1: different algorithm for bc solution. --- challenge-003/abigail/bc/ch-1.bc | 39 +++++++++++++++++++++++++++++++++------ challenge-003/abigail/t/ctest.ini | 3 +++ 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'challenge-003') diff --git a/challenge-003/abigail/bc/ch-1.bc b/challenge-003/abigail/bc/ch-1.bc index d3336b509d..bbbbdf3a2e 100644 --- a/challenge-003/abigail/bc/ch-1.bc +++ b/challenge-003/abigail/bc/ch-1.bc @@ -1,8 +1,35 @@ -max = read () -for (base2 = 1; base2 <= max; base2 *= 2) { - for (base3 = base2; base3 <= max; base3 *= 3) { - for (base5 = base3; base5 <= max; base5 *= 5) { - base5 - } +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bc ch-1.bc < input-file +# + +while (1) { + n = read () + if (n == 0) { + break } + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + index = 1 + while (index < n) { + u2 = 2 * ugly [next_2] + u3 = 3 * ugly [next_3] + u5 = 5 * ugly [next_5] + if ((u2 <= u3) && (u2 <= u5)) {min = u2} + if ((u3 <= u2) && (u3 <= u5)) {min = u3} + if ((u5 <= u2) && (u5 <= u3)) {min = u5} + ugly [index] = min + if (2 * ugly [next_2] <= ugly [index]) {next_2 = next_2 + 1} + if (3 * ugly [next_3] <= ugly [index]) {next_3 = next_3 + 1} + if (5 * ugly [next_5] <= ugly [index]) {next_5 = next_5 + 1} + index = index + 1 + } + ugly [index - 1] } diff --git a/challenge-003/abigail/t/ctest.ini b/challenge-003/abigail/t/ctest.ini index a3434f63d2..71b4641536 100644 --- a/challenge-003/abigail/t/ctest.ini +++ b/challenge-003/abigail/t/ctest.ini @@ -8,3 +8,6 @@ 1-2 = Larger Numbers 2-1 = Minimal triangle 2-2 = 15 row triangle + +[1-1,1-2/bc] +add_to_input = 0 -- cgit From e22456cd4100b9d24449a0a1623236f0b5ecb412 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 01:47:00 +0100 Subject: Week 3, part 2: fix references --- challenge-003/abigail/awk/ch-2.awk | 2 +- challenge-003/abigail/c/ch-2.c | 2 +- challenge-003/abigail/lua/ch-2.lua | 2 +- challenge-003/abigail/node/ch-2.js | 2 +- challenge-003/abigail/perl/ch-2.pl | 2 +- challenge-003/abigail/python/ch-2.py | 2 +- challenge-003/abigail/ruby/ch-2.rb | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) (limited to 'challenge-003') diff --git a/challenge-003/abigail/awk/ch-2.awk b/challenge-003/abigail/awk/ch-2.awk index 1689ce668f..1079aaab62 100644 --- a/challenge-003/abigail/awk/ch-2.awk +++ b/challenge-003/abigail/awk/ch-2.awk @@ -1,7 +1,7 @@ #!/usr/bin/awk # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # diff --git a/challenge-003/abigail/c/ch-2.c b/challenge-003/abigail/c/ch-2.c index 805f0d93be..7dee8c4eff 100644 --- a/challenge-003/abigail/c/ch-2.c +++ b/challenge-003/abigail/c/ch-2.c @@ -3,7 +3,7 @@ # include /* - * See ../README.md + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 */ /* diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua index 0e30b0a9c6..aed77c8e32 100644 --- a/challenge-003/abigail/lua/ch-2.lua +++ b/challenge-003/abigail/lua/ch-2.lua @@ -1,7 +1,7 @@ #!/opt/local/bin/lua -- --- See ../README.md +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 -- -- diff --git a/challenge-003/abigail/node/ch-2.js b/challenge-003/abigail/node/ch-2.js index af1724bb18..a161017ede 100644 --- a/challenge-003/abigail/node/ch-2.js +++ b/challenge-003/abigail/node/ch-2.js @@ -1,7 +1,7 @@ #!/usr/local/bin/node // -// See ../README.md +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 // // diff --git a/challenge-003/abigail/perl/ch-2.pl b/challenge-003/abigail/perl/ch-2.pl index d5c14c4701..6d107b1e2d 100644 --- a/challenge-003/abigail/perl/ch-2.pl +++ b/challenge-003/abigail/perl/ch-2.pl @@ -10,7 +10,7 @@ use experimental 'signatures'; use experimental 'lexical_subs'; # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # diff --git a/challenge-003/abigail/python/ch-2.py b/challenge-003/abigail/python/ch-2.py index 9370561d8b..3518b91c8e 100644 --- a/challenge-003/abigail/python/ch-2.py +++ b/challenge-003/abigail/python/ch-2.py @@ -1,7 +1,7 @@ #!/opt/local/bin/python # -# See ../READ.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # diff --git a/challenge-003/abigail/ruby/ch-2.rb b/challenge-003/abigail/ruby/ch-2.rb index 85736b99cb..1712bedc83 100644 --- a/challenge-003/abigail/ruby/ch-2.rb +++ b/challenge-003/abigail/ruby/ch-2.rb @@ -1,7 +1,7 @@ #!/usr/bin/ruby # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # -- cgit From 76e71dbba093a8fa883b945df09e22d4b5b94ae2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 13:56:30 +0100 Subject: Week 3, part 2: bash solution --- challenge-003/abigail/bash/ch-2.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-003/abigail/bash/ch-2.sh (limited to 'challenge-003') diff --git a/challenge-003/abigail/bash/ch-2.sh b/challenge-003/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..ca5e5fb510 --- /dev/null +++ b/challenge-003/abigail/bash/ch-2.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -A p + +while read n +do for ((row = 0; row <= n; row ++)) + do p["$row;0"]=1 + printf "1 " + for ((col = 1; col <= row; col ++)) + do p["$row;$col"]=$((${p["$((row-1));$((col-1))"]:-0} + \ + ${p["$((row-1));$col"]:-0})) + printf "%d " ${p["$row;$col"]} + done + echo + done +done -- cgit From 27889f392ae8b7b56530ad79424eb9054a249275 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 13:59:18 +0100 Subject: Copied bc solution of week 3, part 1 --- challenge-003/abigail/README.md | 1 + 1 file changed, 1 insertion(+) (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 365945625a..83d08b3cd7 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -15,6 +15,7 @@ ### Part 2 * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) -- cgit From bccd1e3a8efb165516051edab359b83b4b86cf03 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 15:33:59 +0100 Subject: Week 3 & Week 123. Go solutions for part 1. --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/go/ch-1.go | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-003/abigail/go/ch-1.go (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 83d08b3cd7..742ccb1714 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -5,6 +5,7 @@ * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) * [C](c/ch-1.c) +* [Go](go/ch-1.go) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-003/abigail/go/ch-1.go b/challenge-003/abigail/go/ch-1.go new file mode 100644 index 0000000000..03229cb964 --- /dev/null +++ b/challenge-003/abigail/go/ch-1.go @@ -0,0 +1,47 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: go run ch-1.go < input-file +// + +import ( + "fmt" +) + +func main () { + var max int + for { + n, err := fmt . Scanf ("%d", &max) + if (n != 1 || err != nil) { + break + } + ugly := make ([] int, max) + ugly [0] = 1 + count := 0 + next_2 := 0 + next_3 := 0 + next_5 := 0 + min := 0 + + for count < max - 1 { + count ++ + + c2 := 2 * ugly [next_2] + c3 := 3 * ugly [next_3] + c5 := 5 * ugly [next_5] + if c2 <= c3 && c2 <= c5 {min = c2} + if c3 <= c2 && c3 <= c5 {min = c3} + if c5 <= c2 && c5 <= c3 {min = c5} + ugly [count] = min + + if (c2 <= ugly [count]) {next_2 ++} + if (c3 <= ugly [count]) {next_3 ++} + if (c5 <= ugly [count]) {next_5 ++} + } + fmt . Println (ugly [count]) + } +} -- cgit From 117db0cb5639368433cc1d1b683841ff91251665 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 15:35:13 +0100 Subject: Week 3: bc solution for part 2 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/bc/ch-2.bc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-003/abigail/bc/ch-2.bc (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 742ccb1714..3902fc3410 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -17,6 +17,7 @@ ### Part 2 * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) diff --git a/challenge-003/abigail/bc/ch-2.bc b/challenge-003/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..e2c41b0455 --- /dev/null +++ b/challenge-003/abigail/bc/ch-2.bc @@ -0,0 +1,35 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: bc ch-2.bc < input-file +# + +while (1) { + n = read () + if (n == 0) { + break + } + + p [0] = 1 + p [0] + for (i = 1; i <= n; i ++) { + for (j = 0; j < i; j ++) { + n [j] = p [j] + if (j - 1 >= 0) { + n [j] = n [j] + p [j - 1] + } + print n [j], " " + } + n [i] = 1 + print "1 +" + for (j = 0; j <= i; j ++) { + p [j] = n [j] + } + } + break +} -- cgit From bb78b469bdba75ebe82f4f7c9d728281b97d515c Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 16:05:11 +0100 Subject: Week 3 & Week 123: Java solutions for part 1 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/java/ch-1.java | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-003/abigail/java/ch-1.java (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 3902fc3410..5a95fe5f0c 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -6,6 +6,7 @@ * [bc](bc/ch-1.bc) * [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) * [Perl](perl/ch-1.pl) diff --git a/challenge-003/abigail/java/ch-1.java b/challenge-003/abigail/java/ch-1.java new file mode 100644 index 0000000000..651f938314 --- /dev/null +++ b/challenge-003/abigail/java/ch-1.java @@ -0,0 +1,46 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int max = scanner . nextInt (); + long [] ugly = new long [max]; + + ugly [0] = 1L; + int count = 0; + int next_2 = 0; + int next_3 = 0; + int next_5 = 0; + + while (count < max - 1) { + count ++; + long min = 0; + + long c2 = 2 * ugly [next_2]; + long c3 = 3 * ugly [next_3]; + long c5 = 5 * ugly [next_5]; + + if (c2 <= c3 && c2 <= c5) {min = c2;} + if (c3 <= c2 && c3 <= c5) {min = c3;} + if (c5 <= c2 && c5 <= c3) {min = c5;} + + ugly [count] = min; + + if (c2 <= ugly [count]) {next_2 ++;} + if (c3 <= ugly [count]) {next_3 ++;} + if (c5 <= ugly [count]) {next_5 ++;} + } + + System . out . println (ugly [count]); + } + } +} -- cgit From ec311b177fa675877c45ed1518dceff5c787d13b Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 18:22:18 +0100 Subject: Week 3, part 2. Go and Java solutions. --- challenge-003/abigail/README.md | 2 ++ challenge-003/abigail/go/ch-2.go | 40 ++++++++++++++++++++++++++++++++++++ challenge-003/abigail/java/ch-2.java | 34 ++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 challenge-003/abigail/go/ch-2.go create mode 100644 challenge-003/abigail/java/ch-2.java (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 5a95fe5f0c..966649afde 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -20,6 +20,8 @@ * [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) * [Perl](perl/ch-2.pl) diff --git a/challenge-003/abigail/go/ch-2.go b/challenge-003/abigail/go/ch-2.go new file mode 100644 index 0000000000..d66b10dec9 --- /dev/null +++ b/challenge-003/abigail/go/ch-2.go @@ -0,0 +1,40 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" +) + +func main () { + for { + var max int + n, err := fmt . Scanf ("%d", &max) + if n != 1 || err != nil { + break + } + + current_row := make ([] int, 1) + current_row [0] = 1; + fmt . Printf ("1\n") + + for row := 1; row <= max; row ++ { + next_row := make ([] int, row + 1) + next_row [0] = 1 + next_row [row] = 1 + fmt . Printf ("1 ") + for col := 1; col < row; col ++ { + next_row [col] = current_row [col - 1] + current_row [col] + fmt . Printf ("%d ", next_row [col]) + } + fmt . Printf ("1\n") + current_row = next_row + } + } +} diff --git a/challenge-003/abigail/java/ch-2.java b/challenge-003/abigail/java/ch-2.java new file mode 100644 index 0000000000..8f932a81db --- /dev/null +++ b/challenge-003/abigail/java/ch-2.java @@ -0,0 +1,34 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +// + +// +// 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 . hasNextInt ()) { + int max = scanner . nextInt (); + int [] current_row = new int [1]; + current_row [0] = 1; + System . out . println ("1"); + + for (int row = 1; row <= max; row ++) { + int [] next_row = new int [row + 1]; + next_row [0] = 1; + next_row [row] = 1; + System . out . print ("1 "); + for (int col = 1; col < row; col ++) { + next_row [col] = current_row [col - 1] + current_row [col]; + System . out . printf ("%d ", next_row [col]); + } + System . out . println ("1"); + current_row = next_row; + } + } + } +} -- cgit From b97c7e9a60e75f3a5daedbc80fe7d767305b4d53 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 18:41:33 +0100 Subject: Week 3 & Week 123: Pascal solution for part 1 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/pascal/ch-1.p | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 challenge-003/abigail/pascal/ch-1.p (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 966649afde..6d798bf875 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -9,6 +9,7 @@ * [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) * [Ruby](ruby/ch-1.rb) diff --git a/challenge-003/abigail/pascal/ch-1.p b/challenge-003/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..79eec3a1d3 --- /dev/null +++ b/challenge-003/abigail/pascal/ch-1.p @@ -0,0 +1,48 @@ +Program ch1; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + max, count, next_2, next_3, next_5: integer; + min, c2, c3, c5: qword; + ugly: array of qword; + +begin + while not eof do begin + readln (max); + setlength (ugly, max); + + ugly [0] := 1; + count := 0; + next_2 := 0; + next_3 := 0; + next_5 := 0; + min := 0; + + while count < max - 1 do begin + inc (count); + + c2 := 2 * ugly [next_2]; + c3 := 3 * ugly [next_3]; + c5 := 5 * ugly [next_5]; + + if (c2 <= c3) and (c2 <= c5) then begin min := c2; end; + if (c3 <= c2) and (c3 <= c5) then begin min := c3; end; + if (c5 <= c2) and (c5 <= c3) then begin min := c5; end; + + ugly [count] := min; + + if c2 <= ugly [count] then begin inc (next_2); end; + if c3 <= ugly [count] then begin inc (next_3); end; + if c5 <= ugly [count] then begin inc (next_5); end; + end; + + writeln (ugly [count]); + end +end. -- cgit From 5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 19:00:39 +0100 Subject: Week 003 & Week 123: Tcl solution for part 1. --- challenge-003/abigail/README.md | 3 ++- challenge-003/abigail/tcl/ch-1.tcl | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 challenge-003/abigail/tcl/ch-1.tcl (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 6d798bf875..5a50b02ed5 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -12,8 +12,9 @@ * [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) * [R](r/ch-1.r) +* [Ruby](ruby/ch-1.rb) +* [Tcl](tcl/ch-1.tcl) ### Part 2 diff --git a/challenge-003/abigail/tcl/ch-1.tcl b/challenge-003/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..3c1b5f888c --- /dev/null +++ b/challenge-003/abigail/tcl/ch-1.tcl @@ -0,0 +1,37 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin max] >= 0} { + set ugly(0) 1 + set count 0 + set next_2 0 + set next_3 0 + set next_5 0 + + while {$count < $max - 1} { + incr count + + set c2 [expr 2 * $ugly($next_2)] + set c3 [expr 3 * $ugly($next_3)] + set c5 [expr 5 * $ugly($next_5)] + + if {$c2 <= $c3 && $c2 <= $c5} {set min $c2} + if {$c3 <= $c2 && $c3 <= $c5} {set min $c3} + if {$c5 <= $c2 && $c5 <= $c3} {set min $c5} + + set ugly($count) $min + + if {$c2 <= $ugly($count)} {incr next_2} + if {$c3 <= $ugly($count)} {incr next_3} + if {$c5 <= $ugly($count)} {incr next_5} + } + + puts $ugly($count) +} -- cgit From 8775dec9b972b5df9de24f9beb171656361e1bad Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 20:03:30 +0100 Subject: Week 003 & Week 123: Scheme solution for part 1 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/scheme/ch-1.scm | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-003/abigail/scheme/ch-1.scm (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 5a50b02ed5..59a58a6ead 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -14,6 +14,7 @@ * [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) diff --git a/challenge-003/abigail/scheme/ch-1.scm b/challenge-003/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..32a0facf6c --- /dev/null +++ b/challenge-003/abigail/scheme/ch-1.scm @@ -0,0 +1,43 @@ +#!/usr/local/bin/guile +!# + +;;; +;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + + +(use-modules (ice-9 rdelim)) + + +(define (ugly uglylist mx next_2 next_3 next_5) + (define mymin) + (cond ((= mx (length uglylist)) (list-ref uglylist (- mx 1))) + (else + (set! mymin (min (* 2 (list-ref uglylist next_2)) + (* 3 (list-ref uglylist next_3)) + (* 5 (list-ref uglylist next_5)))) + + (ugly (append uglylist (list mymin) ) + mx + (if (<= (* 2 (list-ref uglylist next_2)) mymin) + (+ next_2 1) next_2) + (if (<= (* 3 (list-ref uglylist next_3)) mymin) + (+ next_3 1) next_3) + (if (<= (* 5 (list-ref uglylist next_5)) mymin) + (+ next_5 1) next_5))))) + +(define (main) + (define mx (read-line)) + (if (not (eof-object? mx)) + (begin + (display (ugly (list 1) (string->number mx) 0 0 0))(newline) + (main) + ) + ) +) + +(main) -- cgit From ae5b756068d9f1e7f877967d3cf69711b65f0a29 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 20:13:54 +0100 Subject: Week 3, part 2: Pascal solution --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/pascal/ch-2.p | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-003/abigail/pascal/ch-2.p (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 59a58a6ead..35517254ce 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -27,6 +27,7 @@ * [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) * [Ruby](ruby/ch-2.rb) diff --git a/challenge-003/abigail/pascal/ch-2.p b/challenge-003/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..596acce61d --- /dev/null +++ b/challenge-003/abigail/pascal/ch-2.p @@ -0,0 +1,36 @@ +Program ch2; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +var + max: integer; + row, col: integer; + current_row, next_row: array of integer; + +begin + while not eof do begin + readln (max); + setlength (current_row, 1); + current_row [0] := 1; + writeln (1); + + for row := 1 to max do begin + setlength (next_row, row + 1); + next_row [0] := 1; + next_row [row] := 1; + write ('1 '); + for col := 1 to row - 1 do begin + next_row [col] := current_row [col - 1] + current_row [col]; + write (next_row [col], ' '); + end; + writeln ('1'); + current_row := next_row; + end + end +end. -- cgit From 5ad1d875e914f3a6f67d04e2f46652180b463a5e Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 20:17:41 +0100 Subject: Week 003: R solution for part 2 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/r/ch-2.r | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 challenge-003/abigail/r/ch-2.r (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 35517254ce..457b8caa5a 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -30,4 +30,5 @@ * [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) diff --git a/challenge-003/abigail/r/ch-2.r b/challenge-003/abigail/r/ch-2.r new file mode 100644 index 0000000000..fa4fa1626c --- /dev/null +++ b/challenge-003/abigail/r/ch-2.r @@ -0,0 +1,24 @@ +#!/usr/local/bin/Rscript + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: Rscript ch-2.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + max <- readLines (stdin, n = 1) + if (length (max) == 0) { + break + } + max = as.integer (max) + row <- c (1) + cat (row, "\n") + for (r in 1 : max) { + row <- c (0, row) + c (row, 0) + cat (row, "\n") + } +} -- cgit From 8d0f6e0eb5075b8f0fc726a16b712b9d262df073 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 20:36:50 +0100 Subject: Week 3, part 2: Tcl solution --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/tcl/ch-2.tcl | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 challenge-003/abigail/tcl/ch-2.tcl (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 457b8caa5a..6adf435e09 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -32,3 +32,4 @@ * [Python](python/ch-2.py) * [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) +* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-003/abigail/tcl/ch-2.tcl b/challenge-003/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..ac8f8ef600 --- /dev/null +++ b/challenge-003/abigail/tcl/ch-2.tcl @@ -0,0 +1,27 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +while {[gets stdin max] >= 0} { + set current_row(0) 1 + puts 1 + for {set row 1} {$row <= $max} {incr row} { + set next_row(0) 1 + set next_row($row) 1 + puts -nonewline "1 " + for {set col 1} {$col < $row} {incr col} { + set next_row($col) \ + [expr $current_row([expr $col - 1]) + $current_row($col)] + puts -nonewline $next_row($col) + puts -nonewline " " + } + puts 1 + array set current_row [array get next_row] + } +} -- cgit From 2283fa58f497b5376d4750fd886ddf70e281fe1a Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 21:02:24 +0100 Subject: Week 3, part 2: Scheme solution --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/scheme/ch-2.scm | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-003/abigail/scheme/ch-2.scm (limited to 'challenge-003') diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 6adf435e09..0438781a9f 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -32,4 +32,5 @@ * [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) diff --git a/challenge-003/abigail/scheme/ch-2.scm b/challenge-003/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..cb57dd539b --- /dev/null +++ b/challenge-003/abigail/scheme/ch-2.scm @@ -0,0 +1,50 @@ +#!/usr/local/bin/guile +!# + +;;; +;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm +;;; + + +(use-modules (ice-9 rdelim)) + +(define (merge list1 list2) + (if (> (length list1) 0) + (append (list (+ (car list1) (car list2))) + (merge (cdr list1) (cdr list2))) + '())) + +(define (nextrow row) + (merge (append (list 0) row) (append row (list 0)))) + +(define (show row) + (cond ((= (length row) 0) (newline)) + (else (display (car row)) + (display " ") + (show (cdr row))))) + +(define (_pascal rows row) + (if (>= rows 0) + (begin + (show row) + (_pascal (- rows 1) (nextrow row))))) + +(define (pascal rows) + (_pascal rows (list 1)) +) + +(define (main) + (define max (read-line)) + (if (not (eof-object? max)) + (begin + (pascal (string->number max)) + (main) + ) + ) +) + +(main) -- cgit From 5ef21313761723d1a1e68fd07d770492a7365d56 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 21:06:32 +0100 Subject: Fix references --- challenge-003/abigail/python/ch-2.py | 2 +- challenge-003/abigail/r/ch-1.r | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-003') diff --git a/challenge-003/abigail/python/ch-2.py b/challenge-003/abigail/python/ch-2.py index 3518b91c8e..76bb1cfc4a 100644 --- a/challenge-003/abigail/python/ch-2.py +++ b/challenge-003/abigail/python/ch-2.py @@ -5,7 +5,7 @@ # # -# Run as python ch-2.py < input-file +# Run as: python ch-2.py < input-file # import fileinput diff --git a/challenge-003/abigail/r/ch-1.r b/challenge-003/abigail/r/ch-1.r index 208c5b4dde..95aeeeaae0 100644 --- a/challenge-003/abigail/r/ch-1.r +++ b/challenge-003/abigail/r/ch-1.r @@ -1,5 +1,5 @@ # -# See ../README.md +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003 # # -- cgit