diff options
| author | Abigail <abigail@abigail.be> | 2021-06-09 18:03:02 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-06-09 20:15:02 +0200 |
| commit | 3fe9fba9c9d2ec298020988b59ddf16dade7285f (patch) | |
| tree | 2c95969649117c985d1a5cc9c753be01abf32f29 | |
| parent | ef7ce9bd5bf986bd4f2d99e082013e5f895f0d9e (diff) | |
| download | perlweeklychallenge-club-3fe9fba9c9d2ec298020988b59ddf16dade7285f.tar.gz perlweeklychallenge-club-3fe9fba9c9d2ec298020988b59ddf16dade7285f.tar.bz2 perlweeklychallenge-club-3fe9fba9c9d2ec298020988b59ddf16dade7285f.zip | |
AWK, Bash, Lua, Node.js, Perl, Python, Ruby solutions for week 116, part 1
| -rw-r--r-- | challenge-116/abigail/README.md | 7 | ||||
| -rw-r--r-- | challenge-116/abigail/awk/ch-1.awk | 37 | ||||
| -rw-r--r-- | challenge-116/abigail/bash/ch-1.sh | 50 | ||||
| -rw-r--r-- | challenge-116/abigail/lua/ch-1.lua | 39 | ||||
| -rw-r--r-- | challenge-116/abigail/node/ch-1.js | 41 | ||||
| -rw-r--r-- | challenge-116/abigail/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-116/abigail/python/ch-1.py | 33 | ||||
| -rw-r--r-- | challenge-116/abigail/ruby/ch-1.rb | 38 | ||||
| -rw-r--r-- | challenge-116/abigail/t/ctest.ini | 1 | ||||
| -rw-r--r-- | challenge-116/abigail/t/input-1-2 | 4 | ||||
| -rw-r--r-- | challenge-116/abigail/t/output-1-2.exp | 4 |
11 files changed, 322 insertions, 0 deletions
diff --git a/challenge-116/abigail/README.md b/challenge-116/abigail/README.md index a68ef6430c..eccf1a6b3b 100644 --- a/challenge-116/abigail/README.md +++ b/challenge-116/abigail/README.md @@ -22,6 +22,13 @@ Output: 10203 as it is impossible to split satisfying the conditions. ~~~~ ### Solutions +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) +* [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 [Number Sequence](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-116-1.html) diff --git a/challenge-116/abigail/awk/ch-1.awk b/challenge-116/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..14f6a2ee56 --- /dev/null +++ b/challenge-116/abigail/awk/ch-1.awk @@ -0,0 +1,37 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +function make_chain (string, start) { + if (start == string) { + return start + } + if (1 == index (string, start)) { + tail = substr (string, length (start) + 1) + result = make_chain(tail, start + 1) + if (result == "") { + result = make_chain(tail, start - 1) + } + if (result == "") { + return "" + } + return start "," result + } + return "" +} + +{ + for (i = 1; i <= length ($1); i ++) { + result = make_chain($1, substr ($1, 1, i)) + if (result != "") { + print result + next + } + } +} diff --git a/challenge-116/abigail/bash/ch-1.sh b/challenge-116/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..8aed90c969 --- /dev/null +++ b/challenge-116/abigail/bash/ch-1.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +out="" + +function make_chain () { + local string=$1 + local start=$2 + if [[ "$string" = "$start" ]] + then out=$start + return + fi + + if [[ $string =~ ^$start ]] + then local tail=${string:${#start}} + make_chain $tail $((start + 1)) + if [[ $out =~ ^. ]] + then out=$start,$out + return + fi + make_chain $tail $((start - 1)) + if [[ $out =~ ^. ]] + then out=$start,$out + return + fi + fi + out="" + return +} + + +while read string +do for ((i = 1; i <= ${#string}; i ++)) + do start=${string:0:$i} + make_chain $string $start + if [[ $out =~ ^. ]] + then echo $out + break + fi + done +done diff --git a/challenge-116/abigail/lua/ch-1.lua b/challenge-116/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..df551a2e46 --- /dev/null +++ b/challenge-116/abigail/lua/ch-1.lua @@ -0,0 +1,39 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +function make_chain (string, start) + if string == start + then return start + end + if string : find ("^" .. start) + then tail = string : sub (#start + 1, -1) + result = make_chain (tail, tostring (tonumber (start) + 1)) + if result ~= nil + then return start .. "," .. result + end + result = make_chain (tail, tostring (tonumber (start) - 1)) + if result ~= nil + then return start .. "," .. result + end + end + return nil +end + +for line in io . lines () do + for i = 1, #line do + start = line : sub (1, i) + result = make_chain (line, start) + if result ~= nil + then print (result) + goto end_main + end + end + ::end_main:: +end diff --git a/challenge-116/abigail/node/ch-1.js b/challenge-116/abigail/node/ch-1.js new file mode 100644 index 0000000000..e4bcb1961e --- /dev/null +++ b/challenge-116/abigail/node/ch-1.js @@ -0,0 +1,41 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => main (line)) + +function make_chain (string, start) { + if (string == start) { + return [start] + } + if (0 == string . indexOf (start)) { + let tail = string . substr (start . length) + let result = make_chain (tail, (+ start + 1) . toString ()) || + make_chain (tail, (+ start - 1) . toString ()) + if (result) { + result . unshift (start) + return result + } + } + return null +} + + +function main (line) { + for (let i = 1; i <= line . length; i ++) { + let start = line . substr (0, i) + let result = make_chain (line, start) + if (result) { + console . log (result . join (",")) + break + } + } +} diff --git a/challenge-116/abigail/perl/ch-1.pl b/challenge-116/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..6fbf27a906 --- /dev/null +++ b/challenge-116/abigail/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/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 +# + +# +# Given a chain and a starting number, check whether we can form a +# chain. We will return a list of parts if succes. Note: +# 1) If the start is the same as the string, we return a single +# item list. +# 2) We return the list in reverse order. Due to Perls memory +# management, N pushes will require O (log N) calls to +# malloc, and O (N) memory shuffles. This is worse if we +# use N unshifts. +# +# Note the running time is O (N), despite recursing twice. This is +# because there is no number X such that X + 1 and X - 1 are the same +# number. Hence, in at least one of the recursive make_chain calls the +# 'start' will not match the beginning of 'string'. +# + +sub make_chain ($string, $start) { + if ($string eq $start) { + return [$start] + } + if (index ($string, $start) == 0) { + my $tail = substr $string, length $start; + my $rest; + if (($rest = make_chain ($tail, $start + 1)) || + ($rest = make_chain ($tail, $start - 1))) { + push @$rest => $start; + return $rest; + } + } + return; +} + + +INPUT: while (<>) { + chomp; + for my $i (1 .. length) { + # + # Try to make a chain with each possible start. + # + my $result = make_chain $_, substr $_, 0, $i; + if ($result) { + say join "," => reverse @$result; + next INPUT; + } + } +} + + +__END__ diff --git a/challenge-116/abigail/python/ch-1.py b/challenge-116/abigail/python/ch-1.py new file mode 100644 index 0000000000..795d4ecabd --- /dev/null +++ b/challenge-116/abigail/python/ch-1.py @@ -0,0 +1,33 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +def make_chain (string, start): + if string == start: + return [start] + + if 0 == string . find (start): + tail = string [len (start) :] + result = make_chain (tail, str (int (start) + 1)) or \ + make_chain (tail, str (int (start) - 1)) + if result: + return [start] + result + + return None + + +for line in fileinput . input (): + line = line . strip () + for i in range (0, len (line)): + result = make_chain (line, line [0 : i + 1]) + if result: + print ("," . join (result)) + break diff --git a/challenge-116/abigail/ruby/ch-1.rb b/challenge-116/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..a8e5c0d824 --- /dev/null +++ b/challenge-116/abigail/ruby/ch-1.rb @@ -0,0 +1,38 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +def make_chain (string, start) + if string == start + then return [start] + end + if 0 == string . index(start) + then tail = string [start . length, string . length] + result = make_chain(tail, (start . to_i + 1) . to_s) || + make_chain(tail, (start . to_i - 1) . to_s) + if result + then return result . unshift (start) + end + end + return nil + +end + +ARGF . each_line do + |line| + line . strip! + for i in 1 .. line . length do + start = line [0, i] + result = make_chain(line, start) + if result + then puts (result . join (",")) + break + end + end +end diff --git a/challenge-116/abigail/t/ctest.ini b/challenge-116/abigail/t/ctest.ini index 6e4daf9ab7..00778c3508 100644 --- a/challenge-116/abigail/t/ctest.ini +++ b/challenge-116/abigail/t/ctest.ini @@ -5,6 +5,7 @@ [names]
1-1 = Given Examples
+1-2 = More tests
2-1 = Given Examples
2-2 = Large Numbers
diff --git a/challenge-116/abigail/t/input-1-2 b/challenge-116/abigail/t/input-1-2 new file mode 100644 index 0000000000..a8d40cbcea --- /dev/null +++ b/challenge-116/abigail/t/input-1-2 @@ -0,0 +1,4 @@ +16171817161514131211 +16171817161514131212 +123124125 +3210123 diff --git a/challenge-116/abigail/t/output-1-2.exp b/challenge-116/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..0fb097adb7 --- /dev/null +++ b/challenge-116/abigail/t/output-1-2.exp @@ -0,0 +1,4 @@ +16,17,18,17,16,15,14,13,12,11 +16171817161514131212 +123,124,125 +3,2,1,0,1,2,3 |
