From 1c7830764b45d39110d71a3879c2cc3f79afb922 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 22:18:40 +0100 Subject: Add challenge description to README.md. --- challenge-002/abigail/README | 1 - challenge-002/abigail/README.md | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) delete mode 100644 challenge-002/abigail/README create mode 100644 challenge-002/abigail/README.md (limited to 'challenge-002') diff --git a/challenge-002/abigail/README b/challenge-002/abigail/README deleted file mode 100644 index 5f0d73ae16..0000000000 --- a/challenge-002/abigail/README +++ /dev/null @@ -1 +0,0 @@ -Solution by Abigail diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md new file mode 100644 index 0000000000..cdabb3e041 --- /dev/null +++ b/challenge-002/abigail/README.md @@ -0,0 +1,24 @@ +# Solutions by Abigail + +## [Challenge #1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/#challenge-1) + +Write a script or one-liner to remove leading zeros from positive numbers. + +### Notes +We will be reading numbers from STDIN, and writing the results to STDOUT, +one number per line. + +We cannot have a number with just 0's, as that would not be a positive number. + +## [Challenge #2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/#challenge-2) + +Write a script that can convert integers to and from a base35 +representation, using the characters 0-9 and A-Y. Dave Jacoby came +up with nice description about +[base35](https://gist.github.com/jacoby/764bb4e8a5d3a819b5fbfa497fcb3454), +in case you needed some background. + +### Note +We will be reading numbers from STDIN, and writing the results to STDOUT, +one number per line. We'll use a prefix (T/F) to indicate whether the +number needs to be translated To of From base35. -- cgit From 54c3043ce21f7e5d2e16994eb162492f34a539d2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 01:55:07 +0100 Subject: Perl solution for week 2, part 1 --- challenge-002/abigail/perl/ch-1.pl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-002/abigail/perl/ch-1.pl (limited to 'challenge-002') diff --git a/challenge-002/abigail/perl/ch-1.pl b/challenge-002/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..08572c9fa6 --- /dev/null +++ b/challenge-002/abigail/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/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 +# + +say 0 + $_ for <>; -- cgit From 718e471a8169ceb2699d00d3e290191163ccba82 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 01:56:44 +0100 Subject: bc solution for week 2, part 1 --- challenge-002/abigail/bc/ch-1.bc | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 challenge-002/abigail/bc/ch-1.bc (limited to 'challenge-002') diff --git a/challenge-002/abigail/bc/ch-1.bc b/challenge-002/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..e69de29bb2 -- cgit From e63d8a08c0541506c0dd5fe1da703effa4e29520 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 01:58:25 +0100 Subject: AWK solution for week 2, part 1 --- challenge-002/abigail/awk/ch-1.awk | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-002/abigail/awk/ch-1.awk (limited to 'challenge-002') diff --git a/challenge-002/abigail/awk/ch-1.awk b/challenge-002/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..6f2ef2064c --- /dev/null +++ b/challenge-002/abigail/awk/ch-1.awk @@ -0,0 +1 @@ +{print 0 + $1} -- cgit From 00e3f31cb54718ce62e4cbe19eb83f7bab5d94d5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:09:00 +0100 Subject: Tests --- challenge-002/abigail/t/ctest.ini | 8 ++++++++ challenge-002/abigail/t/input-1-1 | 6 ++++++ challenge-002/abigail/t/input-1-2 | 6 ++++++ challenge-002/abigail/t/output-1-1.exp | 6 ++++++ challenge-002/abigail/t/output-1-2.exp | 6 ++++++ 5 files changed, 32 insertions(+) create mode 100644 challenge-002/abigail/t/ctest.ini create mode 100644 challenge-002/abigail/t/input-1-1 create mode 100644 challenge-002/abigail/t/input-1-2 create mode 100644 challenge-002/abigail/t/output-1-1.exp create mode 100644 challenge-002/abigail/t/output-1-2.exp (limited to 'challenge-002') diff --git a/challenge-002/abigail/t/ctest.ini b/challenge-002/abigail/t/ctest.ini new file mode 100644 index 0000000000..ab42e51d77 --- /dev/null +++ b/challenge-002/abigail/t/ctest.ini @@ -0,0 +1,8 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Numbers with leading zeros +1-2 = Numbers without leading zeros diff --git a/challenge-002/abigail/t/input-1-1 b/challenge-002/abigail/t/input-1-1 new file mode 100644 index 0000000000..390c6c18a3 --- /dev/null +++ b/challenge-002/abigail/t/input-1-1 @@ -0,0 +1,6 @@ +0124 +09823745 +01 +0000374 +000164234 +0000000000000000000000000000000000000000000000000000000000000000000000000000023 diff --git a/challenge-002/abigail/t/input-1-2 b/challenge-002/abigail/t/input-1-2 new file mode 100644 index 0000000000..dd0e917286 --- /dev/null +++ b/challenge-002/abigail/t/input-1-2 @@ -0,0 +1,6 @@ +124 +9823745 +1 +374 +164234 +23 diff --git a/challenge-002/abigail/t/output-1-1.exp b/challenge-002/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..dd0e917286 --- /dev/null +++ b/challenge-002/abigail/t/output-1-1.exp @@ -0,0 +1,6 @@ +124 +9823745 +1 +374 +164234 +23 diff --git a/challenge-002/abigail/t/output-1-2.exp b/challenge-002/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..dd0e917286 --- /dev/null +++ b/challenge-002/abigail/t/output-1-2.exp @@ -0,0 +1,6 @@ +124 +9823745 +1 +374 +164234 +23 -- cgit From b73b75793a4da255950c39fd2c43bc475e84cbc3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:08:06 +0100 Subject: Bash solution for week 2, part 1 --- challenge-002/abigail/bash/ch-1.sh | 15 +++++++++++++++ challenge-002/abigail/t/input-1-1 | 1 + challenge-002/abigail/t/input-1-2 | 1 + challenge-002/abigail/t/output-1-1.exp | 1 + challenge-002/abigail/t/output-1-2.exp | 1 + 5 files changed, 19 insertions(+) create mode 100644 challenge-002/abigail/bash/ch-1.sh (limited to 'challenge-002') diff --git a/challenge-002/abigail/bash/ch-1.sh b/challenge-002/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..b860cc4aa6 --- /dev/null +++ b/challenge-002/abigail/bash/ch-1.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +shopt -s extglob + +while read number +do echo "${number##+(0)}" +done diff --git a/challenge-002/abigail/t/input-1-1 b/challenge-002/abigail/t/input-1-1 index 390c6c18a3..43fa5651c0 100644 --- a/challenge-002/abigail/t/input-1-1 +++ b/challenge-002/abigail/t/input-1-1 @@ -4,3 +4,4 @@ 0000374 000164234 0000000000000000000000000000000000000000000000000000000000000000000000000000023 +0070102 diff --git a/challenge-002/abigail/t/input-1-2 b/challenge-002/abigail/t/input-1-2 index dd0e917286..62d6e16a35 100644 --- a/challenge-002/abigail/t/input-1-2 +++ b/challenge-002/abigail/t/input-1-2 @@ -4,3 +4,4 @@ 374 164234 23 +70102 diff --git a/challenge-002/abigail/t/output-1-1.exp b/challenge-002/abigail/t/output-1-1.exp index dd0e917286..62d6e16a35 100644 --- a/challenge-002/abigail/t/output-1-1.exp +++ b/challenge-002/abigail/t/output-1-1.exp @@ -4,3 +4,4 @@ 374 164234 23 +70102 diff --git a/challenge-002/abigail/t/output-1-2.exp b/challenge-002/abigail/t/output-1-2.exp index dd0e917286..62d6e16a35 100644 --- a/challenge-002/abigail/t/output-1-2.exp +++ b/challenge-002/abigail/t/output-1-2.exp @@ -4,3 +4,4 @@ 374 164234 23 +70102 -- cgit From 4ca25485489f69e87d354ce7351578a9c820ca84 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:34:18 +0100 Subject: C solution for week 2, part 1 --- challenge-002/abigail/c/ch-1.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-002/abigail/c/ch-1.c (limited to 'challenge-002') diff --git a/challenge-002/abigail/c/ch-1.c b/challenge-002/abigail/c/ch-1.c new file mode 100644 index 0000000000..0b2ac69353 --- /dev/null +++ b/challenge-002/abigail/c/ch-1.c @@ -0,0 +1,24 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o cc-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t strlen; + + while ((strlen = getline (&line, &len, stdin)) != -1) { + printf ("%lld\n", atoll (line)); + } + free (line); + + return (0); +} -- cgit From b3b3d15eb8ebb16373c5d258d0b87991145a69aa Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:41:24 +0100 Subject: Lua solution for week 2, part 1 --- challenge-002/abigail/lua/ch-1.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-002/abigail/lua/ch-1.lua (limited to 'challenge-002') diff --git a/challenge-002/abigail/lua/ch-1.lua b/challenge-002/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..a60dee614f --- /dev/null +++ b/challenge-002/abigail/lua/ch-1.lua @@ -0,0 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + io . write (tonumber (line), "\n") +end -- cgit From 1884c584e13f0e24c7239ee7d843ab287354ba0e Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:42:25 +0100 Subject: Node.js solution for week 2, part 1 --- challenge-002/abigail/node/ch-1.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 challenge-002/abigail/node/ch-1.js (limited to 'challenge-002') diff --git a/challenge-002/abigail/node/ch-1.js b/challenge-002/abigail/node/ch-1.js new file mode 100644 index 0000000000..5146e85366 --- /dev/null +++ b/challenge-002/abigail/node/ch-1.js @@ -0,0 +1,14 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (+_)) +; -- cgit From 0fc3be0e79ac2fcda6d0d557d22c998cb35722e1 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:44:16 +0100 Subject: Python solution for week 2, part 1 --- challenge-002/abigail/python/ch-1.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 challenge-002/abigail/python/ch-1.py (limited to 'challenge-002') diff --git a/challenge-002/abigail/python/ch-1.py b/challenge-002/abigail/python/ch-1.py new file mode 100644 index 0000000000..de6839af0a --- /dev/null +++ b/challenge-002/abigail/python/ch-1.py @@ -0,0 +1,14 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + print int (line) -- cgit From ef294b92e27547a1188cf216c2217586f596c822 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:46:19 +0100 Subject: Ruby solution for week 2, part 1 --- challenge-002/abigail/ruby/ch-1.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-002/abigail/ruby/ch-1.rb (limited to 'challenge-002') diff --git a/challenge-002/abigail/ruby/ch-1.rb b/challenge-002/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..0ef00293d6 --- /dev/null +++ b/challenge-002/abigail/ruby/ch-1.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| + puts _ . to_i +end -- cgit From 298f7b19eff08c0945fe3c1134df51937b6be332 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 02:51:15 +0100 Subject: List of solutions for week 2, part 1 --- challenge-002/abigail/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index cdabb3e041..46fcc91c75 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -10,6 +10,17 @@ one number per line. We cannot have a number with just 0's, as that would not be a positive number. +### Solutions +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) +* [bc](bc/ch-1.bc) +* [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-002/#challenge-2) Write a script that can convert integers to and from a base35 -- cgit From 06cd8a5c7b63f97da1c6c9e495b362b8cf86305f Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 03:32:10 +0100 Subject: Befunge-93 solution for week 2, part 1 --- challenge-002/abigail/README.md | 1 + challenge-002/abigail/befunge-93/ch-1.bf93 | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 challenge-002/abigail/befunge-93/ch-1.bf93 (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 46fcc91c75..86d5f256da 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -14,6 +14,7 @@ We cannot have a number with just 0's, as that would not be a positive number. * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) +* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) diff --git a/challenge-002/abigail/befunge-93/ch-1.bf93 b/challenge-002/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..d5a09a4915 --- /dev/null +++ b/challenge-002/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,2 @@ +~:1+!#@_>:"0"-!#v_>:,52*-#v_ + ^ ~$< ^ ~< -- cgit From a7235df7aa6393d25fb9c1cf9378ca021e948ae9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 17:55:09 +0100 Subject: Tests for week 2, part 2 --- challenge-002/abigail/t/ctest.ini | 10 ++++++++++ challenge-002/abigail/t/input-2-1 | 7 +++++++ challenge-002/abigail/t/input-2-2 | 7 +++++++ challenge-002/abigail/t/input-2-3 | 7 +++++++ challenge-002/abigail/t/input-2-4 | 7 +++++++ challenge-002/abigail/t/output-2-1.exp | 7 +++++++ challenge-002/abigail/t/output-2-2.exp | 7 +++++++ challenge-002/abigail/t/output-2-3.exp | 7 +++++++ challenge-002/abigail/t/output-2-4.exp | 7 +++++++ 9 files changed, 66 insertions(+) create mode 100644 challenge-002/abigail/t/input-2-1 create mode 100644 challenge-002/abigail/t/input-2-2 create mode 100644 challenge-002/abigail/t/input-2-3 create mode 100644 challenge-002/abigail/t/input-2-4 create mode 100644 challenge-002/abigail/t/output-2-1.exp create mode 100644 challenge-002/abigail/t/output-2-2.exp create mode 100644 challenge-002/abigail/t/output-2-3.exp create mode 100644 challenge-002/abigail/t/output-2-4.exp (limited to 'challenge-002') diff --git a/challenge-002/abigail/t/ctest.ini b/challenge-002/abigail/t/ctest.ini index ab42e51d77..ae75eb3ad3 100644 --- a/challenge-002/abigail/t/ctest.ini +++ b/challenge-002/abigail/t/ctest.ini @@ -6,3 +6,13 @@ [names] 1-1 = Numbers with leading zeros 1-2 = Numbers without leading zeros +2-1 = Base 10 to base 35 +2-2 = Base 35 to base 10 +2-3 = Base 35 to base 10 +2-4 = Base 10 to base 35 + +[2-1,2-4] +args = -t + +[2-2,2-3] +args = -f diff --git a/challenge-002/abigail/t/input-2-1 b/challenge-002/abigail/t/input-2-1 new file mode 100644 index 0000000000..becb05430c --- /dev/null +++ b/challenge-002/abigail/t/input-2-1 @@ -0,0 +1,7 @@ +1 +10 +100 +1000 +10000 +100000 +1000000 diff --git a/challenge-002/abigail/t/input-2-2 b/challenge-002/abigail/t/input-2-2 new file mode 100644 index 0000000000..becb05430c --- /dev/null +++ b/challenge-002/abigail/t/input-2-2 @@ -0,0 +1,7 @@ +1 +10 +100 +1000 +10000 +100000 +1000000 diff --git a/challenge-002/abigail/t/input-2-3 b/challenge-002/abigail/t/input-2-3 new file mode 100644 index 0000000000..61952fba2d --- /dev/null +++ b/challenge-002/abigail/t/input-2-3 @@ -0,0 +1,7 @@ +1 +A +2U +SK +85P +2BM5 +NBBF diff --git a/challenge-002/abigail/t/input-2-4 b/challenge-002/abigail/t/input-2-4 new file mode 100644 index 0000000000..156338e8ad --- /dev/null +++ b/challenge-002/abigail/t/input-2-4 @@ -0,0 +1,7 @@ +1 +35 +1225 +42875 +1500625 +52521875 +1838265625 diff --git a/challenge-002/abigail/t/output-2-1.exp b/challenge-002/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..61952fba2d --- /dev/null +++ b/challenge-002/abigail/t/output-2-1.exp @@ -0,0 +1,7 @@ +1 +A +2U +SK +85P +2BM5 +NBBF diff --git a/challenge-002/abigail/t/output-2-2.exp b/challenge-002/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..156338e8ad --- /dev/null +++ b/challenge-002/abigail/t/output-2-2.exp @@ -0,0 +1,7 @@ +1 +35 +1225 +42875 +1500625 +52521875 +1838265625 diff --git a/challenge-002/abigail/t/output-2-3.exp b/challenge-002/abigail/t/output-2-3.exp new file mode 100644 index 0000000000..becb05430c --- /dev/null +++ b/challenge-002/abigail/t/output-2-3.exp @@ -0,0 +1,7 @@ +1 +10 +100 +1000 +10000 +100000 +1000000 diff --git a/challenge-002/abigail/t/output-2-4.exp b/challenge-002/abigail/t/output-2-4.exp new file mode 100644 index 0000000000..becb05430c --- /dev/null +++ b/challenge-002/abigail/t/output-2-4.exp @@ -0,0 +1,7 @@ +1 +10 +100 +1000 +10000 +100000 +1000000 -- cgit From 0e2c118dc5236673d8a93eef788c760fe9d73983 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 24 Jan 2021 17:57:16 +0100 Subject: Perl solution for week 2, part 2 --- challenge-002/abigail/README.md | 7 +++-- challenge-002/abigail/perl/ch-2.pl | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 challenge-002/abigail/perl/ch-2.pl (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 86d5f256da..65d0acfddf 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -32,5 +32,8 @@ in case you needed some background. ### Note We will be reading numbers from STDIN, and writing the results to STDOUT, -one number per line. We'll use a prefix (T/F) to indicate whether the -number needs to be translated To of From base35. +one number per line. Programs will use an option, -t (to base 35), or +-f (from base 35) to indicate which direction to go. + +### Solutions +* [Perl](perl/ch-2.pl) diff --git a/challenge-002/abigail/perl/ch-2.pl b/challenge-002/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..d02cf2a999 --- /dev/null +++ b/challenge-002/abigail/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +use Getopt::Long; +GetOptions ('t' => \my $to_base, + 'f' => \my $from_base); + +die "Need exactly one of -t or -f" unless $to_base xor $from_base; + +my $BASE = 35; + +my @digits = (0 .. 9, 'A' .. 'Y'); +my %to; +@to {keys @digits} = @digits; +my %from = reverse %to; + + +sub to_base ($number) { + my $out = ""; + while ($number) { + $out = $to {$number % $BASE} . $out; + $number = int ($number / $BASE); + } + $out || "0"; +} + +sub from_base ($number) { + my $out = 0; + while (length $number) { + my $digit = substr $number, 0, 1, ""; + $out *= $BASE; + $out += $from {$digit}; + } + $out; +} + +while (my $number = <>) { + chomp $number; + say $to_base ? to_base $number : from_base $number +} + +__END__ -- cgit From d0672c43f3f086676e24956d37a97ccc1a3a3ae1 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 00:29:18 +0100 Subject: Node solution for week 2, part 2 --- challenge-002/abigail/README.md | 2 + challenge-002/abigail/node/ch-2.js | 84 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 challenge-002/abigail/node/ch-2.js (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 65d0acfddf..296d886967 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -36,4 +36,6 @@ one number per line. Programs will use an option, -t (to base 35), or -f (from base 35) to indicate which direction to go. ### Solutions +* [AWK](awk/ch-2.awk) +* [Node](node/ch-2.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-002/abigail/node/ch-2.js b/challenge-002/abigail/node/ch-2.js new file mode 100644 index 0000000000..84d0f6976e --- /dev/null +++ b/challenge-002/abigail/node/ch-2.js @@ -0,0 +1,84 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js {-f | -t} < input-file +// +// -f: Translate from base 35 to base 10 +// -t: Translate to base 35 from base 10 +// + +let BASE = 35 + +// +// Parse options using the yargs module +// +const argv = require ('yargs') +. option ('from_base', { + alias: 'f', + type: 'boolean', +}) +. option ('to_base', { + alias: 't', + type: 'boolean', +}) +. argv; + +// +// Check options +// +if ((argv . to_base ? 1 : 0) + (argv . from_base ? 1 : 0) != 1) { + console . log ("Requires exactly one of '-f' or '-t'") + process . exit (1) +} + +// +// Set up digits, mapping base-10 numbers to base-35 digits, and vice versa +// +let digits = {}; +for (let i = 0; i < 10; i ++) { + digits [i] = i +} +for (let i = 10; i < BASE; i ++) { + let char = String . fromCharCode (65 + i - 10) + digits [char] = i + digits [i] = char +} + +// +// Translate to base 35 +// +function to_base (number) { + let out = ""; + while (number) { + let n = number % BASE + out = digits [n] + out + number = Math . floor (number / BASE) + } + return out +} + +// +// Translate from base 35 +// +function from_base (base) { + let out = 0 + base . split ('') . forEach (c => { + out = BASE * out + digits [c] + }) + return out +} + + +// +// Iterate over the input, call either to_base or from_base for each +// line, depending on the -f or -t parameter. +// +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (argv . to_base ? to_base (+ _) + : from_base (_ . trim ()))) +; -- cgit From e589f2892916894b3e404ba98fed1236886cb8a9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 01:59:41 +0100 Subject: Lua solution for week 2, part 2 --- challenge-002/abigail/README.md | 1 + challenge-002/abigail/lua/ch-2.lua | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-002/abigail/lua/ch-2.lua (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 296d886967..10b469478b 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -37,5 +37,6 @@ one number per line. Programs will use an option, -t (to base 35), or ### Solutions * [AWK](awk/ch-2.awk) +* [Lua](lua/ch-2.lua) * [Node](node/ch-2.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-002/abigail/lua/ch-2.lua b/challenge-002/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..789e2a0173 --- /dev/null +++ b/challenge-002/abigail/lua/ch-2.lua @@ -0,0 +1,82 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua {-f | -t} < input-file +-- +-- -f: Translate from base 35 to base 10 +-- -t: Translate to base 35 from base 10 +-- + +local do_to_base = 0 +local do_from_base = 0 +local BASE = 35 + +-- +-- Parse option, and exit if incorrect +-- +if #arg == 1 +then if arg [1] == "-f" + then do_from_base = 1 + end + if arg [1] == "-t" + then do_to_base = 1 + end +end +if do_to_base + do_from_base ~= 1 +then io . stderr : write ("Need exactly one of '-f' or '-t'\n") + os . exit (1) +end + +-- +-- Create an array, mapping base 10 numbers to base 35 digits, and vice versa +-- +local digits = {} +for i = 0, 9 do + digits [i] = i + digits [i .. ""] = i +end +for i = 10, BASE - 1 do + local char = string . char (65 + i - 10); + digits [char] = i; + digits [i] = char; + digits [i .. ""] = char; +end + +-- +-- Take a (base 10) number, turn it to a base 35 number +-- +function to_base (number) + local out = '' + while number > 0 do + local n = number % BASE + out = digits [n] .. out + number = math . floor (number / BASE) + end + return out +end + +-- +-- Take a base 35 number (as a string), turn it to a base 10 number +-- +function from_base (number) + local out = 0 + for char in number : gmatch "." do + out = out * BASE + tonumber (digits [char]) + end + return out +end + +-- +-- Iterate over the input, call to_base/from_base depending +-- on the command line option. +-- +for line in io . lines () do + if do_to_base == 1 + then io . write (to_base (tonumber (line)), "\n") + else io . write (from_base (line), "\n") + end +end -- cgit From e17a558b2ebc880537e1743945304532eb2263bb Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 17:37:17 +0100 Subject: Python solution for week 2, part 2 --- challenge-002/abigail/README.md | 2 ++ challenge-002/abigail/python/ch-2.py | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 challenge-002/abigail/python/ch-2.py (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 10b469478b..4642bfbfa6 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -40,3 +40,5 @@ one number per line. Programs will use an option, -t (to base 35), or * [Lua](lua/ch-2.lua) * [Node](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.by) diff --git a/challenge-002/abigail/python/ch-2.py b/challenge-002/abigail/python/ch-2.py new file mode 100644 index 0000000000..22fe4fcaf0 --- /dev/null +++ b/challenge-002/abigail/python/ch-2.py @@ -0,0 +1,63 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-2.py {-f | -t} < input-file +# + +import fileinput +import sys +import getopt + +BASE = 35 + +# +# Parse options +# +do_to_base = 0 +do_from_base = 0 +opts, args = getopt . getopt (sys . argv [1:], 'ft') + +for opt, val in opts: + if opt == "-f": + do_from_base = 1 + elif opt == "-t": + do_to_base = 1 + +if do_to_base + do_from_base != 1: + print "Need exactly one of -f or -t" + sys . exit (1) + + +# +# Translate a base 10 number to base 35 +# +def to_base (number): + out = "" + while number > 0: + rest = number % BASE + if rest < 10: + char = str (rest) + else: + char = chr (65 + rest - 10) + out = char + out + number = int (number / BASE) + return out + + +# +# Translate a number from base BASE to base 10 +# +def from_base (number): + return int (number, BASE) + +# +# Need to clean argv, else fileinput will try to open a file +# +sys . argv [1:] = [] + +for line in fileinput . input (): + print from_base (line) if do_from_base else to_base (int (line)) -- cgit From 73adfbabd45d6dcced17f410e123349a2b7150a5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 17:44:14 +0100 Subject: Ruby solution for week 2, part 2 --- challenge-002/abigail/ruby/ch-2.rb | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-002/abigail/ruby/ch-2.rb (limited to 'challenge-002') diff --git a/challenge-002/abigail/ruby/ch-2.rb b/challenge-002/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..95efc096a7 --- /dev/null +++ b/challenge-002/abigail/ruby/ch-2.rb @@ -0,0 +1,50 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb {-f | -t} < input-file +# + +require 'optparse' + +BASE = 35 + +# +# Parse and validate options +# +params = ARGV . getopts ('ft') +do_from_base = params ["f"] ? 1 : 0 +do_to_base = params ["t"] ? 1 : 0 +if do_from_base + do_to_base != 1 + STDERR . puts "Program requires exactly one of '-f' or '-t'" + exit 1 +end + + +def to_base (number) + out = "" + while number > 0 do + rest = number % BASE + if rest < 10 + then char = rest . to_s + else char = (65 + rest - 10) . chr + end + out = char + out + number = (number / BASE) . to_i + end + return out +end + +def from_base (number) + return number . to_i (35) +end + +ARGF . each_line do |_| + if do_from_base == 1 + then puts from_base (_) + else puts to_base (_ . to_i) + end +end -- cgit From 60e6a137f0c65f3225da1260aee8abae08d69ac4 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 17:46:09 +0100 Subject: AWK solution for week 2, part 2 --- challenge-002/abigail/awk/ch-2.awk | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 challenge-002/abigail/awk/ch-2.awk (limited to 'challenge-002') diff --git a/challenge-002/abigail/awk/ch-2.awk b/challenge-002/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..b171fcd08f --- /dev/null +++ b/challenge-002/abigail/awk/ch-2.awk @@ -0,0 +1,80 @@ +#!/usr/bin/awk +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk {-f | -t} < input-file +# +# -f: Translate from base 35 to base 10 +# -t: Translate to base 35 from base 10 +# + +BEGIN { + # + # Parse command line + # + from_base = 0; + to_base = 0; + for (i = 0; i < ARGC; i ++) { + if (ARGV [i] == "-f") { + from_base = 1 + } + if (ARGV [i] == "-t") { + to_base = 1 + } + } + ARGC = 0 # Prevent AWK to process the parameters. + + # + # Map base-35 digits to base-10 numbers, and back + # + BASE = 35 + for (i = 0; i < 10; i ++) { + digits [i] = i + } + for (i = 10; i < BASE; i ++) { + char = sprintf("%c", 65 + i - 10) + digits [i] = char + digits [char] = i + } +} + +# +# This is only executed if the -t option is used +# +to_base { + # + # Translate the input number from base 10 to base BASE, + # using a standard mod and divide approach. + # + number = $0 + out = "" + while (number > 0) { + digit = number % BASE + out = digits [digit] out + number = int(number / BASE) + } +} + +# +# This is only executed if the -f option is used +# +from_base { + # + # Translate the input number from base BASE to base 10, + # using a standard multiply and add approach. + # + out = 0 + n = split ($0, d, "") + for (i = 1; i <= n; i ++) { + out = BASE * out + digits [d [i]] + } +} + +# +# Always executed +# +{ + print out +} -- cgit From 6db5313239e2e5996431e9bb4d81619644885cd6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 17:50:01 +0100 Subject: Fix typo --- challenge-002/abigail/c/ch-1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-002') diff --git a/challenge-002/abigail/c/ch-1.c b/challenge-002/abigail/c/ch-1.c index 0b2ac69353..09eb2ebf82 100644 --- a/challenge-002/abigail/c/ch-1.c +++ b/challenge-002/abigail/c/ch-1.c @@ -7,7 +7,7 @@ */ /* - * Run as: cc -o ch-1.o cc-1.c; ./ch-1.o < input-file + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file */ int main (void) { -- cgit From 4d628f1e0675144dddb903665fd94040fa49b832 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 19:21:08 +0100 Subject: C solution for week 2, part 2 --- challenge-002/abigail/README.md | 1 + challenge-002/abigail/c/ch-2.c | 106 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 challenge-002/abigail/c/ch-2.c (limited to 'challenge-002') diff --git a/challenge-002/abigail/README.md b/challenge-002/abigail/README.md index 4642bfbfa6..bb5cb18c8f 100644 --- a/challenge-002/abigail/README.md +++ b/challenge-002/abigail/README.md @@ -37,6 +37,7 @@ one number per line. Programs will use an option, -t (to base 35), or ### Solutions * [AWK](awk/ch-2.awk) +* [C](c/ch-2.c) * [Lua](lua/ch-2.lua) * [Node](node/ch-2.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-002/abigail/c/ch-2.c b/challenge-002/abigail/c/ch-2.c new file mode 100644 index 0000000000..20cdd146d6 --- /dev/null +++ b/challenge-002/abigail/c/ch-2.c @@ -0,0 +1,106 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o {-f | -t} < input-file + * + * -f: Translate from base 35 to base 10 + * -t: Translate to base 35 from base 35 + */ + +int BASE = 35; + +/* + * Convert from base 35 to base 10 + */ +long long from_base (char * number) { + long long out = 0; + while (* number) { + char ch = * number ++; + int n = ch <= '9' ? ch - '0' : ch - 'A' + 10; + out *= BASE; + out += n; + } + return (out); +} + +/* + * Convert to base 35 from base 10 + */ +char * to_base (long long number) { + char * out; + if ((out = (char *) malloc (64 * sizeof (char))) == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + size_t i = 0; + while (number > 0) { + int rest = number % BASE; + out [i ++] = rest < 10 ? '0' + rest + : 'A' + rest - 10; + number = number / BASE; + } + out [i] = '\0'; + + /* + * Reverse the string + */ + for (size_t j = 0; 2 * j < i; j ++) { + char t = out [j]; + out [j] = out [i - j - 1]; + out [i - j - 1] = t; + } + + return (out); +} + +int main (int argc, char ** argv) { + char * line = NULL; + size_t len = 0; + size_t strlen; + + int do_to_base = 0; + int do_from_base = 0; + char ch; + + /* + * Parse and validate options + */ + while ((ch = getopt (argc, argv, "tf")) != -1) { + switch (ch) { + case 't': + do_to_base = 1; + break; + case 'f': + do_from_base = 1; + break; + } + } + + if (do_to_base + do_from_base != 1) { + fprintf (stderr, "Need exactly one of -f or -t options\n"); + exit (1); + } + + while ((strlen = getline (&line, &len, stdin)) != -1) { + line [strlen - 1] = '\0'; /* Chop off newline */ + if (do_from_base) { + printf ("%lld\n", from_base (line)); + } + if (do_to_base) { + char * out = to_base (atoll (line)); + printf ("%s\n", out); + free (out); + } + char * line_ptr = line; + } + free (line); + + return (0); +} -- cgit From 9aeed56c942517679d2e97deea955c6f72abd70a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Jan 2021 19:28:12 +0100 Subject: Only use one one hash to map digits. --- challenge-002/abigail/perl/ch-2.pl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'challenge-002') diff --git a/challenge-002/abigail/perl/ch-2.pl b/challenge-002/abigail/perl/ch-2.pl index d02cf2a999..a9b9c006ba 100644 --- a/challenge-002/abigail/perl/ch-2.pl +++ b/challenge-002/abigail/perl/ch-2.pl @@ -25,17 +25,19 @@ die "Need exactly one of -t or -f" unless $to_base xor $from_base; my $BASE = 35; -my @digits = (0 .. 9, 'A' .. 'Y'); -my %to; -@to {keys @digits} = @digits; -my %from = reverse %to; - +my %digits; +$digits {$_} = $_ for 0 .. 9; +foreach my $n (10 .. $BASE - 1) { + my $ch = chr (65 + $n - 10); + $digits {$ch} = $n; + $digits {$n} = $ch; +} sub to_base ($number) { my $out = ""; while ($number) { - $out = $to {$number % $BASE} . $out; - $number = int ($number / $BASE); + $out = $digits {$number % $BASE} . $out; + $number = int ($number / $BASE); } $out || "0"; } @@ -45,7 +47,7 @@ sub from_base ($number) { while (length $number) { my $digit = substr $number, 0, 1, ""; $out *= $BASE; - $out += $from {$digit}; + $out += $digits {$digit}; } $out; } -- cgit From c402c40f216b437ab1aba13793d493eb25cbfd14 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 25 Jan 2021 20:07:40 +0000 Subject: Replace tabs by spaces so that indentation looks correct --- challenge-002/paulo-custodio/perl/ch-1.pl | 2 +- challenge-002/paulo-custodio/perl/ch-2.pl | 56 +++++++++++++++---------------- challenge-002/paulo-custodio/test.pl | 34 +++++++++---------- 3 files changed, 46 insertions(+), 46 deletions(-) (limited to 'challenge-002') diff --git a/challenge-002/paulo-custodio/perl/ch-1.pl b/challenge-002/paulo-custodio/perl/ch-1.pl index cbe49c0da7..154ae4402e 100644 --- a/challenge-002/paulo-custodio/perl/ch-1.pl +++ b/challenge-002/paulo-custodio/perl/ch-1.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl # Challenge 002 -# +# # Challenge #1 # Write a script or one-liner to remove leading zeros from positive numbers. diff --git a/challenge-002/paulo-custodio/perl/ch-2.pl b/challenge-002/paulo-custodio/perl/ch-2.pl index db37e40e4c..ea000b6ffd 100644 --- a/challenge-002/paulo-custodio/perl/ch-2.pl +++ b/challenge-002/paulo-custodio/perl/ch-2.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl # Challenge 002 -# +# # Challenge #2 # Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y. Dave Jacoby came up with nice description about base35, in case you needed some background. @@ -15,41 +15,41 @@ my @digits = ('0'..'9','A'..'Z'); our $opt_r; (getopts('r') && @ARGV==1) or die "Usage: ch-2.pl [-r] number\n"; if ($opt_r) { - say scan_base($ARGV[0], 35); + say scan_base($ARGV[0], 35); } else { - say print_base($ARGV[0], 35); + say print_base($ARGV[0], 35); } sub print_base { - my($n, $base) = @_; - my $negative = ($n < 0); - $n = abs($n); - my $output = ''; - do { - my $d = $n % $base; - $n = int($n / $base); - $output = $digits[$d].$output; - } while ($n > 0); - $output = "-".$output if $negative; - return $output; + my($n, $base) = @_; + my $negative = ($n < 0); + $n = abs($n); + my $output = ''; + do { + my $d = $n % $base; + $n = int($n / $base); + $output = $digits[$d].$output; + } while ($n > 0); + $output = "-".$output if $negative; + return $output; } sub scan_base { - my($str, $base) = @_; - my $n = 0; - my $negative; - $negative = 1 if $str =~ s/^-//; - while ($str =~ s/^(\w)//) { - my $c = $1; - my $d = ($c =~ /\d/) ? 0+$c : ord(uc($c))-ord('A')+10; - $d < $base or die "cannot parse $c$str\n"; - $n = $base * $n + $d; - } - $str eq '' or die "cannot parse $str\n"; - $n = -$n if $negative; - return $n; + my($str, $base) = @_; + my $n = 0; + my $negative; + $negative = 1 if $str =~ s/^-//; + while ($str =~ s/^(\w)//) { + my $c = $1; + my $d = ($c =~ /\d/) ? 0+$c : ord(uc($c))-ord('A')+10; + $d < $base or die "cannot parse $c$str\n"; + $n = $base * $n + $d; + } + $str eq '' or die "cannot parse $str\n"; + $n = -$n if $negative; + return $n; } - \ No newline at end of file + diff --git a/challenge-002/paulo-custodio/test.pl b/challenge-002/paulo-custodio/test.pl index c322545716..e3ca75b1c3 100644 --- a/challenge-002/paulo-custodio/test.pl +++ b/challenge-002/paulo-custodio/test.pl @@ -6,27 +6,27 @@ use Test::More; use 5.030; is capture("perl perl/ch-1.pl 000123"), "123\n"; -is capture("perl perl/ch-1.pl 123"), "123\n"; +is capture("perl perl/ch-1.pl 123"), "123\n"; -is capture("perl perl/ch-2.pl 0"), "0\n"; -is capture("perl perl/ch-2.pl 1"), "1\n"; -is capture("perl perl/ch-2.pl 10"), "A\n"; -is capture("perl perl/ch-2.pl 34"), "Y\n"; -is capture("perl perl/ch-2.pl 35"), "10\n"; -is capture("perl perl/ch-2.pl -- -35"), "-10\n"; +is capture("perl perl/ch-2.pl 0"), "0\n"; +is capture("perl perl/ch-2.pl 1"), "1\n"; +is capture("perl perl/ch-2.pl 10"), "A\n"; +is capture("perl perl/ch-2.pl 34"), "Y\n"; +is capture("perl perl/ch-2.pl 35"), "10\n"; +is capture("perl perl/ch-2.pl -- -35"), "-10\n"; -is capture("perl perl/ch-2.pl -r 0"), "0\n"; -is capture("perl perl/ch-2.pl -r 1"), "1\n"; -is capture("perl perl/ch-2.pl -r A"), "10\n"; -is capture("perl perl/ch-2.pl -r Y"), "34\n"; -is capture("perl perl/ch-2.pl -r 10"), "35\n"; -is capture("perl perl/ch-2.pl -r -- -10"), "-35\n"; +is capture("perl perl/ch-2.pl -r 0"), "0\n"; +is capture("perl perl/ch-2.pl -r 1"), "1\n"; +is capture("perl perl/ch-2.pl -r A"), "10\n"; +is capture("perl perl/ch-2.pl -r Y"), "34\n"; +is capture("perl perl/ch-2.pl -r 10"), "35\n"; +is capture("perl perl/ch-2.pl -r -- -10"), "-35\n"; done_testing; sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \t\v\f\r]*\n/\n/g; + return $out; } -- cgit