diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-23 23:40:04 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-23 23:40:04 +0000 |
| commit | db264d695f319bc1ab3a5069e4dafa11ff39ffab (patch) | |
| tree | bf5ec947fff9f8c5d9aace68eee69d395510692d /challenge-001 | |
| parent | 03b70bd180d940e1115b0454dc493fa0351dd127 (diff) | |
| parent | 0f5c21ad90c5f91fe51b3f053edac74e420ff536 (diff) | |
| download | perlweeklychallenge-club-db264d695f319bc1ab3a5069e4dafa11ff39ffab.tar.gz perlweeklychallenge-club-db264d695f319bc1ab3a5069e4dafa11ff39ffab.tar.bz2 perlweeklychallenge-club-db264d695f319bc1ab3a5069e4dafa11ff39ffab.zip | |
Merge branch 'abigail/week-001' of git://github.com/Abigail/perlweeklychallenge-club into Abigail-abigail/week-001
Diffstat (limited to 'challenge-001')
28 files changed, 477 insertions, 0 deletions
diff --git a/challenge-001/abigail/README.md b/challenge-001/abigail/README.md new file mode 100644 index 0000000000..941614e09a --- /dev/null +++ b/challenge-001/abigail/README.md @@ -0,0 +1,47 @@ +# Solutions by Abigail + +## [Challenge #1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-001/#challenge-1) + +Write a script to replace the character `e` with `E` in the string +`"Perl Weekly Challenge"`. Also print the number of times the character +`e` is found in the string. + +### Note +We will not be assuming a fixed string. Instead, we read from STDIN, +and for each line read, we change the 'e' characters to 'E', and +count the number of time we encountered an 'e'. + +### Solutions +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) +* [Befunge-93](befunge/ch-1.bf93) +* [C](c/ch-1.c) +* [lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) +* [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) + + +## [Challenge #2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-001/#challenge-2) + +Write a one-liner to solve the *FizzBuzz* problem and print the +numbers `1` through `20`. However, any number divisible by `3` should +be replaced by the word `fizz` and any divisible by `5` by the word +`buzz` Those numbers that are both divisible by `3` and `5` become +`fizzbuzz`. + +### Note +We will not be assuming a fixed upper bound. Instead, we read the +upper boad from STDIN. + +### Solutions +* [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [Befunge-93](befunge-93/ch-2.bf93) +* [C](c/ch-2.c) +* [lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) +* [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) diff --git a/challenge-001/abigail/awk/ch-1.awk b/challenge-001/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..66adcae5c7 --- /dev/null +++ b/challenge-001/abigail/awk/ch-1.awk @@ -0,0 +1,5 @@ +{ + count = gsub ("e", "E") + print $0 + print count +} diff --git a/challenge-001/abigail/awk/ch-2.awk b/challenge-001/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..23f8dff855 --- /dev/null +++ b/challenge-001/abigail/awk/ch-2.awk @@ -0,0 +1,16 @@ +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + for (i = 1; i <= $0; i ++) { + print (i % 15 == 0) ? "fizzbuzz" \ + : (i % 5 == 0) ? "buzz" \ + : (i % 3 == 0) ? "fizz" \ + : i + } +} diff --git a/challenge-001/abigail/bash/ch-1.sh b/challenge-001/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..7133f8ceb7 --- /dev/null +++ b/challenge-001/abigail/bash/ch-1.sh @@ -0,0 +1,5 @@ +while read line +do echo "${line//e/E}" # Replace all 'e's with 'E'; print result. + ees="${line//[^e]}" # Remove anything which is not an 'e'. + echo "${#ees}" # Print the number of the ees. +done diff --git a/challenge-001/abigail/bash/ch-2.sh b/challenge-001/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..b81a0b0414 --- /dev/null +++ b/challenge-001/abigail/bash/ch-2.sh @@ -0,0 +1,24 @@ +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +while read max +do for ((i = 1; i <= $max; i ++)) + do + out=$i + if (($i % 3 == 0)) + then out="fizz" + fi + if (($i % 5 == 0)) + then out="buzz" + fi + if (($i % 15 == 0)) + then out="fizzbuzz" + fi + echo $out + done +done diff --git a/challenge-001/abigail/befunge-93/ch-1.bf93 b/challenge-001/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..5a37405b87 --- /dev/null +++ b/challenge-001/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,3 @@ +~:1+!#v_:"e"-!#v_>, + "E"+1$ < ^ +@,*25.< diff --git a/challenge-001/abigail/befunge-93/ch-2.bf93 b/challenge-001/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..66349eeb9c --- /dev/null +++ b/challenge-001/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,9 @@ +v >v +& >:5%| +1 > : 3 % | ++>:11g-| +1 @ +1^+1<,*52<.: < +>p0 ^ ^ ,,,,"buzz" < + |%5:,,,,"fizz"< + > ^ diff --git a/challenge-001/abigail/c/ch-1.c b/challenge-001/abigail/c/ch-1.c new file mode 100644 index 0000000000..fabd2d2f22 --- /dev/null +++ b/challenge-001/abigail/c/ch-1.c @@ -0,0 +1,25 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t strlen; + + while ((strlen = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + size_t e_count = 0; + while (* line_ptr) { + if (* line_ptr == 'e') { + * line_ptr = 'E'; + e_count ++; + } + line_ptr ++; + } + printf ("%s%zu\n", line, e_count); + } + free (line); + + return (0); +} diff --git a/challenge-001/abigail/c/ch-2.c b/challenge-001/abigail/c/ch-2.c new file mode 100644 index 0000000000..4c72835827 --- /dev/null +++ b/challenge-001/abigail/c/ch-2.c @@ -0,0 +1,30 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o cc-2.c; ./ch-2.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t strlen; + + while ((strlen = getline (&line, &len, stdin)) != -1) { + int max = atoi (line); + for (int i = 1; i <= max; i ++) { + if (i % 15 == 0) {printf ("%s\n", "fizzbuzz"); continue;} + if (i % 5 == 0) {printf ("%s\n", "buzz"); continue;} + if (i % 3 == 0) {printf ("%s\n", "fizz"); continue;} + printf ("%d\n", i); + } + } + free (line); + + return (0); +} diff --git a/challenge-001/abigail/lua/ch-1.lua b/challenge-001/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..2876669f0d --- /dev/null +++ b/challenge-001/abigail/lua/ch-1.lua @@ -0,0 +1,12 @@ +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + line, count = string . gsub (line, "e", "E"); + io . write (line, "\n", count, "\n") +end diff --git a/challenge-001/abigail/lua/ch-2.lua b/challenge-001/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..749cdcd444 --- /dev/null +++ b/challenge-001/abigail/lua/ch-2.lua @@ -0,0 +1,25 @@ +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + for i = 1, line do + if i % 15 == 0 + then + out = "fizzbuzz" + elseif i % 5 == 0 + then + out = "buzz" + elseif i % 3 == 0 + then + out = "fizz" + else + out = i + end + io . write (out, "\n") + end +end diff --git a/challenge-001/abigail/node/ch-1.js b/challenge-001/abigail/node/ch-1.js new file mode 100644 index 0000000000..9cc9980719 --- /dev/null +++ b/challenge-001/abigail/node/ch-1.js @@ -0,0 +1,21 @@ +// +// Read STDIN. Split on newlines, filter out empty lines, then call "main". +// + require ("fs") +. readFileSync (0) // Read all. +. toString () // Turn it into a string. +. split ("\n") // Split on newlines. +. filter (_ => _ . length) // Filter out empty lines. +. map (_ => { + // + // replace() returns the modified string, so we do a separate + // match to get the actual count of the number of 'e's + // + count = [... _ . matchAll (/e/g)] . length; + // + // Do the replacement, and print the results. Print also + // the number of times 'e' appears. + // + process . stdout . write (_ . replace (/e/g, "E") + "\n" + count + "\n") +}) +; diff --git a/challenge-001/abigail/node/ch-2.js b/challenge-001/abigail/node/ch-2.js new file mode 100644 index 0000000000..41155714f8 --- /dev/null +++ b/challenge-001/abigail/node/ch-2.js @@ -0,0 +1,21 @@ +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ("fs") +. readFileSync (0) // Read all. +. toString () // Turn it into a string. +. split ("\n") // Split on newlines. +. filter (_ => _ . length) // Filter out empty lines. +. map (_ => { + for (let i = 1; i <= +_; i ++) { + console . log (i % 15 == 0 ? "fizzbuzz" + : i % 5 == 0 ? "buzz" + : i % 3 == 0 ? "fizz" + : i) + } +}); diff --git a/challenge-001/abigail/perl/ch-1.pl b/challenge-001/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..8fd893fb93 --- /dev/null +++ b/challenge-001/abigail/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +while (<>) { + my $changes = y/e/E/; + say $_, $changes +} diff --git a/challenge-001/abigail/perl/ch-2.pl b/challenge-001/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..27521c71cb --- /dev/null +++ b/challenge-001/abigail/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/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 +# + +while (<>) { + foreach my $number (1 .. $_) { + say $number % 15 == 0 ? "fizzbuzz" + : $number % 5 == 0 ? "buzz" + : $number % 3 == 0 ? "fizz" + : $number + } +} diff --git a/challenge-001/abigail/python/ch-1.py b/challenge-001/abigail/python/ch-1.py new file mode 100644 index 0000000000..193586bcf6 --- /dev/null +++ b/challenge-001/abigail/python/ch-1.py @@ -0,0 +1,11 @@ +# +# See ../READE,md +# + +# +# Run as python ch-1.py < input-file +# +import fileinput + +for line in fileinput . input (): + print line . replace ("e", "E"), line . count ("e") diff --git a/challenge-001/abigail/python/ch-2.py b/challenge-001/abigail/python/ch-2.py new file mode 100644 index 0000000000..9e93cb08e2 --- /dev/null +++ b/challenge-001/abigail/python/ch-2.py @@ -0,0 +1,16 @@ +# +# See ../READ.md +# + +# +# Run as python ch-2.py < input-file +# + +import fileinput + +for max in fileinput . input (): + for i in range (1, int (max) + 1): + print "fizzbuzz" if i % 15 == 0 else\ + "buzz" if i % 5 == 0 else\ + "fizz" if i % 3 == 0 else\ + i diff --git a/challenge-001/abigail/ruby/ch-1.rb b/challenge-001/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..c3491daf34 --- /dev/null +++ b/challenge-001/abigail/ruby/ch-1.rb @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| + puts _ . gsub "e", "E" + puts _ . count "e" +end diff --git a/challenge-001/abigail/ruby/ch-2.rb b/challenge-001/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..4d68cd1cfa --- /dev/null +++ b/challenge-001/abigail/ruby/ch-2.rb @@ -0,0 +1,16 @@ +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |_| + for i in 1 .. _ . to_i do + puts i % 15 == 0 ? "fizzbuzz" + : i % 5 == 0 ? "buzz" + : i % 3 == 0 ? "fizz" + : i + end +end diff --git a/challenge-001/abigail/t/ctest.ini b/challenge-001/abigail/t/ctest.ini new file mode 100644 index 0000000000..abca6977af --- /dev/null +++ b/challenge-001/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +#
+# Configuration file for running tests, using ctest.
+# See https://github.com/Abigail/Misc/blob/master/ctest
+#
+[names]
+1-1 = The challenge
+1-2 = No e
+2-1 = The Challenge
+2-2 = Regular upper bound
diff --git a/challenge-001/abigail/t/input-1-1 b/challenge-001/abigail/t/input-1-1 new file mode 100644 index 0000000000..8bf4a74aa0 --- /dev/null +++ b/challenge-001/abigail/t/input-1-1 @@ -0,0 +1 @@ +Perl Weekly Challenge diff --git a/challenge-001/abigail/t/input-1-2 b/challenge-001/abigail/t/input-1-2 new file mode 100644 index 0000000000..2e0f5b7508 --- /dev/null +++ b/challenge-001/abigail/t/input-1-2 @@ -0,0 +1 @@ +foo bar baz quux diff --git a/challenge-001/abigail/t/input-2-1 b/challenge-001/abigail/t/input-2-1 new file mode 100644 index 0000000000..209e3ef4b6 --- /dev/null +++ b/challenge-001/abigail/t/input-2-1 @@ -0,0 +1 @@ +20 diff --git a/challenge-001/abigail/t/input-2-2 b/challenge-001/abigail/t/input-2-2 new file mode 100644 index 0000000000..29d6383b52 --- /dev/null +++ b/challenge-001/abigail/t/input-2-2 @@ -0,0 +1 @@ +100 diff --git a/challenge-001/abigail/t/output-1-1.exp b/challenge-001/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..180b2cd17a --- /dev/null +++ b/challenge-001/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +PErl WEEkly ChallEngE +5 diff --git a/challenge-001/abigail/t/output-1-2.exp b/challenge-001/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..674aefe381 --- /dev/null +++ b/challenge-001/abigail/t/output-1-2.exp @@ -0,0 +1,2 @@ +foo bar baz quux +0 diff --git a/challenge-001/abigail/t/output-2-1.exp b/challenge-001/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..aa03b8dd28 --- /dev/null +++ b/challenge-001/abigail/t/output-2-1.exp @@ -0,0 +1,20 @@ +1 +2 +fizz +4 +buzz +fizz +7 +8 +fizz +buzz +11 +fizz +13 +14 +fizzbuzz +16 +17 +fizz +19 +buzz diff --git a/challenge-001/abigail/t/output-2-2.exp b/challenge-001/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..27e9a38fd3 --- /dev/null +++ b/challenge-001/abigail/t/output-2-2.exp @@ -0,0 +1,100 @@ +1 +2 +fizz +4 +buzz +fizz +7 +8 +fizz +buzz +11 +fizz +13 +14 +fizzbuzz +16 +17 +fizz +19 +buzz +fizz +22 +23 +fizz +buzz +26 +fizz +28 +29 +fizzbuzz +31 +32 +fizz +34 +buzz +fizz +37 +38 +fizz +buzz +41 +fizz +43 +44 +fizzbuzz +46 +47 +fizz +49 +buzz +fizz +52 +53 +fizz +buzz +56 +fizz +58 +59 +fizzbuzz +61 +62 +fizz +64 +buzz +fizz +67 +68 +fizz +buzz +71 +fizz +73 +74 +fizzbuzz +76 +77 +fizz +79 +buzz +fizz +82 +83 +fizz +buzz +86 +fizz +88 +89 +fizzbuzz +91 +92 +fizz +94 +buzz +fizz +97 +98 +fizz +buzz |
