From 5e4bc741d5ce6a37db6a72c3845ba4caefae1b6b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 09:48:53 +0200 Subject: Test cases for week 112 --- challenge-112/abigail/t/ctest.ini | 9 +++++++++ challenge-112/abigail/t/input-1-1 | 3 +++ challenge-112/abigail/t/input-1-2 | 3 +++ challenge-112/abigail/t/input-2-1 | 2 ++ challenge-112/abigail/t/output-1-1.exp | 3 +++ challenge-112/abigail/t/output-1-2.exp | 3 +++ challenge-112/abigail/t/output-2-1.exp | 2 ++ 7 files changed, 25 insertions(+) create mode 100644 challenge-112/abigail/t/ctest.ini create mode 100644 challenge-112/abigail/t/input-1-1 create mode 100644 challenge-112/abigail/t/input-1-2 create mode 100644 challenge-112/abigail/t/input-2-1 create mode 100644 challenge-112/abigail/t/output-1-1.exp create mode 100644 challenge-112/abigail/t/output-1-2.exp create mode 100644 challenge-112/abigail/t/output-2-1.exp diff --git a/challenge-112/abigail/t/ctest.ini b/challenge-112/abigail/t/ctest.ini new file mode 100644 index 0000000000..a9e28e24b6 --- /dev/null +++ b/challenge-112/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given examples +1-2 = More tests +2-1 = Given examples diff --git a/challenge-112/abigail/t/input-1-1 b/challenge-112/abigail/t/input-1-1 new file mode 100644 index 0000000000..21654c1df7 --- /dev/null +++ b/challenge-112/abigail/t/input-1-1 @@ -0,0 +1,3 @@ +/a/ +/a/b//c/ +/a/b/c/../.. diff --git a/challenge-112/abigail/t/input-1-2 b/challenge-112/abigail/t/input-1-2 new file mode 100644 index 0000000000..e56eb484ba --- /dev/null +++ b/challenge-112/abigail/t/input-1-2 @@ -0,0 +1,3 @@ +/../../foo +/a/.////./b// +/a/..../b diff --git a/challenge-112/abigail/t/input-2-1 b/challenge-112/abigail/t/input-2-1 new file mode 100644 index 0000000000..b94473479c --- /dev/null +++ b/challenge-112/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +3 +4 diff --git a/challenge-112/abigail/t/output-1-1.exp b/challenge-112/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..088085a167 --- /dev/null +++ b/challenge-112/abigail/t/output-1-1.exp @@ -0,0 +1,3 @@ +/a +/a/b/c +/a diff --git a/challenge-112/abigail/t/output-1-2.exp b/challenge-112/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..4baf181d18 --- /dev/null +++ b/challenge-112/abigail/t/output-1-2.exp @@ -0,0 +1,3 @@ +/foo +/a/b +/a/..../b diff --git a/challenge-112/abigail/t/output-2-1.exp b/challenge-112/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..7ee0007bf1 --- /dev/null +++ b/challenge-112/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +3 +5 -- cgit From ad6243d65f3989322281f49c60264a38f10b7961 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 09:56:20 +0200 Subject: Perl solutions for week 112 --- challenge-112/abigail/perl/ch-1.pl | 43 ++++++++++++++++++++++++++++++++++++++ challenge-112/abigail/perl/ch-2.pl | 24 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 challenge-112/abigail/perl/ch-1.pl create mode 100644 challenge-112/abigail/perl/ch-2.pl diff --git a/challenge-112/abigail/perl/ch-1.pl b/challenge-112/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..e1f01db484 --- /dev/null +++ b/challenge-112/abigail/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/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; + + # Remove duplicate slashes + s !/\K/+!!g; + + # Add a trailing slash; this makes it easier to deal + # with the cases below. + $_ .= "/"; + + # Remove single period + s !/\.(?=/)!!g; + + # Remove double period + 1 while s !/[^/]+/\.\.(?=/)!!; + + # Remove any leading /../ + 1 while s !^/\.\./!/!; + + # Remove trailing slashes + s !/+$!!; + + say; +} diff --git a/challenge-112/abigail/perl/ch-2.pl b/challenge-112/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..2d52916053 --- /dev/null +++ b/challenge-112/abigail/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/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 +# + +# +# This is just the Fibonacci numbers... +# +sub f ($n) {state $c = {0 => 1, 1 => 1}; $$c {$n} //= f ($n - 1) + f ($n - 2)} +say f ($_) for <>; -- cgit From dd5ce43b6bbc9d5b3849b071fcddc6f32d70c017 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 12:35:38 +0200 Subject: README for week 112 --- challenge-112/abigail/README.md | 108 ++++++++++++---------------------------- 1 file changed, 32 insertions(+), 76 deletions(-) diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 021fb3375e..1048c5465e 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -1,98 +1,54 @@ # Solutions by Abigail -## [Search Matrix](https://perlweeklychallenge.org/blog/perl-weekly-challenge-111/#TASK1) +## [Canonical Path](https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK1) -> You are given 5x5 matrix filled with integers such that each row is -> sorted from left to right and the first integer of each row is greater -> than the last integer of the previous row. +> You are given a string path, starting with a slash `/`. > -> Write a script to find a given integer in the matrix using an -> efficient search algorithm. - -### Notes -This challenge confuses me. We're basically asked to find a number -in a sorted list. Which in languages without hashes one would solve -with binary search (yielding an O (log N) solution), and in languages -with hashes you'd use a hash (yielding an O (1) (expected) time solution). -Sure, the hash takes linear preprocessing time, but since we're asked -to write a script, we're spending linear time reading in the data -anyway. - -Perhaps the intend was a subroutine which takes a matrix and a target -number, but that was not what is being asked. The challenge explicitly -asks for *a script*, which means we have to spend a linear amount of -time reading data anyway. So, that's what you get. +> Write a script to convert the given absolute path to the simplified +> canonical path. +> +> In a Unix-style file system: +> +> * A period `.` refers to the current directory. +> * A double period `..` refers to the directory up a level. +> * Multiple consecutive slashes (`//`) are treated as a single slash `/`. +> +> The canonical path format: +> +> * The path starts with a single slash `/`. +> * Any two directories are separated by a single slash `/`. +> * The path does not end with a trailing `/`. +> * The path only contains the directories on the path from the root +> directory to the target file or directory -The only part where we use the fact we are given a matrix is for the -input: the first five lines are assumed to contain the matrix. The -rest of the input is taken as numbers to search for. +### Example +~~~~ +Input: "/a/" +Output: "/a" -Only for language lacking hashes/maps/dictionaries/tables, we will -make use of the fact the input is sorted. For the majority of -languages, the fact input is sorted does not offer additional benefits. +Input: "/a/b//c/" +Output: "/a/b/c" -For Perl, we make two implementations: one based on a hash, the -other using binary search. +Input: "/a/b/c/../.." +Output: "/a" +~~~~ ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [C](c/ch-1.c) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 111: Search Matrix](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-111-1.html) -## [Ordered Letters](https://perlweeklychallenge.org/blog/perl-weekly-challenge-111/#TASK2) +## [Climb Stairs](https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK2) -> Given a word, you can sort its letters alphabetically (case insensitive). -> For example, 'beekeeper' becomes 'beeeeekpr' and 'dictionary' becomes -> 'acdiinorty'. +> You are given `$n` steps to climb > -> Write a script to find the longest English words that don't change when -> their letters are sorted. +> Write a script to find out the distinct ways to climb to the top. +> You are allowed to climb either 1 or 2 steps at a time. ### Notes -We will grep the words from standard input which don't change -if they are sorted; these are the words which match the pattern -/^a*b*c*...z*$/i. We keep track of the longest word. - -If course, there does not have to be a unique word. It will depend -on the word list used to search in. For instance, three different -word list I used give different results: - - infochimps.com /usr/share/dict/words enable.lst - -------------- --------------------- ---------- - Adelops - aegilops - alloquy - beefily beefily - begorry - billowy billowy - egilops - -Only one of them has a unique longest word. - -We will be reading a word list from standard input, and write -the longest word where the letters are in alphabetical order -to standard output. In case of ties, we print the first one found. +This is just finding the `$n + 1` Fibonacci number. ### Solutions -* [GNU AWK](awk/ch-2.gawk) -* [Bash](bash/ch-2.sh) -* [C](c/ch-2.c) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 111: Ordered Letters](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-111-2.html) - -- cgit From 354e9f2bd924a07b9c8b5ab0ff57aadad6f147ab Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 12:52:27 +0200 Subject: More tests for week 112, part 1 --- challenge-112/abigail/t/ctest.ini | 1 + challenge-112/abigail/t/input-1-3 | 7 +++++++ challenge-112/abigail/t/output-1-3.exp | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 challenge-112/abigail/t/input-1-3 create mode 100644 challenge-112/abigail/t/output-1-3.exp diff --git a/challenge-112/abigail/t/ctest.ini b/challenge-112/abigail/t/ctest.ini index a9e28e24b6..ad939f9ac3 100644 --- a/challenge-112/abigail/t/ctest.ini +++ b/challenge-112/abigail/t/ctest.ini @@ -6,4 +6,5 @@ [names] 1-1 = Given examples 1-2 = More tests +1-3 = Root directory 2-1 = Given examples diff --git a/challenge-112/abigail/t/input-1-3 b/challenge-112/abigail/t/input-1-3 new file mode 100644 index 0000000000..91d300fffc --- /dev/null +++ b/challenge-112/abigail/t/input-1-3 @@ -0,0 +1,7 @@ +/ +/// +/././././ +/. +/../../.. +/foo/../.. +/bar/../ diff --git a/challenge-112/abigail/t/output-1-3.exp b/challenge-112/abigail/t/output-1-3.exp new file mode 100644 index 0000000000..1472c679b8 --- /dev/null +++ b/challenge-112/abigail/t/output-1-3.exp @@ -0,0 +1,7 @@ +/ +/ +/ +/ +/ +/ +/ -- cgit From ea5d349d51ce09985b5c59e346ea6da3bdc37562 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 13:05:07 +0200 Subject: AWK solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/awk/ch-1.awk | 44 ++++++++++++++++++++++++++++++++++++++ challenge-112/abigail/awk/ch-2.awk | 25 ++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 challenge-112/abigail/awk/ch-1.awk create mode 100644 challenge-112/abigail/awk/ch-2.awk diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 1048c5465e..dbd9121743 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -33,6 +33,7 @@ Output: "/a" ~~~~ ### Solutions +* [AWK](awk/ch-1.awk) * [Perl](perl/ch-1.pl) ### Blog @@ -49,6 +50,7 @@ This is just finding the `$n + 1` Fibonacci number. ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/awk/ch-1.awk b/challenge-112/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..5de385b451 --- /dev/null +++ b/challenge-112/abigail/awk/ch-1.awk @@ -0,0 +1,44 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +BEGIN { + FS="/" # So we split into directory parts +} + +{ + delete path + j = 0 # Tracks the number of parts in + # the canonical part. + for (i = 1; i <= NF; i ++) { # Loop over directory parts + if ($i == "") { # Skip empty parts + continue; + } + if ($i == ".") { # Skip current directory + continue; + } + if ($i == "..") { # Back up to parent directory + if (j > 0) { + j -- + } + continue; + } + path [j] = $i # Copy + j ++ + } + if (j == 0) { # Root directory + print "/" + } + else { # Print parts, preceeded by a / + for (k = 0; k < j; k ++) { + printf "/%s", path [k] + } + print "" + } +} diff --git a/challenge-112/abigail/awk/ch-2.awk b/challenge-112/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..5053d8467a --- /dev/null +++ b/challenge-112/abigail/awk/ch-2.awk @@ -0,0 +1,25 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +BEGIN { + cache [0] = 1 + cache [1] = 1 +} + +function fib (n) { + if (!(n in cache)) { + cache [n] = fib(n - 1) + fib(n - 2) + } + return cache [n] +} + +{ + print fib($1) +} -- cgit From 32c098711bd7b38c7ec4bbaeccac07af560ffbaa Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 13:06:45 +0200 Subject: Don't print an empty string --- challenge-112/abigail/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-112/abigail/perl/ch-1.pl b/challenge-112/abigail/perl/ch-1.pl index e1f01db484..b68a966191 100644 --- a/challenge-112/abigail/perl/ch-1.pl +++ b/challenge-112/abigail/perl/ch-1.pl @@ -39,5 +39,5 @@ while (<>) { # Remove trailing slashes s !/+$!!; - say; + say $_ || '/'; } -- cgit From 09b7571ec15816cfedda4642d1c3fc8e3e9b0f74 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 16:05:45 +0200 Subject: Bash solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/bash/ch-1.sh | 41 ++++++++++++++++++++++++++++++++++++++ challenge-112/abigail/bash/ch-2.sh | 29 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-112/abigail/bash/ch-1.sh create mode 100644 challenge-112/abigail/bash/ch-2.sh diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index dbd9121743..574d51411f 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -34,6 +34,7 @@ Output: "/a" ### Solutions * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [Perl](perl/ch-1.pl) ### Blog @@ -51,6 +52,7 @@ This is just finding the `$n + 1` Fibonacci number. ### Solutions * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/bash/ch-1.sh b/challenge-112/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..6f41950bc5 --- /dev/null +++ b/challenge-112/abigail/bash/ch-1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +IFS="/" + +while read -a i_parts +do declare -a o_parts + j=0 + for ((i = 0; i < ${#i_parts[@]}; i ++)) + do if [ "X${i_parts[$i]}" == "X" ] # Skip empty parts + then continue + fi + if [ "X${i_parts[$i]}" == "X." ] # Skip current directory + then continue + fi + if [ "X${i_parts[$i]}" == "X.." ] # Back up to parent directory + then if ((j > 0)) + then ((j --)) + fi + continue + fi + o_parts[$j]=${i_parts[$i]} # Copy part + ((j ++)) + done + if ((j == 0)) + then echo "/" # Root directory + else for ((k = 0; k < j; k ++)) # Canonical path + do printf "/%s" ${o_parts[$k]} + done + echo + fi +done diff --git a/challenge-112/abigail/bash/ch-2.sh b/challenge-112/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..17f2d49b9b --- /dev/null +++ b/challenge-112/abigail/bash/ch-2.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +declare -A cache +cache[0]=1 +cache[1]=1 + +function fib () { + local n=$1 + if [[ -z ${cache[$n]} ]] + then fib $((n - 1)) + cache[$n]=$result + fib $((n - 2)) + cache[$n]=$((cache[$n] + result)) + fi + result=${cache[$n]} +} + +while read n +do fib $n + echo $result +done -- cgit From 7b5597d51092301df5e79f1104bb1403e42abd65 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 17:12:12 +0200 Subject: C solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/c/ch-1.c | 78 +++++++++++++++++++++++++++++++++++++++++ challenge-112/abigail/c/ch-2.c | 25 +++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 challenge-112/abigail/c/ch-1.c create mode 100644 challenge-112/abigail/c/ch-2.c diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 574d51411f..c619dd35d2 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -35,6 +35,7 @@ Output: "/a" ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ### Blog @@ -53,6 +54,7 @@ This is just finding the `$n + 1` Fibonacci number. ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [C](c/ch-2.c) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/c/ch-1.c b/challenge-112/abigail/c/ch-1.c new file mode 100644 index 0000000000..d7445188a6 --- /dev/null +++ b/challenge-112/abigail/c/ch-1.c @@ -0,0 +1,78 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +/* + * Here, we will "eliminate" parts (".", or ".." and its parent) + * by replacing the parts with slashes. Then we print the string, + * without printing 2 slashes in succession. + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + line [str_len - 1] = '/'; + for (size_t i = 0; i < str_len; i ++) { + if (line [i] == '.' && line [i - 1] == '/') { + /* Component starts with a . */ + if (line [i + 1] == '/') { + line [i] = '/'; /* Current directory */ + continue; + } + else { + if (line [i + 1] == '.' && line [i + 2] == '/') { + /* Parent directory. */ + /* First wipe this component */ + line [i] = '/'; + line [i + 1] = '/'; + /* Then wipe the previous component, if any. */ + /* First, skip the slashes */ + size_t j = i - 1; + while (j && line [j] == '/') { + j --; + } + /* Now, erase exactly one component */ + while (j && line [j] != '/') { + line [j] = '/'; + j --; + } + } + } + } + } + /* Get rid of trailing slashes */ + while (str_len > 1 && line [str_len - 1] == '/') { + str_len --; + } + /* Print string, eliminating double slashes */ + bool slash = false; + for (size_t i = 0; i < str_len; i ++) { + if (line [i] == '/') { + if (slash) { + continue; + } + slash = true; + } + else { + slash = false; + } + printf ("%c", line [i]); + } + printf ("\n"); + } + free (line); + + return (0); +} diff --git a/challenge-112/abigail/c/ch-2.c b/challenge-112/abigail/c/ch-2.c new file mode 100644 index 0000000000..97c36677ab --- /dev/null +++ b/challenge-112/abigail/c/ch-2.c @@ -0,0 +1,25 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + int n, f1, f2; + + while (scanf ("%d", &n) == 1) { + for (f1 = 0, f2 = 1;n --;) { + f2 += f1; + f1 = f2 - f1; + } + printf ("%d\n", f2); + } + + return (0); +} -- cgit From 5af854f4892557e1f1de653b46402cbfd7baaff9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 21:06:57 +0200 Subject: Lua solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/lua/ch-1.lua | 35 +++++++++++++++++++++++++++++++++++ challenge-112/abigail/lua/ch-2.lua | 24 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 challenge-112/abigail/lua/ch-1.lua create mode 100644 challenge-112/abigail/lua/ch-2.lua diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index c619dd35d2..83b5465420 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -36,6 +36,7 @@ Output: "/a" * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) * [Perl](perl/ch-1.pl) ### Blog @@ -55,6 +56,7 @@ This is just finding the `$n + 1` Fibonacci number. * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/lua/ch-1.lua b/challenge-112/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..2d866dd9bf --- /dev/null +++ b/challenge-112/abigail/lua/ch-1.lua @@ -0,0 +1,35 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + -- + -- Split into parts + -- + local parts = {} + for part in line : gmatch ("[^/]+") do + table . insert (parts, part) + end + -- + -- Copy to new structure + -- + local parts2 = {} + for index, part in ipairs (parts) do + if part == "." then -- Current directory -> skip + goto continue + end + if part == ".." then -- Parent direction -> pop from new structure + table . remove (parts2) + goto continue + end + table . insert (parts2, part) -- Else, copy + ::continue:: + end + print ("/" .. table . concat (parts2, "/")) -- And print +end diff --git a/challenge-112/abigail/lua/ch-2.lua b/challenge-112/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..db76016a8e --- /dev/null +++ b/challenge-112/abigail/lua/ch-2.lua @@ -0,0 +1,24 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local cache = {} +cache [0] = 1 +cache [1] = 1 + +function fib (n) + if cache [n] == nil + then cache [n] = fib (n - 1) + fib (n - 2) + end + return cache [n] +end + +for line in io . lines () do + print (fib (tonumber (line))) +end -- cgit From 2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 10 May 2021 21:52:24 +0200 Subject: Node.js solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/node/ch-1.js | 28 ++++++++++++++++++++++++++++ challenge-112/abigail/node/ch-2.js | 20 ++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 challenge-112/abigail/node/ch-1.js create mode 100644 challenge-112/abigail/node/ch-2.js diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 83b5465420..f3d497b249 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -37,6 +37,7 @@ Output: "/a" * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) ### Blog @@ -57,6 +58,7 @@ This is just finding the `$n + 1` Fibonacci number. * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/node/ch-1.js b/challenge-112/abigail/node/ch-1.js new file mode 100644 index 0000000000..ce79942bfe --- /dev/null +++ b/challenge-112/abigail/node/ch-1.js @@ -0,0 +1,28 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => { + let parts = _ . split (/\/+/) // Split on slash. + let parts2 = [] + parts . every (_ => { + if (_ == "." || _ == "") { // Skip current directory, + return true // and empty parts. + } + if (_ == "..") { // Pop parent directory. + parts2 . pop () + return true + } + parts2 . push (_) // Copy part. + return true + }) + console . log ("/" + parts2 . join ("/")) // Print result. +}) diff --git a/challenge-112/abigail/node/ch-2.js b/challenge-112/abigail/node/ch-2.js new file mode 100644 index 0000000000..aa80e0970d --- /dev/null +++ b/challenge-112/abigail/node/ch-2.js @@ -0,0 +1,20 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +let cache = {0: 1, 1: 1} + +function fib (n) { + cache [n] = cache [n] || fib (n - 1) + fib (n - 2) + return cache [n] +} + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => console . log (fib (+ _))) -- cgit From bfb3640986af5c780b19d4165f36892336cdbcc9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 May 2021 00:32:12 +0200 Subject: Python solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/python/ch-1.py | 24 ++++++++++++++++++++++++ challenge-112/abigail/python/ch-2.py | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 challenge-112/abigail/python/ch-1.py create mode 100644 challenge-112/abigail/python/ch-2.py diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index f3d497b249..4b619db213 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -39,6 +39,7 @@ Output: "/a" * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ### Blog @@ -60,5 +61,6 @@ This is just finding the `$n + 1` Fibonacci number. * [Lua](lua/ch-2.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) ### Blog diff --git a/challenge-112/abigail/python/ch-1.py b/challenge-112/abigail/python/ch-1.py new file mode 100644 index 0000000000..bf52e6d83c --- /dev/null +++ b/challenge-112/abigail/python/ch-1.py @@ -0,0 +1,24 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + parts = line . rstrip () . split ("/") # Split input on / + parts2 = [] + for part in parts: + if part == "" or part == ".": # Skip empty parts, + continue # and current directory. + if part == "..": # Pop parent directory + if len (parts2): + parts2 . pop () + continue + parts2 . append (part) # Else, append. + print ("/" + "/" . join (parts2)) # Print result. diff --git a/challenge-112/abigail/python/ch-2.py b/challenge-112/abigail/python/ch-2.py new file mode 100644 index 0000000000..3e2a55750d --- /dev/null +++ b/challenge-112/abigail/python/ch-2.py @@ -0,0 +1,21 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +cache = {0: 1, 1: 1} + +def fib (n): + if not n in cache: + cache [n] = fib (n - 1) + fib (n - 2) + return (cache [n]) + +for line in fileinput . input (): + print (fib (int (line))) -- cgit From 6b21ba5c2303a59a3cca7b693ea0a55faf766c95 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 May 2021 01:24:43 +0200 Subject: Ruby solutions for week 112 --- challenge-112/abigail/README.md | 2 ++ challenge-112/abigail/ruby/ch-1.rb | 27 +++++++++++++++++++++++++++ challenge-112/abigail/ruby/ch-2.rb | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 challenge-112/abigail/ruby/ch-1.rb create mode 100644 challenge-112/abigail/ruby/ch-2.rb diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 4b619db213..2b454118bb 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -40,6 +40,7 @@ Output: "/a" * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog @@ -62,5 +63,6 @@ This is just finding the `$n + 1` Fibonacci number. * [Node.js](node/ch-1.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-112/abigail/ruby/ch-1.rb b/challenge-112/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..e26463cb68 --- /dev/null +++ b/challenge-112/abigail/ruby/ch-1.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + | line | + parts = line . strip() . split (/\/+/) + parts2 = [] + parts . each do + | part | + if part == "" or part == "." # Skip empty parts and current directory + next + end + if part == ".." # Remove parent directory + parts2 . pop + next + end + parts2 . push (part) # Add part + end + puts ("/" + parts2 . join("/")) # Print result +end diff --git a/challenge-112/abigail/ruby/ch-2.rb b/challenge-112/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..68c59fef40 --- /dev/null +++ b/challenge-112/abigail/ruby/ch-2.rb @@ -0,0 +1,22 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +$cache = {} +$cache [0] = 1 +$cache [1] = 1 + +def fib (n) + $cache [n] ||= fib(n - 1) + fib(n - 2) +end + +ARGF . each_line do + | n | + puts (fib(n . to_i)) +end -- cgit From 7bea5ff392f56d61f6334766a56d088e4fb21a97 Mon Sep 17 00:00:00 2001 From: lakpatashi Date: Tue, 11 May 2021 19:10:31 +0530 Subject: Finished challenge-021 ch-1 only with perl --- challenge-021/lakpatashi/README | 1 + challenge-021/lakpatashi/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge-021/lakpatashi/README create mode 100755 challenge-021/lakpatashi/perl/ch-1.pl diff --git a/challenge-021/lakpatashi/README b/challenge-021/lakpatashi/README new file mode 100644 index 0000000000..bc153bd576 --- /dev/null +++ b/challenge-021/lakpatashi/README @@ -0,0 +1 @@ +Solution by lakpatashi diff --git a/challenge-021/lakpatashi/perl/ch-1.pl b/challenge-021/lakpatashi/perl/ch-1.pl new file mode 100755 index 0000000000..c51c29b389 --- /dev/null +++ b/challenge-021/lakpatashi/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use Data::Dumper; +use List::Util qw(max min sum); +use feature qw(switch); +use Memoize; +memoize qw( factorial ); +#part 1 +my $e; +my $iterLimit = 100; +for my $i (0..$iterLimit){ + $e += exponTerm( $i ); + if( $i == $iterLimit ){ + print "value of e after $iterLimit iteration => $e\n" + } +} + +sub exponTerm{ + my ($n) = @_; + return 1/factorial($n); +} + +sub factorial{ + my ($n) = @_; + if( $n < 2 ){ + return 1; + } + return $n * factorial($n-1); +} + + -- cgit From d498fd3ea3e1df8c647b83e5e4922e6af971960b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 May 2021 19:29:08 +0200 Subject: Use closed form to calculate Fibonacci numbers for week 112, part 2. --- challenge-112/abigail/awk/ch-2.awk | 13 +++---------- challenge-112/abigail/c/ch-2.c | 13 ++++++------- challenge-112/abigail/lua/ch-2.lua | 16 ++++------------ challenge-112/abigail/node/ch-2.js | 12 +++++------- challenge-112/abigail/perl/ch-2.pl | 19 ++++++++++++++++--- challenge-112/abigail/python/ch-2.py | 13 +++++-------- challenge-112/abigail/ruby/ch-2.rb | 13 ++++--------- 7 files changed, 43 insertions(+), 56 deletions(-) diff --git a/challenge-112/abigail/awk/ch-2.awk b/challenge-112/abigail/awk/ch-2.awk index 5053d8467a..57f4b8cc6f 100644 --- a/challenge-112/abigail/awk/ch-2.awk +++ b/challenge-112/abigail/awk/ch-2.awk @@ -9,17 +9,10 @@ # BEGIN { - cache [0] = 1 - cache [1] = 1 -} - -function fib (n) { - if (!(n in cache)) { - cache [n] = fib(n - 1) + fib(n - 2) - } - return cache [n] + SQRT5 = sqrt (5) + PHI = (1 + SQRT5) / 2 } { - print fib($1) + print int (0.5 + PHI ^ ($1 + 1) / SQRT5) } diff --git a/challenge-112/abigail/c/ch-2.c b/challenge-112/abigail/c/ch-2.c index 97c36677ab..73344c2aa5 100644 --- a/challenge-112/abigail/c/ch-2.c +++ b/challenge-112/abigail/c/ch-2.c @@ -1,6 +1,6 @@ # include # include -# include +# include /* * See ../README.md @@ -10,15 +10,14 @@ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file */ +# define SQRT5 (sqrt (5)) +# define PHI ((1 + SQRT5) / 2) + int main (void) { - int n, f1, f2; + int n; while (scanf ("%d", &n) == 1) { - for (f1 = 0, f2 = 1;n --;) { - f2 += f1; - f1 = f2 - f1; - } - printf ("%d\n", f2); + printf ("%lld\n", (long long) floor (0.5 + pow (PHI, n + 1) / SQRT5)); } return (0); diff --git a/challenge-112/abigail/lua/ch-2.lua b/challenge-112/abigail/lua/ch-2.lua index db76016a8e..8932952bbd 100644 --- a/challenge-112/abigail/lua/ch-2.lua +++ b/challenge-112/abigail/lua/ch-2.lua @@ -5,20 +5,12 @@ -- -- --- Run as: lua ch-1.lua < input-file +-- Run as: lua ch-2.lua < input-file -- -local cache = {} -cache [0] = 1 -cache [1] = 1 - -function fib (n) - if cache [n] == nil - then cache [n] = fib (n - 1) + fib (n - 2) - end - return cache [n] -end +local SQRT5 = math . sqrt (5) +local PHI = (1 + SQRT5) / 2 for line in io . lines () do - print (fib (tonumber (line))) + print (math . floor (0.5 + PHI ^ (tonumber (line) + 1) / SQRT5)) end diff --git a/challenge-112/abigail/node/ch-2.js b/challenge-112/abigail/node/ch-2.js index aa80e0970d..2c06c8fdcd 100644 --- a/challenge-112/abigail/node/ch-2.js +++ b/challenge-112/abigail/node/ch-2.js @@ -8,13 +8,11 @@ // Run as: node ch-2.js < input-file // -let cache = {0: 1, 1: 1} - -function fib (n) { - cache [n] = cache [n] || fib (n - 1) + fib (n - 2) - return cache [n] -} +let SQRT5 = Math . sqrt (5) +let PHI = (1 + SQRT5) / 2 require ('readline') . createInterface ({input: process . stdin}) -. on ('line', _ => console . log (fib (+ _))) +. on ('line', _ => console . log ( + Math . round (Math . pow (PHI, +_ + 1) / SQRT5) +)) diff --git a/challenge-112/abigail/perl/ch-2.pl b/challenge-112/abigail/perl/ch-2.pl index 2d52916053..09c5717d1c 100644 --- a/challenge-112/abigail/perl/ch-2.pl +++ b/challenge-112/abigail/perl/ch-2.pl @@ -18,7 +18,20 @@ use experimental 'lexical_subs'; # # -# This is just the Fibonacci numbers... +# This is just the Fibonacci numbers. For a staircase of n steps, +# we need F_(n + 1), where F_n is the nearest integer to # -sub f ($n) {state $c = {0 => 1, 1 => 1}; $$c {$n} //= f ($n - 1) + f ($n - 2)} -say f ($_) for <>; +# n +# phi +# ---- +# 1/2 +# 5 +# 1/2 +# 1 + 5 +# where phi equals -------- (the golden ratio). +# 2 +# +# +my $SQRT5 = sqrt (5); +my $PHI = (1 + $SQRT5) / 2; +say int (1 / 2 + $PHI ** ($_ + 1) / $SQRT5) for <>; diff --git a/challenge-112/abigail/python/ch-2.py b/challenge-112/abigail/python/ch-2.py index 3e2a55750d..da542b06f2 100644 --- a/challenge-112/abigail/python/ch-2.py +++ b/challenge-112/abigail/python/ch-2.py @@ -5,17 +5,14 @@ # # -# Run as: python ch-1.py < input-file +# Run as: python ch-2.py < input-file # import fileinput +from math import sqrt -cache = {0: 1, 1: 1} - -def fib (n): - if not n in cache: - cache [n] = fib (n - 1) + fib (n - 2) - return (cache [n]) +SQRT5 = sqrt (5) +PHI = (1 + SQRT5) / 2 for line in fileinput . input (): - print (fib (int (line))) + print (round (pow (PHI, int (line) + 1) / SQRT5)) diff --git a/challenge-112/abigail/ruby/ch-2.rb b/challenge-112/abigail/ruby/ch-2.rb index 68c59fef40..003d99288f 100644 --- a/challenge-112/abigail/ruby/ch-2.rb +++ b/challenge-112/abigail/ruby/ch-2.rb @@ -5,18 +5,13 @@ # # -# Run as: ruby ch-1.rb < input-file +# Run as: ruby ch-2.rb < input-file # -$cache = {} -$cache [0] = 1 -$cache [1] = 1 - -def fib (n) - $cache [n] ||= fib(n - 1) + fib(n - 2) -end +SQRT5 = Math . sqrt 5 +PHI = (1 + SQRT5) / 2 ARGF . each_line do | n | - puts (fib(n . to_i)) + puts ((PHI ** (n . to_i + 1) / SQRT5) . round) end -- cgit From 962c17a7075cedbd4b75e894cd40e06ebb33da69 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 May 2021 19:59:10 +0200 Subject: Pascal solution for week 112, part 2 --- challenge-112/abigail/README.md | 1 + challenge-112/abigail/pascal/ch-2.p | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge-112/abigail/pascal/ch-2.p diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 2b454118bb..6fae38d608 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -62,6 +62,7 @@ This is just finding the `$n + 1` Fibonacci number. * [Lua](lua/ch-2.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-2.pl) +* [Pascal](pascal/ch-2.p) * [Python](python/ch-2.py) * [Ruby](ruby/ch-1.rb) diff --git a/challenge-112/abigail/pascal/ch-2.p b/challenge-112/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..40c2427265 --- /dev/null +++ b/challenge-112/abigail/pascal/ch-2.p @@ -0,0 +1,26 @@ +Program StairCase; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses math; + +const + sqrt5 = sqrt (5); + phi = (1 + sqrt5) / 2; + +var + n: integer; + +begin + while not eof () do + begin + readln (n); + writeln (round (power (phi, n + 1) / sqrt5)); + end +end. -- cgit From 028c211ace8aaa8e026dfe9f0a6ca979f4901c07 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 11 May 2021 20:39:02 +0200 Subject: Scheme solution for week 112, part 2 --- challenge-112/abigail/README.md | 3 ++- challenge-112/abigail/scheme/ch-2.scm | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 challenge-112/abigail/scheme/ch-2.scm diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 6fae38d608..38877538e6 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -64,6 +64,7 @@ This is just finding the `$n + 1` Fibonacci number. * [Perl](perl/ch-2.pl) * [Pascal](pascal/ch-2.p) * [Python](python/ch-2.py) -* [Ruby](ruby/ch-1.rb) +* [Ruby](ruby/ch-2.rb) +* [Scheme](scheme/ch-2.scm) ### Blog diff --git a/challenge-112/abigail/scheme/ch-2.scm b/challenge-112/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..1223c8719b --- /dev/null +++ b/challenge-112/abigail/scheme/ch-2.scm @@ -0,0 +1,25 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm +;;; + +(use-modules (ice-9 format)) + +(define sqrt5 (sqrt 5)) +(define phi (/ (+ 1 sqrt5) 2)) + +(define (stairs) + (define n (read)) + (if (not (eof-object? n)) + (begin + (format #t "~d\n" + (inexact->exact (round (/ (expt phi (+ n 1)) sqrt5)))) + (stairs) + ) + ) +) + +(stairs) -- cgit From 01c37a09a333d2679de4ef5f485cf5a02c00d701 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 11 May 2021 23:58:50 +0200 Subject: Add solution to 112: Canonical Path & Climb Stairs by E. Choroba --- challenge-112/e-choroba/perl/ch-1.pl | 50 ++++++++++++++++++++++++++++++++++ challenge-112/e-choroba/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100755 challenge-112/e-choroba/perl/ch-1.pl create mode 100755 challenge-112/e-choroba/perl/ch-2.pl diff --git a/challenge-112/e-choroba/perl/ch-1.pl b/challenge-112/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..e36c9e60bb --- /dev/null +++ b/challenge-112/e-choroba/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub canonical_path { + my ($path) = @_; + my @steps = grep length && $_ ne '.', split m{/}, $path; + for (my $pos = 0; $pos <= $#steps; ++$pos) { + if ('..' eq $steps[$pos]) { + if ($pos > 0) { + splice @steps, $pos - 1, 2; + $pos -= 2; + } else { + splice @steps, $pos--, 1; + } + } + } + return '/' . join '/', @steps +} + +use Test::More; + +is canonical_path('/a/'), '/a', 'Example 1'; +is canonical_path('/a/b//c/'), '/a/b/c', 'Example 2'; +is canonical_path('/a/b/c/../..'), '/a', 'Example 3'; + +is canonical_path('/a/./b/./c/.'), '/a/b/c', 'Single dots'; +is canonical_path('/a/b/../c/../d'), '/a/d', 'Double dots'; +is canonical_path('/a/b/../../../../c'), '/c', 'Too many double dots'; # (*) +is canonical_path('/../a'), '/a', 'Double dots at the beginning'; +is canonical_path('/../..'), '/', 'Only double dots at the beginning'; + +done_testing(); + +=heading1 COMMENTS + +(*) This test and the following ones mimic the shell's behaviour for +existing paths only, e.g. /usr/share/../../../bin resolves to /bin, +but /usr/NONEXISTENT/../../../bin fails. + +In fact, on *nix with symlinks involved, you can't resolve .. without +checking the actual file system. For example, + + $ mkdir 1 2 + $ ln -s ../1 2/0 + $ ls 2/0/../1 + +The last command lists 1, not 2/1 (which doesn't exist). + +=cut diff --git a/challenge-112/e-choroba/perl/ch-2.pl b/challenge-112/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..039a0af4b8 --- /dev/null +++ b/challenge-112/e-choroba/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub climb_stairs { + my ($n) = @_; + return 0 if 0 == $n; + + my @s = (1, 2); + push @s, $s[1] + shift @s for 2 .. $n; + return $s[0] +} + +sub climb_stairs_options { + my ($n) = @_; + return [] if 0 == $n; + + my @s = ([[1]], [[1, 1], [2]]); + push @s, [(map [1, @$_], @{ $s[1] }), + (map [2, @$_], @{ shift @s })] + for 2 .. $n; + return $s[0] +} + +use Test::More; +use Test::Deep; + +is climb_stairs(0), 0; +is climb_stairs(1), 1; +is climb_stairs(2), 2; +is climb_stairs(3), 3; +is climb_stairs(4), 5; +is climb_stairs(5), 8; +is climb_stairs(6), 13; + +# ^ +# | +# Hmm, looks almost like the Fibonacci sequence! +# The difference is the element 0, it depends on whether we allow no +# steps as being a solution or not. + +cmp_deeply climb_stairs_options(1), [[1]]; +cmp_deeply climb_stairs_options(2), [[1, 1], [2]]; +cmp_deeply climb_stairs_options(3), [[1, 1, 1], [1, 2], [2, 1]]; +cmp_deeply climb_stairs_options(4), bag([1, 1, 1, 1], + [1, 1, 2], [1, 2, 1], [2, 1, 1], + [2, 2]); +cmp_deeply climb_stairs_options(5), + bag([1, 1, 1, 1, 1], + [1, 1, 1, 2], [1, 1, 2, 1], [1, 2, 1, 1], [2, 1, 1, 1], + [1, 2, 2], [2, 1, 2], [2, 2, 1]); + +done_testing(); -- cgit From 7545df1494144c724111ae198f7ba8caae9e3e03 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 12 May 2021 01:18:52 +0200 Subject: Go solution for week 112, part 2 --- challenge-112/abigail/README.md | 3 ++- challenge-112/abigail/go/ch-2.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 challenge-112/abigail/go/ch-2.go diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index 38877538e6..aba06c6afa 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -59,8 +59,9 @@ This is just finding the `$n + 1` Fibonacci number. * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) +* [Go](go/ch-2.go) * [Lua](lua/ch-2.lua) -* [Node.js](node/ch-1.js) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Pascal](pascal/ch-2.p) * [Python](python/ch-2.py) diff --git a/challenge-112/abigail/go/ch-2.go b/challenge-112/abigail/go/ch-2.go new file mode 100644 index 0000000000..74225957db --- /dev/null +++ b/challenge-112/abigail/go/ch-2.go @@ -0,0 +1,27 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import "fmt" +import "math" + +var SQRT5 float64 = math . Sqrt (5) +var PHI float64 = (1 + SQRT5) / 2 + +func main () { + for { + var n, r float64 + c, _ := fmt . Scanf ("%f", &n) + if (c != 1) { + break + } + r = math . Round (math . Pow (PHI, (n + 1)) / SQRT5) + fmt . Printf ("%d\n", (int) (r)) + } +} -- cgit From 56dff1e5a018f8d2ca229494d30d56294c677a40 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 12 May 2021 09:30:20 +0100 Subject: - Added solutions by E. Choroba. --- stats/pwc-current.json | 201 ++++---- stats/pwc-language-breakdown-summary.json | 58 +-- stats/pwc-language-breakdown.json | 766 +++++++++++++++--------------- stats/pwc-leaders.json | 372 +++++++-------- stats/pwc-summary-1-30.json | 100 ++-- stats/pwc-summary-121-150.json | 120 ++--- stats/pwc-summary-151-180.json | 50 +- stats/pwc-summary-181-210.json | 110 ++--- stats/pwc-summary-211-240.json | 66 +-- stats/pwc-summary-31-60.json | 30 +- stats/pwc-summary-61-90.json | 38 +- stats/pwc-summary-91-120.json | 102 ++-- stats/pwc-summary.json | 50 +- 13 files changed, 1039 insertions(+), 1024 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index d4c47c7a66..d549fb3b6c 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,16 +1,90 @@ { - "title" : { - "text" : "Perl Weekly Challenge - 112" + "xAxis" : { + "type" : "category" + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
", + "headerFormat" : "{series.name}
" + }, + "series" : [ + { + "name" : "Perl Weekly Challenge - 112", + "data" : [ + { + "drilldown" : "Dave Jacoby", + "name" : "Dave Jacoby", + "y" : 3 + }, + { + "y" : 2, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "y" : 2, + "name" : "Feng Chang", + "drilldown" : "Feng Chang" + }, + { + "name" : "Flavio Poletti", + "drilldown" : "Flavio Poletti", + "y" : 4 + }, + { + "y" : 2, + "name" : "James Smith", + "drilldown" : "James Smith" + }, + { + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari", + "y" : 4 + }, + { + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West", + "y" : 4 + }, + { + "name" : "Stuart Little", + "drilldown" : "Stuart Little", + "y" : 4 + }, + { + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "y" : 2 + } + ], + "colorByPoint" : 1 + } + ], + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } }, "plotOptions" : { "series" : { "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" + "format" : "{point.y}", + "enabled" : 1 }, "borderWidth" : 0 } }, + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" + }, "drilldown" : { "series" : [ { @@ -24,20 +98,31 @@ 1 ] ], - "id" : "Dave Jacoby", - "name" : "Dave Jacoby" + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" }, { - "name" : "Feng Chang", - "id" : "Feng Chang", + "id" : "E. Choroba", + "name" : "E. Choroba", "data" : [ [ - "Raku", + "Perl", 2 ] ] }, { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Feng Chang", + "id" : "Feng Chang" + }, + { + "id" : "Flavio Poletti", "data" : [ [ "Perl", @@ -48,20 +133,20 @@ 2 ] ], - "id" : "Flavio Poletti", "name" : "Flavio Poletti" }, { + "name" : "James Smith", "data" : [ [ "Perl", 2 ] ], - "id" : "James Smith", - "name" : "James Smith" + "id" : "James Smith" }, { + "name" : "Luca Ferrari", "data" : [ [ "Raku", @@ -72,21 +157,19 @@ 2 ] ], - "id" : "Luca Ferrari", - "name" : "Luca Ferrari" + "id" : "Luca Ferrari" }, { + "id" : "Mark Anderson", + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "name" : "Mark Anderson", - "id" : "Mark Anderson" + ] }, { - "name" : "Roger Bell_West", "id" : "Roger Bell_West", "data" : [ [ @@ -97,9 +180,11 @@ "Raku", 2 ] - ] + ], + "name" : "Roger Bell_West" }, { + "name" : "Stuart Little", "data" : [ [ "Perl", @@ -110,8 +195,7 @@ 2 ] ], - "id" : "Stuart Little", - "name" : "Stuart Little" + "id" : "Stuart Little" }, { "id" : "Ulrich Rieke", @@ -129,79 +213,10 @@ } ] }, - "chart" : { - "type" : "column" - }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "xAxis" : { - "type" : "category" - }, "subtitle" : { - "text" : "[Champions: 9] Last updated at 2021-05-11 10:28:20 GMT" - }, - "series" : [ - { - "name" : "Perl Weekly Challenge - 112", - "colorByPoint" : 1, - "data" : [ - { - "name" : "Dave Jacoby", - "y" : 3, - "drilldown" : "Dave Jacoby" - }, - { - "y" : 2, - "name" : "Feng Chang", - "drilldown" : "Feng Chang" - }, - { - "name" : "Flavio Poletti", - "y" : 4, - "drilldown" : "Flavio Poletti" - }, - { - "drilldown" : "James Smith", - "name" : "James Smith", - "y" : 2 - }, - { - "drilldown" : "Luca Ferrari", - "y" : 4, - "name" : "Luca Ferrari" - }, - { - "drilldown" : "Mark Anderson", - "name" : "Mark Anderson", - "y" : 2 - }, - { - "drilldown" : "Roger Bell_West", - "y" : 4, - "name" : "Roger Bell_West" - }, - { - "y" : 4, - "name" : "Stuart Little", - "drilldown" : "Stuart Little" - }, - { - "y" : 2, - "name" : "Ulrich Rieke", - "drilldown" : "Ulrich Rieke" - } - ] - } - ], - "tooltip" : { - "followPointer" : 1, - "headerFormat" : "{series.name}
", - "pointFormat" : "{point.name}: {point.y:f}
" + "text" : "[Champions: 10] Last updated at 2021-05-12 08:29:57 GMT" }, - "legend" : { - "enabled" : 0 + "title" : { + "text" : "Perl Weekly Challenge - 112" } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index b441cab59f..0e2ba9dc53 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,15 +1,28 @@ { + "yAxis" : { + "min" : 0, + "title" : { + "text" : null + } + }, "tooltip" : { "pointFormat" : "{point.y:.0f}" }, - "legend" : { - "enabled" : "false" - }, - "subtitle" : { - "text" : "Last updated at 2021-05-11 10:28:20 GMT" - }, "series" : [ { + "name" : "Contributions", + "dataLabels" : { + "align" : "right", + "color" : "#FFFFFF", + "style" : { + "fontFamily" : "Verdana, sans-serif", + "fontSize" : "13px" + }, + "y" : 10, + "format" : "{point.y:.0f}", + "rotation" : -90, + "enabled" : "true" + }, "data" : [ [ "Blog", @@ -17,47 +30,34 @@ ], [ "Perl", - 5279 + 5281 ], [ "Raku", 3354 ] - ], - "dataLabels" : { - "style" : { - "fontSize" : "13px", - "fontFamily" : "Verdana, sans-serif" - }, - "color" : "#FFFFFF", - "format" : "{point.y:.0f}", - "enabled" : "true", - "rotation" : -90, - "y" : 10, - "align" : "right" - }, - "name" : "Contributions" + ] } ], - "yAxis" : { - "title" : { - "text" : null - }, - "min" : 0 - }, "xAxis" : { - "type" : "category", "labels" : { "style" : { "fontSize" : "13px", "fontFamily" : "Verdana, sans-serif" } - } + }, + "type" : "category" }, "title" : { "text" : "Perl Weekly Challenge Contributions [2019 - 2020]" }, "chart" : { "type" : "column" + }, + "subtitle" : { + "text" : "Last updated at 2021-05-12 08:29:57 GMT" + }, + "legend" : { + "enabled" : "false" } } diff --git a/stats/pwc-language-breakdown.json b/stats/pwc-language-breakdown.json index 169887918d..dcf553c76c 100644 --- a/stats/pwc-language-breakdown.json +++ b/stats/pwc-language-breakdown.json @@ -1,19 +1,14 @@ { - "title" : { - "text" : "Perl Weekly Challenge Language" + "legend" : { + "enabled" : "false" }, - "plotOptions" : { - "series" : { - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - }, - "borderWidth" : 0 - } + "chart" : { + "type" : "column" }, "drilldown" : { "series" : [ { + "id" : "001", "data" : [ [ "Perl", @@ -28,12 +23,10 @@ 11 ] ], - "id" : "001", "name" : "001" }, { "name" : "002", - "id" : "002", "data" : [ [ "Perl", @@ -47,9 +40,11 @@ "Blog", 10 ] - ] + ], + "id" : "002" }, { + "name" : "003", "data" : [ [ "Perl", @@ -64,10 +59,10 @@ 9 ] ], - "id" : "003", - "name" : "003" + "id" : "003" }, { + "name" : "004", "data" : [ [ "Perl", @@ -82,10 +77,10 @@ 10 ] ], - "name" : "004", "id" : "004" }, { + "name" : "005", "data" : [ [ "Perl", @@ -100,10 +95,10 @@ 12 ] ], - "id" : "005", - "name" : "005" + "id" : "005" }, { + "name" : "006", "data" : [ [ "Perl", @@ -118,7 +113,6 @@ 7 ] ], - "name" : "006", "id" : "006" }, { @@ -140,6 +134,8 @@ "id" : "007" }, { + "id" : "008", + "name" : "008", "data" : [ [ "Perl", @@ -153,9 +149,7 @@ "Blog", 12 ] - ], - "id" : "008", - "name" : "008" + ] }, { "data" : [ @@ -176,8 +170,6 @@ "id" : "009" }, { - "name" : "010", - "id" : "010", "data" : [ [ "Perl", @@ -191,9 +183,13 @@ "Blog", 11 ] - ] + ], + "name" : "010", + "id" : "010" }, { + "id" : "011", + "name" : "011", "data" : [ [ "Perl", @@ -207,13 +203,9 @@ "Blog", 10 ] - ], - "id" : "011", - "name" : "011" + ] }, { - "id" : "012", - "name" : "012", "data" : [ [ "Perl", @@ -227,11 +219,12 @@ "Blog", 11 ] - ] + ], + "name" : "012", + "id" : "012" }, { "id" : "013", - "name" : "013", "data" : [ [ "Perl", @@ -245,7 +238,8 @@ "Blog", 13 ] - ] + ], + "name" : "013" }, { "data" : [ @@ -280,10 +274,12 @@ 15 ] ], - "id" : "015", - "name" : "015" + "name" : "015", + "id" : "015" }, { + "id" : "016", + "name" : "016", "data" : [ [ "Perl", @@ -297,9 +293,7 @@ "Blog", 12 ] - ], - "id" : "016", - "name" : "016" + ] }, { "data" : [ @@ -320,8 +314,8 @@ "id" : "017" }, { - "name" : "018", "id" : "018", + "name" : "018", "data" : [ [ "Perl", @@ -338,8 +332,8 @@ ] }, { - "name" : "019", "id" : "019", + "name" : "019", "data" : [ [ "Perl", @@ -356,7 +350,6 @@ ] }, { - "id" : "020", "name" : "020", "data" : [ [ @@ -371,9 +364,12 @@ "Blog", 13 ] - ] + ], + "id" : "020" }, { + "id" : "021", + "name" : "021", "data" : [ [ "Perl", @@ -387,12 +383,9 @@ "Blog", 10 ] - ], - "id" : "021", - "name" : "021" + ] }, { - "id" : "022", "name" : "022", "data" : [ [ @@ -407,10 +400,10 @@ "Blog", 10 ] - ] + ], + "id" : "022" }, { - "id" : "023", "name" : "023", "data" : [ [ @@ -425,11 +418,10 @@ "Blog", 12 ] - ] + ], + "id" : "023" }, { - "name" : "024", - "id" : "024", "data" : [ [ "Perl", @@ -443,9 +435,12 @@ "Blog", 11 ] - ] + ], + "name" : "024", + "id" : "024" }, { + "name" : "025", "data" : [ [ "Perl", @@ -460,11 +455,9 @@ 12 ] ], - "name" : "025", "id" : "025" }, { - "name" : "026", "id" : "026", "data" : [ [ @@ -479,11 +472,11 @@ "Blog", 10 ] - ] + ], + "name" : "026" }, { "name" : "027", - "id" : "027", "data" : [ [ "Perl"