From 71f9f1a635be9052c36e5c4c6d25b75281d2376f Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 19:57:38 +0100 Subject: README --- challenge-097/abigail/README.md | 84 ++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 55 deletions(-) diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 9e5afd71ff..03b63ac13b 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -1,84 +1,58 @@ # Solution by Abigail -## [Reverse Words](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK1) +## [Ceasar Cipher](https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/#TASK1) -You are given a string `$S`. +You are given string `$S` containing alphabets `A..Z` only and a number `$N`. -Write a script to reverse the order of words in the given string. -The string may contain leading/trailing spaces. The string may have -more than one space between words in the string. Print the result -without leading/trailing spaces and there should be only one space -between words. +Write a script to encrypt the given string `$S` using Caesar Cipher with +left shift of size `$N`. -### Examples -~~~~ -Input: $S = "The Weekly Challenge" -Output: "Challenge Weekly The" - -Input: $S = " Perl and Raku are part of the same family " -Output: "family same the of part are Raku and Perl" +### Example ~~~~ +Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3 +Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" -### Notes +Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ +Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW -The challenge isn't quite clear on whether we should output a number -(the minimal number of operations required), or the actual operations. -The examples show both -- but separated by a blank line. Previous -challenges typically use a blank line to separate the required output -from the explaination on why that it is the correct answer. - -We're opting to only print the number of operations, not the actual -operations. +Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD +~~~~ ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](sh/ch-1.sh) -* [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) ### Blog -[Perl Weekly Challenge 96: Reverse Words](https://wp.me/pcxd30-mj) -## [Edit Distance](https://perlweeklychallenge.org/blog/perl-weekly-challenge-096/#TASK2) +## [Binary Substrings](https://perlweeklychallenge.org/blog/perl-weekly-challenge-097/#TASK2) -You are given two strings `$S1` and `$S2`. +You are given a binary string `$B` and an integer `$S`. -Write a script to find out the minimum operations required to convert -`$S1` into `$S2`. The operations can be insert, remove or replace a -character. Please check out [Wikipedia -page](https://en.wikipedia.org/wiki/Edit_distance) for more information. +Write a script to split the binary string `$B` of size `$S` and then +find the minimum number of flips required to make it all the same. -### Example +### Examples +#### Example 1 ~~~~ -Input: $S1 = "kitten"; $S2 = "sitting" -Output: 3 +Input: $B = "101100101" $S = 3 +Output: 1 -Operation 1: replace 'k' with 's' -Operation 2: replace 'e' with 'i' -Operation 3: insert 'g' at the end +Binary Substrings: + "101": 0 flip + "100": 1 flip to make it "101" + "101": 0 flip ~~~~ +#### Example 2 ~~~~ -Input: $S1 = "sunday"; $S2 = "monday" +Input $B = "10110111", $S = 4 Output: 2 -Operation 1: replace 's' with 'm' -Operation 2: replace 'u' with 'o' +Binary Substrings: + "1011": 0 flip + "0111": 2 flips to make it "1011" ~~~~ ### Solutions -* [AWK](awk/ch-2.awk) -* [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) ### Blog -[Perl Weekly Challenge 96: Edit Distance](https://wp.me/pcxd30-n7) -- cgit From 2f7c1b777992058c0b7d4082435e2db27253a871 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 20:00:55 +0100 Subject: Examples --- challenge-097/abigail/t/ctest.ini | 3 +++ challenge-097/abigail/t/input-1-1 | 1 + challenge-097/abigail/t/input-2-1 | 2 ++ challenge-097/abigail/t/output-1-1.exp | 1 + challenge-097/abigail/t/output-2-1.exp | 2 ++ 5 files changed, 9 insertions(+) create mode 100644 challenge-097/abigail/t/ctest.ini create mode 100644 challenge-097/abigail/t/input-1-1 create mode 100644 challenge-097/abigail/t/input-2-1 create mode 100644 challenge-097/abigail/t/output-1-1.exp create mode 100644 challenge-097/abigail/t/output-2-1.exp diff --git a/challenge-097/abigail/t/ctest.ini b/challenge-097/abigail/t/ctest.ini new file mode 100644 index 0000000000..0ae9d07567 --- /dev/null +++ b/challenge-097/abigail/t/ctest.ini @@ -0,0 +1,3 @@ +[names] +1-1 = Given example +2-1 = Given examples diff --git a/challenge-097/abigail/t/input-1-1 b/challenge-097/abigail/t/input-1-1 new file mode 100644 index 0000000000..6c3d28d154 --- /dev/null +++ b/challenge-097/abigail/t/input-1-1 @@ -0,0 +1 @@ +3 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG diff --git a/challenge-097/abigail/t/input-2-1 b/challenge-097/abigail/t/input-2-1 new file mode 100644 index 0000000000..33d5874cee --- /dev/null +++ b/challenge-097/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +3 101100101 +4 10110111 diff --git a/challenge-097/abigail/t/output-1-1.exp b/challenge-097/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..b841844ca3 --- /dev/null +++ b/challenge-097/abigail/t/output-1-1.exp @@ -0,0 +1 @@ +QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD diff --git a/challenge-097/abigail/t/output-2-1.exp b/challenge-097/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..1191247b6d --- /dev/null +++ b/challenge-097/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +1 +2 -- cgit From 6c5715542385de49f20d0f56d1c9aa66b5736cab Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 20:10:22 +0100 Subject: Perl solution for week 97, part 1 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge-097/abigail/perl/ch-1.pl diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 03b63ac13b..4c9207dc23 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -20,6 +20,7 @@ Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD ~~~~ ### Solutions +* [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-097/abigail/perl/ch-1.pl b/challenge-097/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..9ffda0a6e5 --- /dev/null +++ b/challenge-097/abigail/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/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 +# + +while (<>) { + chomp; + my ($times, $plain) = split ' ', $_, 2; + $times %= 26; + $plain =~ y/A-Z/ZA-Y/ for 1 .. $times; + say $plain; +} -- cgit From 1889deddca521e1aa013d53a749c693c6589deba Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 01:13:15 +0100 Subject: Use an option to determine the shift size. --- challenge-097/abigail/perl/ch-1.pl | 16 +++++++++++----- challenge-097/abigail/t/ctest.ini | 3 +++ challenge-097/abigail/t/input-1-1 | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/challenge-097/abigail/perl/ch-1.pl b/challenge-097/abigail/perl/ch-1.pl index 9ffda0a6e5..eabc49db2c 100644 --- a/challenge-097/abigail/perl/ch-1.pl +++ b/challenge-097/abigail/perl/ch-1.pl @@ -14,13 +14,19 @@ use experimental 'lexical_subs'; # # -# Run as: perl ch-1.pl < input-file +# Run as: perl ch-1.pl -t TIMES < input-file # +use Getopt::Long; + +GetOptions 't=i' => \my $times; + +die "-t option required" unless defined $times; + +$times %= 26; + while (<>) { chomp; - my ($times, $plain) = split ' ', $_, 2; - $times %= 26; - $plain =~ y/A-Z/ZA-Y/ for 1 .. $times; - say $plain; + s/([A-Z])/my $ch = ord ($1) - $times; $ch += 26 if $ch < 65; chr $ch/eg; + say; } diff --git a/challenge-097/abigail/t/ctest.ini b/challenge-097/abigail/t/ctest.ini index 0ae9d07567..8fee647f8b 100644 --- a/challenge-097/abigail/t/ctest.ini +++ b/challenge-097/abigail/t/ctest.ini @@ -1,3 +1,6 @@ [names] 1-1 = Given example 2-1 = Given examples + +[1-1] +args = -t 3 diff --git a/challenge-097/abigail/t/input-1-1 b/challenge-097/abigail/t/input-1-1 index 6c3d28d154..678239264e 100644 --- a/challenge-097/abigail/t/input-1-1 +++ b/challenge-097/abigail/t/input-1-1 @@ -1 +1 @@ -3 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG +THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG -- cgit From 3a68dc14c70c709ea8097e98e38931ef93e643af Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 01:53:18 +0100 Subject: Use option -s (shift), instead of -t. --- challenge-097/abigail/README.md | 5 +++++ challenge-097/abigail/perl/ch-1.pl | 10 +++++----- challenge-097/abigail/t/ctest.ini | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 4c9207dc23..9eec07616d 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -19,7 +19,12 @@ Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD ~~~~ +### Note +We will be reading the plain text from STDIN, and use an option (-s) +to indicate the left shift. + ### Solutions +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-097/abigail/perl/ch-1.pl b/challenge-097/abigail/perl/ch-1.pl index eabc49db2c..9a94c07550 100644 --- a/challenge-097/abigail/perl/ch-1.pl +++ b/challenge-097/abigail/perl/ch-1.pl @@ -14,19 +14,19 @@ use experimental 'lexical_subs'; # # -# Run as: perl ch-1.pl -t TIMES < input-file +# Run as: perl ch-1.pl -s TIMES < input-file # use Getopt::Long; -GetOptions 't=i' => \my $times; +GetOptions 's=i' => \my $shift; -die "-t option required" unless defined $times; +die "-s option required" unless defined $shift; -$times %= 26; +$shift %= 26; while (<>) { chomp; - s/([A-Z])/my $ch = ord ($1) - $times; $ch += 26 if $ch < 65; chr $ch/eg; + s/([A-Z])/my $ch = ord ($1) - $shift; $ch += 26 if $ch < 65; chr $ch/eg; say; } diff --git a/challenge-097/abigail/t/ctest.ini b/challenge-097/abigail/t/ctest.ini index 8fee647f8b..886776f178 100644 --- a/challenge-097/abigail/t/ctest.ini +++ b/challenge-097/abigail/t/ctest.ini @@ -3,4 +3,4 @@ 2-1 = Given examples [1-1] -args = -t 3 +args = -s 3 -- cgit From 2ff2fcdc51c6bad5b0bd61f645226e7838c26579 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 01:54:23 +0100 Subject: Node.js solution for week 097, part 1 --- challenge-097/abigail/node/ch-1.js | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-097/abigail/node/ch-1.js diff --git a/challenge-097/abigail/node/ch-1.js b/challenge-097/abigail/node/ch-1.js new file mode 100644 index 0000000000..0cba6f1a23 --- /dev/null +++ b/challenge-097/abigail/node/ch-1.js @@ -0,0 +1,50 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js -s TIMES < input-file +// + +const NR_OF_LETTERS = 26 + +// +// Parse input +// +const argv = require ('yargs') +. option ('s', { + type: 'number', + }) +. demandOption ('s') +. argv; + +const shift = argv . s + +// +// Shift capital letters, by a given amount. +// +function shift_char (char, shift) { + if (char . match (/[A-Z]/)) { + let n = char . charCodeAt (0) - (shift % NR_OF_LETTERS) + if (n < 65) { + n = n + NR_OF_LETTERS + } + return String . fromCharCode (n) + } + else { + return char + } +} + + +// +// Iterate over the input, and shift characters +// +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (_ . split ("") + . map (_ => shift_char (_, shift)) + . join (""))) +; -- cgit From cc68e1c2ea0d685072b4063aeb9af50f7e42cd98 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 02:09:46 +0100 Subject: Bash solution for week 097, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/bash/ch-1.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-097/abigail/bash/ch-1.sh diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 9eec07616d..bd64d4cbbe 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -24,6 +24,7 @@ We will be reading the plain text from STDIN, and use an option (-s) to indicate the left shift. ### Solutions +* [Bash](bash/ch-1.sh) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-097/abigail/bash/ch-1.sh b/challenge-097/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..e51ebe5562 --- /dev/null +++ b/challenge-097/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh -s SHIFT < input-file +# + +# +# Read the option +# +while getopts "s:" name +do if [ "$name" = "s" ] + then shift=$OPTARG + fi +done + + +# +# Iterate over the input, shifting each line as many times as needed +# +while read line +do for ((i = 0; i < $shift; i ++)) + do line=`echo $line | tr A-Z ZA-Y` + done + echo $line +done -- cgit From 31f71b49569afa8b719e15047bb0d2392bbb7993 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 02:52:09 +0100 Subject: AWK solution for week 097, part 1 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/awk/ch-1.awk | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-097/abigail/awk/ch-1.awk diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index bd64d4cbbe..6d03a861aa 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -24,6 +24,7 @@ We will be reading the plain text from STDIN, and use an option (-s) to indicate the left shift. ### Solutions +* [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-097/abigail/awk/ch-1.awk b/challenge-097/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..a33507ee33 --- /dev/null +++ b/challenge-097/abigail/awk/ch-1.awk @@ -0,0 +1,53 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk -s SHIFT < input-file +# + +BEGIN { + NR_OF_LETTERS = 26 + ORD_A = 65 + + # + # Create a letter to number mapping + # + for (i = ORD_A; i < ORD_A + NR_OF_LETTERS; i ++) { + t = sprintf ("%c", i) + ord [t] = i + } + + # + # Parse command line + # + for (i = 1; i < ARGC; i ++) { + if (ARGV [i] == "-s") { + shift = ARGV [i + 1] + } + } + ARGC = 0 +} + +{ + split($0, letters, "") + out = "" + # + # Iterate over the individual letters, shifting capital letters + # + for (i = 1; i <= length (letters); i ++) { + char = letters [i] + if (ord [char]) { + n = ord [char] - shift + if (n < ORD_A) { + n = n + NR_OF_LETTERS + } + char = sprintf ("%c", n) + } + out = out char + } + print out +} + -- cgit From e1e2237723cd0e85c69c1c53644ebe8c9614dc8a Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 12:22:55 +0100 Subject: C solution for week 97, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/c/ch-1.c | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-097/abigail/c/ch-1.c diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 6d03a861aa..50f59dc1e2 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -26,6 +26,7 @@ to indicate the left shift. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-097/abigail/c/ch-1.c b/challenge-097/abigail/c/ch-1.c new file mode 100644 index 0000000000..8219361e36 --- /dev/null +++ b/challenge-097/abigail/c/ch-1.c @@ -0,0 +1,50 @@ +# include +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o -s SHIFT < input-file + */ + +int main (int argc, char ** argv) { + char * line = NULL; + size_t len = 0; + size_t strlen; + int ch; + int shift = -1; + int NR_OF_LETTERS = 26; + + while ((ch = getopt (argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + shift = atoi (optarg) % NR_OF_LETTERS; + break; + } + } + if (shift < 0) { + fprintf (stderr, "Requires an -s parameter\n"); + exit (1); + } + + while ((strlen = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + while (* line_ptr) { + if (isupper (* line_ptr)) { + * line_ptr -= shift; + if (* line_ptr < 'A') { + * line_ptr += NR_OF_LETTERS; + } + } + line_ptr ++; + } + printf ("%s", line); + } + free (line); + return (0); +} -- cgit From 2655ea9e186249126f61b18ec9b02c7069ab4235 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 12:59:54 +0100 Subject: Lua solution for week 97, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/lua/ch-1.lua | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-097/abigail/lua/ch-1.lua diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 50f59dc1e2..3f52048daa 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -26,6 +26,7 @@ to indicate the left shift. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [Lua](lua/ch-1.lua) * [C](c/ch-1.c) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-097/abigail/lua/ch-1.lua b/challenge-097/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..d674cdf7c2 --- /dev/null +++ b/challenge-097/abigail/lua/ch-1.lua @@ -0,0 +1,47 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua -s SHIFT < input-file +-- + +local shift = -1 +local NR_OF_LETTERS = 26 +local ORD_A = string . byte ("A") + +-- +-- Parse option, and exit if incorrect +-- +if #arg == 2 and arg [1] == "-s" +then shift = arg [2] % NR_OF_LETTERS +end + +if shift < 0 +then io . stderr : write ("Requires a '-s SHIFT' option\n") + os . exit (1) +end + + +-- +-- Shift a capital letter 'shift' places to the left, +-- rotating if shifted before 'A' +-- +function do_shift (char, shift) + local n = string . byte (char) - shift + if n < ORD_A + then n = n + NR_OF_LETTERS + end + return string . char (n) +end + +-- +-- Shift capital letters +-- +for line in io . lines () do + io . write (string . gsub (line, "[A-Z]", function (ch) + return do_shift (ch, shift) + end), "\n") +end -- cgit From 6b9d80211e0e0330704638b9d2ac74c6fc2b4eba Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 17:35:17 +0100 Subject: Python solution for week 97, part 1 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/python/ch-1.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-097/abigail/python/ch-1.py diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 3f52048daa..ef9eaa5301 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -30,6 +30,7 @@ to indicate the left shift. * [C](c/ch-1.c) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ### Blog diff --git a/challenge-097/abigail/python/ch-1.py b/challenge-097/abigail/python/ch-1.py new file mode 100644 index 0000000000..d5a42bbcf2 --- /dev/null +++ b/challenge-097/abigail/python/ch-1.py @@ -0,0 +1,46 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-1.py -s SHIFT < input-file +# + +import fileinput +import sys +import getopt + +NR_OF_LETTERS = 26 +ORD_A = ord ("A") + +# +# Parse and validate options +# +shift = -1 +opts, args = getopt . getopt (sys . argv [1:], 's:') +for opt, val in opts: + if opt == "-s": + shift = int (val) % NR_OF_LETTERS + +sys . argv [1:] = [] + +if shift < 0: + sys . stderr . write ("Argument -s SHIFT is required\n") + sys . exit (1) + +# +# Iterate over the input. Shift capital letters +# +for line in fileinput . input (): + out = "" + for i in range (len (line)): + if "A" <= line [i] <= "Z": + n = ord (line [i]) - shift + if n < ORD_A: + n = n + NR_OF_LETTERS + out += chr (n) + else: + out += line [i] + sys . stdout . write (out) -- cgit From ae613406d9d15aaecda3bf823c09cc50af180b5b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 18:24:43 +0100 Subject: Ruby solution for week 97, part 1 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/ruby/ch-1.rb | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-097/abigail/ruby/ch-1.rb diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index ef9eaa5301..0e2295286a 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -31,6 +31,7 @@ to indicate the left shift. * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-097/abigail/ruby/ch-1.rb b/challenge-097/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..1225bd290c --- /dev/null +++ b/challenge-097/abigail/ruby/ch-1.rb @@ -0,0 +1,46 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb -s SHIFT < input-file +# + +require 'optparse' + +NR_OF_LETTERS = 26 + + +# +# Parse and validate options +# +params = ARGV . getopts ('s:') +shift = params ["s"] ? params ["s"] . to_i % NR_OF_LETTERS : -1 + +if shift < 0 + STDERR . puts "Requires a -s SHIFT option" + exit 1 +end + + +# +# Method to shift an upper case letter +# +def shift_letter (letter, shift) + n = letter . ord - shift + if n < 'A' . ord + then n = n + NR_OF_LETTERS + end + return n . chr +end + + +# +# Iterate over the input, shift capital letters +# +ARGF . each_line do |line| + line = line . gsub (/[A-Z]/) {|_| shift_letter _, shift} + puts line +end -- cgit From f4e861bc27167c67ebb9bbda9e03884778cdd525 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 19:13:48 +0100 Subject: Split example for part 2 Since they need different command line options --- challenge-097/abigail/t/ctest.ini | 9 ++++++++- challenge-097/abigail/t/input-2-1 | 3 +-- challenge-097/abigail/t/input-2-2 | 1 + challenge-097/abigail/t/output-2-1.exp | 1 - challenge-097/abigail/t/output-2-2.exp | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 challenge-097/abigail/t/input-2-2 create mode 100644 challenge-097/abigail/t/output-2-2.exp diff --git a/challenge-097/abigail/t/ctest.ini b/challenge-097/abigail/t/ctest.ini index 886776f178..bf326e1fd7 100644 --- a/challenge-097/abigail/t/ctest.ini +++ b/challenge-097/abigail/t/ctest.ini @@ -1,6 +1,13 @@ [names] 1-1 = Given example -2-1 = Given examples +2-1 = Given example 1 +2-2 = Given example 2 [1-1] args = -s 3 + +[2-1] +args = -s 3 + +[2-2] +args = -s 4 diff --git a/challenge-097/abigail/t/input-2-1 b/challenge-097/abigail/t/input-2-1 index 33d5874cee..d3c6b61c0c 100644 --- a/challenge-097/abigail/t/input-2-1 +++ b/challenge-097/abigail/t/input-2-1 @@ -1,2 +1 @@ -3 101100101 -4 10110111 +101100101 diff --git a/challenge-097/abigail/t/input-2-2 b/challenge-097/abigail/t/input-2-2 new file mode 100644 index 0000000000..057b63d2d9 --- /dev/null +++ b/challenge-097/abigail/t/input-2-2 @@ -0,0 +1 @@ +10110111 diff --git a/challenge-097/abigail/t/output-2-1.exp b/challenge-097/abigail/t/output-2-1.exp index 1191247b6d..d00491fd7e 100644 --- a/challenge-097/abigail/t/output-2-1.exp +++ b/challenge-097/abigail/t/output-2-1.exp @@ -1,2 +1 @@ 1 -2 diff --git a/challenge-097/abigail/t/output-2-2.exp b/challenge-097/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/challenge-097/abigail/t/output-2-2.exp @@ -0,0 +1 @@ +2 -- cgit From 18f3e7ff84691edc8c9d8241f72005c0e6cb8f7e Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 19:22:57 +0100 Subject: Perl solution for week 97, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-097/abigail/perl/ch-2.pl diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 0e2295286a..1761fd8622 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -66,5 +66,6 @@ Binary Substrings: ~~~~ ### Solutions +* [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-097/abigail/perl/ch-2.pl b/challenge-097/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..b0d145069e --- /dev/null +++ b/challenge-097/abigail/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/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 -s SECTIONS < input-file +# + +use Getopt::Long; +use List::Util qw [min]; + +GetOptions 's=i' => \my $sections; + +die "-s option required" unless defined $sections && $sections > 0; + +# +# To get the minimum number of flips, for each position take the +# minimum of the number of 0s and the number of 1s, and sum over +# all the positions +# + +while (<>) { + my $sum = 0; + chomp; + my @chunks = /.{$sections}/g; + for (my $i = 0; $i < length $chunks [0]; $i ++) { + my $zeros = grep {substr ($_, $i, 1) eq "0"} @chunks; + $sum += min $zeros, @chunks - $zeros; + } + say $sum; +} -- cgit From 639e5ef464790d8954a9ebbf667188311350ca1a Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 19:45:06 +0100 Subject: AWK solution for week 97, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/awk/ch-2.awk | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-097/abigail/awk/ch-2.awk diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 1761fd8622..e5b520217a 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -66,6 +66,7 @@ Binary Substrings: ~~~~ ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-097/abigail/awk/ch-2.awk b/challenge-097/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..f3d1ef2e89 --- /dev/null +++ b/challenge-097/abigail/awk/ch-2.awk @@ -0,0 +1,46 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk -s SECTIONS < input-file +# + +BEGIN { + # + # Parse command line + # + for (i = 1; i < ARGC; i ++) { + if (ARGV [i] == "-s") { + sections = ARGV [i + 1] + } + } + ARGC = 0 +} + +{ + # + # Split the string into individual letters, use + # indexing to get the ith letter of the jth section + # + split ($0, letters, "") + sum = 0 + l = length($0) / sections # Length of a section + for (i = 0; i < l; i ++) { + zeros = 0; # Count the zeros on position i + for (j = 0; j < sections; j ++) { + if (letters [j * l + i + 1] == "0") { + zeros ++ + } + } + ones = sections - zeros + # + # Sum the minimum of the 0's and 1's + # + sum += zeros < ones ? zeros : ones + } + print sum +} + -- cgit From 3dc5f6becbebddd2f841ac8463065a4471cb679f Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 00:17:21 +0100 Subject: Fix comment --- challenge-097/abigail/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-097/abigail/perl/ch-1.pl b/challenge-097/abigail/perl/ch-1.pl index 9a94c07550..7558150f4d 100644 --- a/challenge-097/abigail/perl/ch-1.pl +++ b/challenge-097/abigail/perl/ch-1.pl @@ -14,7 +14,7 @@ use experimental 'lexical_subs'; # # -# Run as: perl ch-1.pl -s TIMES < input-file +# Run as: perl ch-1.pl -s SHIFT < input-file # use Getopt::Long; -- cgit From c045cac9f990ed22afe8c2630a014c74baea3b46 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 00:29:05 +0100 Subject: Bash solution: disable pathname expansion --- challenge-097/abigail/bash/ch-1.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/challenge-097/abigail/bash/ch-1.sh b/challenge-097/abigail/bash/ch-1.sh index e51ebe5562..8e275a6519 100644 --- a/challenge-097/abigail/bash/ch-1.sh +++ b/challenge-097/abigail/bash/ch-1.sh @@ -8,6 +8,11 @@ # Run as: bash ch-1.sh -s SHIFT < input-file # +# +# Disable pathname expansion +# +set -f + # # Read the option # -- cgit From 45121a3b1ac26d9806cc672d7dcbef8e162d1e18 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 00:29:48 +0100 Subject: C solution for week 97. part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/c/ch-2.c | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-097/abigail/c/ch-2.c diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index e5b520217a..10240df509 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -67,6 +67,7 @@ Binary Substrings: ### Solutions * [AWK](awk/ch-2.awk) +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-097/abigail/c/ch-2.c b/challenge-097/abigail/c/ch-2.c new file mode 100644 index 0000000000..dd1441f6a4 --- /dev/null +++ b/challenge-097/abigail/c/ch-2.c @@ -0,0 +1,52 @@ +# include +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o -s SECTIONS < input-file + */ + +int main (int argc, char ** argv) { + char * line = NULL; + size_t len = 0; + size_t strlen; + int ch; + int sections = -1; + + while ((ch = getopt (argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + sections = atoi (optarg); + break; + } + } + if (sections < 0) { + fprintf (stderr, "Requires an -s SECTIONS parameter\n"); + exit (1); + } + + while ((strlen = getline (&line, &len, stdin)) != -1) { + strlen --; /* We don't care about the newline */ + int len = strlen / sections; /* Section length */ + int sum = 0; + for (int i = 0; i < len; i ++) { + int zeros = 0; + for (int j = 0; j < sections; j ++) { + if (line [j * len + i] == '0') { + zeros ++; + } + } + int ones = sections - zeros; + sum += zeros < ones ? zeros : ones; + } + printf ("%d\n", sum); + } + free (line); + return (0); +} -- cgit From 8f7f5c93def45398db496f35e7921cfe0d264d1d Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 26 Jan 2021 23:31:20 +0000 Subject: Remove List::Util and Data::Dump dependencies --- challenge-007/paulo-custodio/perl/ch-1.pl | 8 +++++++- challenge-008/paulo-custodio/perl/ch-2.pl | 10 +++++++++- challenge-010/paulo-custodio/perl/ch-2.pl | 21 +++++++++++++++++---- challenge-012/paulo-custodio/perl/ch-2.pl | 2 -- challenge-014/paulo-custodio/perl/ch-2.pl | 2 -- challenge-016/paulo-custodio/perl/ch-2.pl | 1 - challenge-018/paulo-custodio/perl/ch-1.pl | 9 +++++++-- challenge-021/paulo-custodio/perl/ch-2.pl | 2 -- challenge-024/paulo-custodio/perl/ch-2.pl | 2 -- challenge-026/paulo-custodio/perl/ch-2.pl | 7 ++++++- challenge-076/paulo-custodio/perl/ch-1.pl | 7 ++++++- challenge-077/paulo-custodio/perl/ch-1.pl | 7 ++++++- challenge-079/paulo-custodio/perl/ch-1.pl | 7 ++++++- challenge-079/paulo-custodio/perl/ch-2.pl | 15 ++++++++++++++- challenge-080/paulo-custodio/perl/ch-2.pl | 7 ++++++- challenge-095/paulo-custodio/perl/ch-2.pl | 11 +++++++++-- challenge-096/paulo-custodio/perl/ch-2.pl | 11 ++++++++--- challenge-096/paulo-custodio/perl/ch-2a.pl | 9 ++++++++- 18 files changed, 109 insertions(+), 29 deletions(-) diff --git a/challenge-007/paulo-custodio/perl/ch-1.pl b/challenge-007/paulo-custodio/perl/ch-1.pl index ad7c00ff22..d60477d7fa 100644 --- a/challenge-007/paulo-custodio/perl/ch-1.pl +++ b/challenge-007/paulo-custodio/perl/ch-1.pl @@ -9,7 +9,6 @@ use strict; use warnings; use 5.030; -use List::Util 'sum'; sub solve { my($max) = @_; @@ -19,5 +18,12 @@ sub solve { } } +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} + + my $max = shift || 50; solve($max); diff --git a/challenge-008/paulo-custodio/perl/ch-2.pl b/challenge-008/paulo-custodio/perl/ch-2.pl index afea2cc1f9..39d44bd75a 100644 --- a/challenge-008/paulo-custodio/perl/ch-2.pl +++ b/challenge-008/paulo-custodio/perl/ch-2.pl @@ -11,7 +11,6 @@ use strict; use warnings; use 5.030; -use List::Util 'max'; sub center { my(@lines) = @_; @@ -20,5 +19,14 @@ sub center { return @lines; } +sub max { + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; +} + + my @lines = center(@ARGV); say join "\n", @lines; diff --git a/challenge-010/paulo-custodio/perl/ch-2.pl b/challenge-010/paulo-custodio/perl/ch-2.pl index 165abd8481..85ea6e9281 100644 --- a/challenge-010/paulo-custodio/perl/ch-2.pl +++ b/challenge-010/paulo-custodio/perl/ch-2.pl @@ -9,10 +9,6 @@ use strict; use warnings; use 5.030; -use List::Util 'max', 'min'; - - -use Data::Dump 'dump'; sub jaro_similarity { my($s1, $s2) = @_; @@ -88,4 +84,21 @@ sub jaro_winkler_distance { return 1-jaro_winkler_similarity($s1,$s2); } +sub min { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; +} + +sub max { + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; +} + + say jaro_winkler_distance(@ARGV); diff --git a/challenge-012/paulo-custodio/perl/ch-2.pl b/challenge-012/paulo-custodio/perl/ch-2.pl index d3d39d2184..8aa3015ae9 100644 --- a/challenge-012/paulo-custodio/perl/ch-2.pl +++ b/challenge-012/paulo-custodio/perl/ch-2.pl @@ -17,8 +17,6 @@ use strict; use warnings; use 5.030; -use Data::Dump 'dump'; - # extract a common prefix, if one exists sub extract_common_prefix { my($paths) = @_; diff --git a/challenge-014/paulo-custodio/perl/ch-2.pl b/challenge-014/paulo-custodio/perl/ch-2.pl index 0586bf1282..11cacb2bfe 100644 --- a/challenge-014/paulo-custodio/perl/ch-2.pl +++ b/challenge-014/paulo-custodio/perl/ch-2.pl @@ -19,8 +19,6 @@ use warnings; use 5.030; use Locale::US; -use Data::Dump 'dump'; - my $u = Locale::US->new; my @codes = $u->all_state_codes; # all states two letter codes my $codes = join("|", @codes); # regex to match any codes diff --git a/challenge-016/paulo-custodio/perl/ch-2.pl b/challenge-016/paulo-custodio/perl/ch-2.pl index a497c3f127..6155424257 100644 --- a/challenge-016/paulo-custodio/perl/ch-2.pl +++ b/challenge-016/paulo-custodio/perl/ch-2.pl @@ -14,7 +14,6 @@ # # 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 # 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy -use Data::Dump 'dump'; use strict; use warnings; diff --git a/challenge-018/paulo-custodio/perl/ch-1.pl b/challenge-018/paulo-custodio/perl/ch-1.pl index a7c7d65c34..b79fbe788b 100644 --- a/challenge-018/paulo-custodio/perl/ch-1.pl +++ b/challenge-018/paulo-custodio/perl/ch-1.pl @@ -12,7 +12,6 @@ use strict; use warnings; use 5.030; -use List::Util 'all'; say "(", join(", ", map {$_=qq("$_")} longest_substr(@ARGV)), ")"; @@ -28,7 +27,7 @@ sub longest_substr { next if $longest_len > $len; # prune search my $substr = substr($str, $s, $len); next if $longest{$str}; # prune search - if (all {/$substr/} @strs) { # matches all + if (all(sub {/$substr/}, @strs)) { # matches all if ($longest_len == $len) { $longest{$substr}=1; } @@ -42,3 +41,9 @@ sub longest_substr { } return sort keys %longest; } + +sub all { + my($sub, @a) = @_; + for (@a) { return if !$sub->($_); } + return 1; +} diff --git a/challenge-021/paulo-custodio/perl/ch-2.pl b/challenge-021/paulo-custodio/perl/ch-2.pl index 9fc86e910d..9f4a0fc9aa 100644 --- a/challenge-021/paulo-custodio/perl/ch-2.pl +++ b/challenge-021/paulo-custodio/perl/ch-2.pl @@ -15,8 +15,6 @@ use strict; use warnings; use 5.030; -use Data::Dump 'dump'; - sub decode_triplets { my($hex) = @_; my $c = chr(hex($hex)); diff --git a/challenge-024/paulo-custodio/perl/ch-2.pl b/challenge-024/paulo-custodio/perl/ch-2.pl index 5bd3c85fe5..6c85b9ba4d 100644 --- a/challenge-024/paulo-custodio/perl/ch-2.pl +++ b/challenge-024/paulo-custodio/perl/ch-2.pl @@ -25,8 +25,6 @@ use DBD::SQLite; use Path::Tiny; use constant DBFILE => "index.db3"; -use Data::Dump 'dump'; - # Create database if index does not exist BEGIN { if (! -f DBFILE) { diff --git a/challenge-026/paulo-custodio/perl/ch-2.pl b/challenge-026/paulo-custodio/perl/ch-2.pl index 487024d364..e2a27e4736 100644 --- a/challenge-026/paulo-custodio/perl/ch-2.pl +++ b/challenge-026/paulo-custodio/perl/ch-2.pl @@ -10,7 +10,6 @@ use strict; use warnings; use 5.030; use Math::Trig; -use List::Util 'sum'; say mean(@ARGV); @@ -31,3 +30,9 @@ sub mean { $a = rad2deg($a); return $a; } + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-076/paulo-custodio/perl/ch-1.pl b/challenge-076/paulo-custodio/perl/ch-1.pl index 1eeef4b579..a3adbf928e 100644 --- a/challenge-076/paulo-custodio/perl/ch-1.pl +++ b/challenge-076/paulo-custodio/perl/ch-1.pl @@ -21,7 +21,6 @@ use strict; use warnings; use 5.030; use Math::Combinatorics; -use List::Util 'sum'; my($N) = shift; @@ -94,3 +93,9 @@ sub find_set { } return @solution; } + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-077/paulo-custodio/perl/ch-1.pl b/challenge-077/paulo-custodio/perl/ch-1.pl index 03544024a8..7939010631 100644 --- a/challenge-077/paulo-custodio/perl/ch-1.pl +++ b/challenge-077/paulo-custodio/perl/ch-1.pl @@ -28,7 +28,6 @@ use strict; use warnings; use 5.030; use Math::Combinatorics; -use List::Util 'sum'; my($N) = shift; @@ -59,3 +58,9 @@ sub compute_fib { push @fib, $fib[-1]+$fib[-2]; } } + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-079/paulo-custodio/perl/ch-1.pl b/challenge-079/paulo-custodio/perl/ch-1.pl index 2b34fdff7b..dba39d1251 100644 --- a/challenge-079/paulo-custodio/perl/ch-1.pl +++ b/challenge-079/paulo-custodio/perl/ch-1.pl @@ -56,7 +56,6 @@ use strict; use warnings; use 5.030; -use List::Util 'sum'; @ARGV==1 or die "Usage: ch-1.pl N\n"; say(sum(map {bit_count($_)} 1..$ARGV[0]) % 1000000007); @@ -70,3 +69,9 @@ sub bit_count { } return $count; } + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-079/paulo-custodio/perl/ch-2.pl b/challenge-079/paulo-custodio/perl/ch-2.pl index 8caa2c0c95..1c4248898a 100644 --- a/challenge-079/paulo-custodio/perl/ch-2.pl +++ b/challenge-079/paulo-custodio/perl/ch-2.pl @@ -39,7 +39,6 @@ use strict; use warnings; use 5.030; -use List::Util 'max', 'sum'; @ARGV or die "Usage: ch-2.pl list\n"; my @N = @ARGV; @@ -71,3 +70,17 @@ sub draw_hist { } return @hist; } + +sub max { + my($max, @a) = @_; + for (@a) { + $max = $_ if $max < $_; + } + return $max; +} + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-080/paulo-custodio/perl/ch-2.pl b/challenge-080/paulo-custodio/perl/ch-2.pl index b22a6edd3a..10061bdc95 100644 --- a/challenge-080/paulo-custodio/perl/ch-2.pl +++ b/challenge-080/paulo-custodio/perl/ch-2.pl @@ -26,7 +26,6 @@ use strict; use warnings; use 5.030; -use List::Util 'sum'; say candies(@ARGV); @@ -40,3 +39,9 @@ sub candies { } return sum(@candy); } + +sub sum { + my($sum, @a) = @_; + $sum += $_ for @a; + return $sum; +} diff --git a/challenge-095/paulo-custodio/perl/ch-2.pl b/challenge-095/paulo-custodio/perl/ch-2.pl index c2cf2ae54c..b3eace4cb3 100644 --- a/challenge-095/paulo-custodio/perl/ch-2.pl +++ b/challenge-095/paulo-custodio/perl/ch-2.pl @@ -27,13 +27,20 @@ use 5.030; { package Stack; - use List::Util; sub new { my($class) = @_; return bless [], $class; } sub push { my($self, $n) = @_; push @$self, $n; } sub pop { my($self) = @_; pop @$self; } sub top { my($self) = @_; return $self->[-1]; } - sub min { my($self) = @_; return List::Util::min(@$self); } + sub min { my($self) = @_; return min_(@$self); } + + sub min_ { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; + } } my $stack = Stack->new; diff --git a/challenge-096/paulo-custodio/perl/ch-2.pl b/challenge-096/paulo-custodio/perl/ch-2.pl index 5d1b0be743..f2733fc227 100644 --- a/challenge-096/paulo-custodio/perl/ch-2.pl +++ b/challenge-096/paulo-custodio/perl/ch-2.pl @@ -30,9 +30,6 @@ use strict; use warnings; use 5.030; -use List::Util 'min'; - -use Data::Dump 'dump'; wag_fis_dist(@ARGV); @@ -112,3 +109,11 @@ sub wag_fis_dist { } } } + +sub min { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; +} diff --git a/challenge-096/paulo-custodio/perl/ch-2a.pl b/challenge-096/paulo-custodio/perl/ch-2a.pl index fb0fe511fa..22294b7e03 100644 --- a/challenge-096/paulo-custodio/perl/ch-2a.pl +++ b/challenge-096/paulo-custodio/perl/ch-2a.pl @@ -30,7 +30,6 @@ use strict; use warnings; use 5.030; -use List::Util 'min'; # avoid repeated recursive calls of lev_dist # for the Example 1 reduces from 29737 to 56 calls! @@ -58,3 +57,11 @@ sub lev_dist { ); } } + +sub min { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; +} -- cgit From 13a77a864078520a7df6a1638f37e07440e87250 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 00:58:37 +0100 Subject: Lua solution for week 97, part 2 --- challenge-097/abigail/README.md | 1 + challenge-097/abigail/lua/ch-2.lua | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-097/abigail/lua/ch-2.lua diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 10240df509..4db89a1982 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -68,6 +68,7 @@ Binary Substrings: ### Solutions * [AWK](awk/ch-2.awk) * [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-097/abigail/lua/ch-2.lua b/challenge-097/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..36a1a9098e --- /dev/null +++ b/challenge-097/abigail/lua/ch-2.lua @@ -0,0 +1,49 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua -s SECTIONS < input-file +-- + +-- +-- Parse option, and exit if incorrect +-- + +local sections = -1 +if #arg == 2 and arg [1] == "-s" +then sections = arg [2] + 0 +end + +if sections < 0 +then io . stderr : write ("Requires a '-s SECTION' option\n") + os . exit (1) +end + + +-- +-- Iterate over the input, line by line. +-- For each line, count the number of 0's on each position of the sections +-- Calculate the number of 1's. Add the minimum of the two to a running sum. +-- +for line in io . lines () do + local s_len = #line / sections + local sum = 0 + for i = 0, s_len - 1 do + local zeros = 0 + for j = 0, sections - 1 do + index = j * s_len + i + 1 + if string . sub (line, index, index) == "0" + then zeros = zeros + 1 + end + end + local ones = sections - zeros + if zeros < ones + then sum = sum + zeros + else sum = sum + ones + end + end + io . write (sum, "\n") +end -- cgit From 79d558dab08ac36e167743b18d8514d7967209ba Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Tue, 26 Jan 2021 19:09:11 -0500 Subject: 1st commit on 016_haskell --- challenge-016/stuart-little/haskell/ch-1.hs | 6 ++++ challenge-016/stuart-little/haskell/ch-2.hs | 43 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 challenge-016/stuart-little/haskell/ch-1.hs create mode 100755 challenge-016/stuart-little/haskell/ch-2.hs diff --git a/challenge-016/stuart-little/haskell/ch-1.hs b/challenge-016/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..d6127006c9 --- /dev/null +++ b/challenge-016/stuart-little/haskell/ch-1.hs @@ -0,0 +1,6 @@ +#!/usr/bin/env runghc + +-- run