From 2dbeaa8b6609f83fa270a4da2ebbfabd85da3e8e Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:03:05 -0500 Subject: Add files via upload --- challenge-119/paul-fajman/ch-1.pl | 36 ++++++++++++++++++++++++++++ challenge-119/paul-fajman/ch-2.pl | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-119/paul-fajman/ch-1.pl create mode 100644 challenge-119/paul-fajman/ch-2.pl diff --git a/challenge-119/paul-fajman/ch-1.pl b/challenge-119/paul-fajman/ch-1.pl new file mode 100644 index 0000000000..03918969c3 --- /dev/null +++ b/challenge-119/paul-fajman/ch-1.pl @@ -0,0 +1,36 @@ +#/usr/bin/perl + +use strict; +use warnings; + +use POSIX; + +my $int = $ARGV[0]; +my $quo = $int; +my ($i, $rem); +my $total = 0; +my @nibbles=(); + +# Calculate binary number +while($quo ne 0) { + $rem = floor($quo % 2); + unshift @nibbles, $rem; + $quo = floor($quo/2); +} + +# Check that final number is 8 digits +while ($#nibbles+1 lt 8) { + unshift @nibbles, 0; +} + +# Swap the nibbles +my @final = splice(@nibbles, 4); +@final = (@final, @nibbles); + +# Calcuate the new decimal number +for ($i=7; $i>-1; $i--) { + $total+= $final[$i]*(2**(7-$i)); +} + +print "Input: \$N = $int\n"; +print "Output: $total\n"; diff --git a/challenge-119/paul-fajman/ch-2.pl b/challenge-119/paul-fajman/ch-2.pl new file mode 100644 index 0000000000..06299baa92 --- /dev/null +++ b/challenge-119/paul-fajman/ch-2.pl @@ -0,0 +1,49 @@ +#/usr/bin/perl + +use strict; +use warnings; + +use POSIX; + +my $input = $ARGV[0]; +my @numbers; + +my ($quo, $rem, $i); +my $check=0; +my ($oneone, $output); + +for ($i=1;$i<$input+1;$i++) { + $quo = $i; + # Calculate base 4 number + # Later throw out any values with 0 + while ($quo ne 0) { + $rem = floor($quo % 4); + unshift @numbers, $rem; + $quo = floor($quo/4); + } + # Check for any digit with a 0 + foreach (@numbers) { + if ($_ eq 0) { + $check=1; + next; + } + $oneone.=$_; + } + + # Check if any digits are consecutive ones or if one was a 0; + if ($oneone =~ m/11/ || $check eq 1) { + undef($oneone); + undef(@numbers); + $check = 0; + $input++; + next; + } + + $output = $oneone; + undef(@numbers); + undef($oneone); +} + +print "INPUT: \$N = $ARGV[0]\n"; +print "OUTPUT: $output\n"; + -- cgit From b6b74384b9f09082c406e24565dd5fac508443eb Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:05:46 -0500 Subject: Create perl --- challenge-119/paul-fajman/perl | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-119/paul-fajman/perl diff --git a/challenge-119/paul-fajman/perl b/challenge-119/paul-fajman/perl new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/challenge-119/paul-fajman/perl @@ -0,0 +1 @@ + -- cgit From 1956511b5d4e5099a5122add81c907b443f0c585 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:06:06 -0500 Subject: Delete perl --- challenge-119/paul-fajman/perl | 1 - 1 file changed, 1 deletion(-) delete mode 100644 challenge-119/paul-fajman/perl diff --git a/challenge-119/paul-fajman/perl b/challenge-119/paul-fajman/perl deleted file mode 100644 index 8b13789179..0000000000 --- a/challenge-119/paul-fajman/perl +++ /dev/null @@ -1 +0,0 @@ - -- cgit From 316acf3cc0f6c5cb781224dadfd13525b40c0ed7 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:06:19 -0500 Subject: Create ch-1.pl --- challenge-119/paul-fajman/perl/ch-1.pl | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-119/paul-fajman/perl/ch-1.pl diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/challenge-119/paul-fajman/perl/ch-1.pl @@ -0,0 +1 @@ + -- cgit From c26da087402272113cd8393e96a0c8bcb48aeba8 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:06:47 -0500 Subject: Update ch-1.pl --- challenge-119/paul-fajman/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl index 8b13789179..8175881325 100644 --- a/challenge-119/paul-fajman/perl/ch-1.pl +++ b/challenge-119/paul-fajman/perl/ch-1.pl @@ -1 +1,37 @@ +#/usr/bin/perl + +use strict; +use warnings; + +use POSIX; + +my $int = $ARGV[0]; +my $quo = $int; +my ($i, $rem); +my $total = 0; +my @nibbles=(); + +# Calculate binary number +while($quo ne 0) { + $rem = floor($quo % 2); + unshift @nibbles, $rem; + $quo = floor($quo/2); +} + +# Check that final number is 8 digits +while ($#nibbles+1 lt 8) { + unshift @nibbles, 0; +} + +# Swap the nibbles +my @final = splice(@nibbles, 4); +@final = (@final, @nibbles); + +# Calcuate the new decimal number +for ($i=7; $i>-1; $i--) { + $total+= $final[$i]*(2**(7-$i)); +} + +print "Input: \$N = $int\n"; +print "Output: $total\n"; -- cgit From 1a957ed2bf8665653288733ac1ce66b0a87adf2b Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:07:02 -0500 Subject: Delete ch-1.pl --- challenge-119/paul-fajman/ch-1.pl | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 challenge-119/paul-fajman/ch-1.pl diff --git a/challenge-119/paul-fajman/ch-1.pl b/challenge-119/paul-fajman/ch-1.pl deleted file mode 100644 index 03918969c3..0000000000 --- a/challenge-119/paul-fajman/ch-1.pl +++ /dev/null @@ -1,36 +0,0 @@ -#/usr/bin/perl - -use strict; -use warnings; - -use POSIX; - -my $int = $ARGV[0]; -my $quo = $int; -my ($i, $rem); -my $total = 0; -my @nibbles=(); - -# Calculate binary number -while($quo ne 0) { - $rem = floor($quo % 2); - unshift @nibbles, $rem; - $quo = floor($quo/2); -} - -# Check that final number is 8 digits -while ($#nibbles+1 lt 8) { - unshift @nibbles, 0; -} - -# Swap the nibbles -my @final = splice(@nibbles, 4); -@final = (@final, @nibbles); - -# Calcuate the new decimal number -for ($i=7; $i>-1; $i--) { - $total+= $final[$i]*(2**(7-$i)); -} - -print "Input: \$N = $int\n"; -print "Output: $total\n"; -- cgit From 94e46372cf8cf4a68ed5099aeeec66ab21ceb4cb Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:07:07 -0500 Subject: Delete ch-2.pl --- challenge-119/paul-fajman/ch-2.pl | 49 --------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 challenge-119/paul-fajman/ch-2.pl diff --git a/challenge-119/paul-fajman/ch-2.pl b/challenge-119/paul-fajman/ch-2.pl deleted file mode 100644 index 06299baa92..0000000000 --- a/challenge-119/paul-fajman/ch-2.pl +++ /dev/null @@ -1,49 +0,0 @@ -#/usr/bin/perl - -use strict; -use warnings; - -use POSIX; - -my $input = $ARGV[0]; -my @numbers; - -my ($quo, $rem, $i); -my $check=0; -my ($oneone, $output); - -for ($i=1;$i<$input+1;$i++) { - $quo = $i; - # Calculate base 4 number - # Later throw out any values with 0 - while ($quo ne 0) { - $rem = floor($quo % 4); - unshift @numbers, $rem; - $quo = floor($quo/4); - } - # Check for any digit with a 0 - foreach (@numbers) { - if ($_ eq 0) { - $check=1; - next; - } - $oneone.=$_; - } - - # Check if any digits are consecutive ones or if one was a 0; - if ($oneone =~ m/11/ || $check eq 1) { - undef($oneone); - undef(@numbers); - $check = 0; - $input++; - next; - } - - $output = $oneone; - undef(@numbers); - undef($oneone); -} - -print "INPUT: \$N = $ARGV[0]\n"; -print "OUTPUT: $output\n"; - -- cgit From 5ca123778f0bf377a2de08729ca85509f1358d02 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:07:19 -0500 Subject: Add files via upload --- challenge-119/paul-fajman/perl/ch-2.pl | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-119/paul-fajman/perl/ch-2.pl diff --git a/challenge-119/paul-fajman/perl/ch-2.pl b/challenge-119/paul-fajman/perl/ch-2.pl new file mode 100644 index 0000000000..06299baa92 --- /dev/null +++ b/challenge-119/paul-fajman/perl/ch-2.pl @@ -0,0 +1,49 @@ +#/usr/bin/perl + +use strict; +use warnings; + +use POSIX; + +my $input = $ARGV[0]; +my @numbers; + +my ($quo, $rem, $i); +my $check=0; +my ($oneone, $output); + +for ($i=1;$i<$input+1;$i++) { + $quo = $i; + # Calculate base 4 number + # Later throw out any values with 0 + while ($quo ne 0) { + $rem = floor($quo % 4); + unshift @numbers, $rem; + $quo = floor($quo/4); + } + # Check for any digit with a 0 + foreach (@numbers) { + if ($_ eq 0) { + $check=1; + next; + } + $oneone.=$_; + } + + # Check if any digits are consecutive ones or if one was a 0; + if ($oneone =~ m/11/ || $check eq 1) { + undef($oneone); + undef(@numbers); + $check = 0; + $input++; + next; + } + + $output = $oneone; + undef(@numbers); + undef($oneone); +} + +print "INPUT: \$N = $ARGV[0]\n"; +print "OUTPUT: $output\n"; + -- cgit From c29db6e7df729967c26e749e9cf831bdb5b1d853 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:09:40 -0500 Subject: Update ch-1.pl --- challenge-119/paul-fajman/perl/ch-1.pl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl index 8175881325..5a96f61777 100644 --- a/challenge-119/paul-fajman/perl/ch-1.pl +++ b/challenge-119/paul-fajman/perl/ch-1.pl @@ -1,5 +1,23 @@ #/usr/bin/perl +# Weekly Challenge 119 +# You are given a positive integer $N. +# Write a script to swap the two nibbles of the binary representation of the +# given number and print the decimal number of the new binary representation. +# +# Input: $N = 101 +# Output: 86 + +# Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101). +# The swapped nibbles would be (0101)(0110) same as decimal 86. + +# Input: $N = 18 +# Output: 33 + +# Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010). +# The swapped nibbles would be (0010)(0001) same as decimal 33. +############################## + use strict; use warnings; -- cgit From d6c3b2aab247c84967ffba1e92232e3a43c34b28 Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:10:03 -0500 Subject: Update ch-1.pl --- challenge-119/paul-fajman/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl index 5a96f61777..cd2b2f0165 100644 --- a/challenge-119/paul-fajman/perl/ch-1.pl +++ b/challenge-119/paul-fajman/perl/ch-1.pl @@ -1,6 +1,6 @@ #/usr/bin/perl -# Weekly Challenge 119 +# Weekly Challenge 119 Task #1 # You are given a positive integer $N. # Write a script to swap the two nibbles of the binary representation of the # given number and print the decimal number of the new binary representation. -- cgit From 66cea3b9b11f47e8ddad959ccabbf0b1018b5dde Mon Sep 17 00:00:00 2001 From: corvettes13 <86648326+corvettes13@users.noreply.github.com> Date: Fri, 2 Jul 2021 10:11:19 -0500 Subject: Update ch-2.pl --- challenge-119/paul-fajman/perl/ch-2.pl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/challenge-119/paul-fajman/perl/ch-2.pl b/challenge-119/paul-fajman/perl/ch-2.pl index 06299baa92..f764ea153a 100644 --- a/challenge-119/paul-fajman/perl/ch-2.pl +++ b/challenge-119/paul-fajman/perl/ch-2.pl @@ -1,5 +1,14 @@ #/usr/bin/perl +# Weekly Challenge 119 Task #2 +# Write a script to generate sequence starting at 1. Consider the +# increasing sequence of integers which contain only 1’s, 2’s and +# 3’s, and do not have any doublets of 1’s like below. Please +# accept a positive integer $N and print the $Nth term in the +# generated sequence. + +# 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131 + use strict; use warnings; -- cgit From 87a6047a60f9e12271645370808b9624df18a58c Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 4 Jul 2021 21:58:13 +0100 Subject: tidied --- challenge-119/james-smith/README.md | 2 +- challenge-119/james-smith/cesil/cesil.pl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index 11f1a24015..663a88f2d2 100644 --- a/challenge-119/james-smith/README.md +++ b/challenge-119/james-smith/README.md @@ -176,7 +176,7 @@ my %commands = ( ## Parser loop while(<>) { ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/; - ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/; + ($ptrs{$1},$_)=(0+@code,$2) if /^(\S{1,7})\s+(.*)/; my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2; die "\n## Unknown command [$cmd @ ",1+@code,"]\n" unless exists $commands{$cmd}; diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl index 64b0355de7..756f10227b 100644 --- a/challenge-119/james-smith/cesil/cesil.pl +++ b/challenge-119/james-smith/cesil/cesil.pl @@ -36,7 +36,7 @@ my %commands = ( ## Parser loop while(<>) { ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/; - ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/; + ($ptrs{$1},$_)=(0+@code,$2) if /^(\S{1,7})\s+(.*)/; my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2; die "\n## Unknown command [$cmd @ ",1+@code,"]\n" unless exists $commands{$cmd}; -- cgit From 36df052252a4d1b6d145e4a44dc5360091c3878b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 12:35:46 +0200 Subject: README for week 120 --- challenge-120/abigail/README.md | 62 ++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/challenge-120/abigail/README.md b/challenge-120/abigail/README.md index 2fc20d5e68..c7dfc7d883 100644 --- a/challenge-120/abigail/README.md +++ b/challenge-120/abigail/README.md @@ -1,34 +1,29 @@ # Solutions by Abigail -## [Swap Nibbles](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK1) +## [Swap Odd/Even Bits](https://perlweeklychallenge.org/blog/perl-weekly-challenge-120/#TASK1) -> You are given a positive integer `$N`. -> -> Write a script to swap the two nibbles of the binary representation of -> the given number and print the decimal number of the new binary -> representation. -> -> > A nibble is a four-bit aggregation, or half an octet. -> -> To keep the task simple, we only allow integer less than or equal to `255`. +> You are given a positive integer `$N` less than or equal to 255. +> +> Write a script to swap the odd positioned bit with even positioned +> bit and print the decimal equivalent of the new binary representation. ### Examples ~~~~ Input: $N = 101 -Output: 86 +Output: 154 ~~~~ -Binary representation of decimal `101` is `1100101` or as 2 nibbles -`(0110)(0101)`. The swapped nibbles would be `(0101)(0110)` same as -decimal `86`. +Binary representation of the given number is `01 10 01 01`. +The new binary representation after the odd/even swap is `10 01 10 10`. +The decimal equivalent of `10011010` is `154`. ~~~~ Input: $N = 18 Output: 33 ~~~~ -Binary representation of decimal `18` is `10010` or as 2 nibbles -`(0001)(0010)`. The swapped nibbles would be `(0010)(0001)` same as -decimal `33`. +Binary representation of the given number is `00 01 00 10`. +The new binary representation after the odd/even swap is `00 10 00 01`. +The decimal equivalent of `100001` is `33`. ### Solutions * [AWK](awk/ch-1.awk) @@ -49,16 +44,33 @@ decimal `33`. * [Tcl](tcl/ch-1.tcl) ### Blog -[Swap Nibbles](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-1.html) +[Swap Odd/Even Bits](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-1.html) -## [Sequence without 1-on-1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) +## [Clock Angle](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) -> Write a script to generate sequence starting at `1`. Consider the -> increasing sequence of integers which contain only `1`s, `2`s, and -> `3`s, and do not have any doublets of `1`s > like below. Please accept -> a positive integer `$N` and print the `$N`th term in the generated sequence. +> You are given time `$T` in the format `hh:mm`. > -> > 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... +> Write a script to find the smaller angle formed by the hands of an +> analog clock at a given time.
+> **HINT: A analog clock is divided up into `12` sectors. One sector +> represents `30` degree (`360/12 = 30`).** + +### Examples +~~~~ +Input: $T = '03:10' +Output: 35 degree +~~~~ + +The distance between the `2` and the `3` on the clock is `30` degree. +For the `10` minutes i.e. `1/6` of an hour that have passed. The hour +hand has also moved `1/6` of the distance between the `3` and the `4`, +which adds `5` degree (`1/6` of `30`). The total measure of the angle +is `35` degree. + +~~~~ +Input: $T = '04:00' +Output: 120 degree +~~~~ ### Solutions * [AWK](awk/ch-2.awk) @@ -71,4 +83,4 @@ decimal `33`. * [Ruby](ruby/ch-2.rb) ### Blog -[Sequence without 1-on-1](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-2.html) +[Clock Angle](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-2.html) -- cgit From 7724f41709fab6db7dcd3ed4343dab68d72a734b Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:51:47 +0100 Subject: first pass at solution using & | << and >> --- challenge-120/james-smith/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-120/james-smith/perl/ch-2.pl | 22 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-120/james-smith/perl/ch-1.pl create mode 100644 challenge-120/james-smith/perl/ch-2.pl diff --git a/challenge-120/james-smith/perl/ch-1.pl b/challenge-120/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..31b470c961 --- /dev/null +++ b/challenge-120/james-smith/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 101, 154 ], + [ 18, 33 ], +); + +is( switch_bits($_->[0]), $_->[1] ) foreach @TESTS; +is( switch_bits($_->[1]), $_->[0] ) foreach @TESTS; + +done_testing(); + +## Similar to last weeks calculation. We and +## with 10101010 to get the higher bits and +## with 01010101 to get the lower bits... +## and use bit shift operators to move them +## left and right respectively. +sub switch_bits { + ($_[0]&0xaa)>>1 | ($_[0]&0x55)<<1; +} + diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..2348c8b946 --- /dev/null +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 0, 1 ], +); + +is( my_function($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub my_function { + return 1; +} + -- cgit From 7ecb9c24d7c69747e9dee7bd0e6d3a4d25fff466 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:54:25 +0100 Subject: clock angle added resolving issue of fractional angles --- challenge-120/james-smith/perl/ch-2.pl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl index 2348c8b946..f34d666f9b 100644 --- a/challenge-120/james-smith/perl/ch-2.pl +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -9,14 +9,23 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my @TESTS = ( - [ 0, 1 ], + [ '03:10', 35 ], + [ '04:00', 120 ], ); -is( my_function($_->[0]), $_->[1] ) foreach @TESTS; +is( clock_angle($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); -sub my_function { - return 1; +sub clock_angle { +## The difference is: hr*30+min/2 - min*12 +## Modulo is int based so to avoid issue +## when min is even we multiply by 2 take +## modulus and then divide by 2. +## If value is > 180 then we subtract from +## 360.... + my($h,$m) = split /:/,shift; + my $a = abs($h*60-$m*11)%720/2; + return $a > 180 ? 360-$a : $a; } -- cgit From 6f9030422fb5642e773e8a15fc071d4be4499a93 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 5 Jul 2021 11:55:24 +0100 Subject: Solutions for challenge #120 --- challenge-120/roger-bell-west/perl/ch-1.pl | 14 +++++++++++++ challenge-120/roger-bell-west/perl/ch-2.pl | 23 +++++++++++++++++++++ challenge-120/roger-bell-west/python/ch-1.py | 17 +++++++++++++++ challenge-120/roger-bell-west/python/ch-2.py | 27 ++++++++++++++++++++++++ challenge-120/roger-bell-west/raku/ch-1.p6 | 12 +++++++++++ challenge-120/roger-bell-west/raku/ch-2.p6 | 21 +++++++++++++++++++ challenge-120/roger-bell-west/ruby/ch-1.rb | 19 +++++++++++++++++ challenge-120/roger-bell-west/ruby/ch-2.rb | 31 ++++++++++++++++++++++++++++ challenge-120/roger-bell-west/rust/ch-1.rs | 16 ++++++++++++++ challenge-120/roger-bell-west/rust/ch-2.rs | 27 ++++++++++++++++++++++++ 10 files changed, 207 insertions(+) create mode 100755 challenge-120/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-120/roger-bell-west/perl/ch-2.pl create mode 100755 challenge-120/roger-bell-west/python/ch-1.py create mode 100755 challenge-120/roger-bell-west/python/ch-2.py create mode 100755 challenge-120/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-120/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-120/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-120/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-120/roger-bell-west/rust/ch-1.rs create mode 100644 challenge-120/roger-bell-west/rust/ch-2.rs diff --git a/challenge-120/roger-bell-west/perl/ch-1.pl b/challenge-120/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..e5e75b5a5d --- /dev/null +++ b/challenge-120/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,14 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(seob(101),154,'example 1'); +is(seob(18),33,'example 2'); + +sub seob { + my $n=shift; + return (($n & 0x55)<<1) | (($n & 0xAA)>>1); +} diff --git a/challenge-120/roger-bell-west/perl/ch-2.pl b/challenge-120/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..7cc9f76e75 --- /dev/null +++ b/challenge-120/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(ca('03:10'),35,'example 1'); +is(ca('04:00'),120,'example 2'); + +sub ca { + my $n=shift; + my $a=0; + if ($n =~ /([0-9]+):([0-9]+)/) { + my ($ha,$ma)=map {$_ % 360} ($1*30+$2/2,$2*6); + $a=abs($ha-$ma); + while ($a > 180) { + $a-=360; + } + $a=abs($a); + } + return $a; +} diff --git a/challenge-120/roger-bell-west/python/ch-1.py b/challenge-120/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c3e70fa0b2 --- /dev/null +++ b/challenge-120/roger-bell-west/python/ch-1.py @@ -0,0 +1,17 @@ +#! /usr/bin/python3 + +import unittest +import re + +def seob(n): + return ((n & 0x55)<<1) | ((n & 0xAA)>>1) + +class TestSeob(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(seob(101),154,'example 1') + + def test_ex2(self): + self.assertEqual(seob(18),33,'example 2') + +unittest.main() diff --git a/challenge-120/roger-bell-west/python/ch-2.py b/challenge-120/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..78cf2e9f98 --- /dev/null +++ b/challenge-120/roger-bell-west/python/ch-2.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest +import re + +def ca(n): + a=0.0 + p=re.compile(r"([0-9]+):([0-9]+)") + m=p.match(n) + hh=int(m.group(1)) + mm=int(m.group(2)) + ha=hh*30+mm/2.0 + ma=mm*6 + a=abs(ha-ma) + while a > 180: + a -= 360 + return abs(a) + +class TestCa(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ca('03:10'),35,'example 1') + + def test_ex2(self): + self.assertEqual(ca('04:00'),120,'example 2') + +unittest.main() diff --git a/challenge-120/roger-bell-west/raku/ch-1.p6 b/challenge-120/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..056f91ef6f --- /dev/null +++ b/challenge-120/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(seob(101),154,'example 1'); +is(seob(18),33,'example 2'); + +sub seob($n) { + return (($n +& 0x55)+<1) +| (($n +& 0xAA)+>1); +} diff --git a/challenge-120/roger-bell-west/raku/ch-2.p6 b/challenge-120/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..1cfdcce8c1 --- /dev/null +++ b/challenge-120/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(ca('03:10'),35,'example 1'); +is(ca('04:00'),120,'example 2'); + +sub ca($n) { + my $a=0; + if ($n ~~ /(<[0..9]>+)\:(<[0..9]>+)/) { + my ($ha,$ma)=map {$_ % 360}, ($0*30+$1/2,$1*6); + $a=abs($ha-$ma); + while ($a > 180) { + $a-=360; + } + $a=abs($a); + } + return $a; +} diff --git a/challenge-120/roger-bell-west/ruby/ch-1.rb b/challenge-120/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..c77793ddaa --- /dev/null +++ b/challenge-120/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def seob(n) + return ((n & 0x55)<<1) | ((n & 0xAA)>>1) +end + +require 'test/unit' + +class TestSeob < Test::Unit::TestCase + + def test_ex1 + assert_equal(154,seob(101)) + end + + def test_ex2 + assert_equal(33,seob(18)) + end + +end diff --git a/challenge-120/roger-bell-west/ruby/ch-2.rb b/challenge-120/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..8c7843ac61 --- /dev/null +++ b/challenge-120/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def ca(n) + a=0 + p=Regexp.new("([0-9]+):([0-9]+)") + if n =~ /([0-9]+):([0-9]+)/ then + hh=$1.to_f + mm=$2.to_f + hm=[hh*30+mm/2,mm*6].map {|i| i % 360} + a=(hm[0]-hm[1]).abs + while (a > 180) do + a-=360; + end + a=a.abs + end + return a; +end + +require 'test/unit' + +class TestCa < Test::Unit::TestCase + + def test_ex1 + assert_equal(35,ca('03:10')) + end + + def test_ex2 + assert_equal(120,ca('04:00')) + end + +end diff --git a/challenge-120/roger-bell-west/rust/ch-1.rs b/challenge-120/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e8f8cffbbd --- /dev/null +++ b/challenge-120/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,16 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(seob(101),154); +} + +#[test] +fn test_ex2() { + assert_eq!(seob(18),33); +} + +fn seob (n: u8) -> u8 { + return ((n & 0x55)<<1) | ((n & 0xAA)>>1); +} diff --git a/challenge-120/roger-bell-west/rust/ch-2.rs b/challenge-120/roger-bell-west/rust/ch-2.rs new file mode 100644 index 0000000000..b85890da50 --- /dev/null +++ b/challenge-120/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,27 @@ +use regex::Regex; + +#[test] +fn test_ex1() { + assert_eq!(ca("03:10"),35.0); +} + +#[test] +fn test_ex2() { + assert_eq!(ca("04:00"),120.0); +} + +fn ca (n: &str) -> f32 { + let mut a; + let p = Regex::new(r"([0-9]+):([0-9]+)").unwrap(); + let m=p.captures(&n).unwrap(); + let hh=m.get(1).unwrap().as_str().parse::().unwrap(); + let mm=m.get(2).unwrap().as_str().parse::().unwrap(); + let ha=hh*30.+mm/2.; + let ma=mm*6.; + a=(ha-ma).abs(); + while a > 180. { + a -= 180.; + } + a=a.abs(); + return a; +} -- cgit From 44286b8a2788cd6f76fd0f67167d052a86e1782b Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:56:15 +0100 Subject: init --- challenge-120/james-smith/README.md | 290 +----------------------------------- 1 file changed, 4 insertions(+), 286 deletions(-) diff --git a/challenge-120/james-smith/README.md b/challenge-120/james-smith/README.md index 11f1a24015..697c8c702a 100644 --- a/challenge-120/james-smith/README.md +++ b/challenge-120/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #119 +# Perl Weekly Challenge #120 You can find more information about this weeks, and previous weeks challenges at: @@ -14,297 +14,15 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/ja # Task 1 - Swap Nibbles -***You are given a positive integer `$N`. Write a script to swap the two nibbles of the binary representation of the given number and print the decimal number of the new binary representation.((( +***You are given a positive integer `$N` less than or equal to `255`. Write a script to swap the odd positioned bit with even positioned bit and print the decimal equivalent of the new binary representation.*** ## The solution -This is a simple binary manipulation problem. +# Task 1 - Clock Angle -We can isolate the lower-nibble by bit-wise `&`ing with `15` or `00001111`. We then move it to the upper-nibble by multiplying by `16` OR using the bit-shift operator `<<` to move the bits 4 spaces to the left. - -We can isolate the upper-nibble by bit-wise `&`ing with `240` or `11110000`, and again use the bit-shift operator `>>` to move 4 bits to the right. BUT actually we don't need to perform the bit-wise `&` IF we assume the number is in range (`0`..`255`). As the `>>` operator just drops the bits shifted off. - -To stitch it back together we just `|` the two numbers together. - -```perl -sub swap_nibble { - return ($_[0]>>4) | (($_[0]&15)<<4); -} -``` - -## Other languages - -I've included some other language versions of this code... - -The first 3 are just direct lifts as Javascript, PHP and Python all have bit shift and bitwise operators! The fourth tho' is a bit of curveball "CESIL" the first -language I was "taught" at school - the code was run from punch cards and the printout was on green bar paper! - -### Javascript -```javascript -TESTS = [ [101,86],[18,33] ]; - -TESTS.forEach(_ => console.log( swap_nibble( _[0] ) == _[1] ? 'OK' : '--' ) ); -TESTS.forEach(_ => console.log( swap_nibble( _[1] ) == _[0] ? 'OK' : '--' ) ); - -function swap_nibble(_) { - return (_>>4) | ((_&15)<<4); -} -``` - -### PHP -```php ->4) | (($_&15)<<4); -} -``` - -### Python -```python -TESTS=[[15,240],[165,90]] - -def swap_nibbles(_): - return (_>>4)|((_&15)<<4) - -for t in TESTS: - print( 'OK' if swap_nibbles(t[0])==t[1] else 'Not OK' ) - print( 'OK' if swap_nibbles(t[1])==t[0] else 'Not OK' ) - -``` -### CESIL - -Thought I'd throw in a slightly curve ball - CESIL "*Computer Education in Schools Instructional Language*" written by ICL was the -first language I was formally taught - back in the days when you had to learn how computers worked at bare bones. So it had a -reduced instruction set with just 14 commands - ADD/SUBTRACT/MULTIPLY/DIVIDE, IN/LOAD/STORE, JUMP/JIZERO/JINEG, PRINT/OUT/LINE & HALT. The layout -of code is "structured" with 8 character indent as it was designed to be stored/run on punch-cards... {The reason the "input" appears after a card -with % in it}. - -CESIL was designed to teach "machine-code" to Computer science students - so other than the "I/O" commands everything else was at a basic operation level. - -**Note:** Interestingly in just 26 lines of code - all 14 of the instructions are used...! - -``` - LINE -Next IN - JINEG End - OUT - PRINT " => " - STORE a - IN - STORE c - LOAD a - DIVIDE 16 - STORE b - MULTIPLY -16 - ADD a - MULTIPLY 16 - ADD b - OUT - PRINT " : " - SUBTRACT c - JIZERO Ok - PRINT "--" - JUMP Line -Ok PRINT "OK" -Line LINE - JUMP Next -End LINE - HALT - % - 240 - 15 - 15 - 240 - 0 - 0 - 255 - 255 - 99 - 54 - -1 -``` -### Side note... an intepreter for CESIL... - -Didn't like the idea of relying on JAVA... so here is a bare bones -interpreter... This uses a dispatch table to execute the commands -- a list of "anonymous" subroutines stored in a hash. - -```perl -use strict; -use warnings; - -## Initialize state -my($MAX,$ptr,$reg,@in,%mem,@code,%ptrs)=(1e6,0,0); - -## Define error messages -my %messages = ( -'i','No further input','d','Division by zero ', -'l','Unknown pointer ','m','Unitialized memory at '); - -## Support functions -sub _e { die sprintf "\n** %s%s [%s @ %d]\n", - $messages{$_[0]},@{$code[$ptr]}[1,0],1+$ptr} -sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_e 'l'} -sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_e 'm'} - -## Command dispatch table -my %commands = ( -'LINE' ,sub{print "\n"}, -'OUT' ,sub{print $reg}, -'PRINT' ,sub{print s/^"//r=~s/"$//r}, -'IN' ,sub{@in?($reg=shift@in):_e 'i'}, -'STORE' ,sub{$mem{$_}=$reg}, -'LOAD' ,sub{$reg=_v}, -'ADD' ,sub{$reg+=_v}, -'SUBTRACT',sub{$reg-=_v}, -'MULTIPLY',sub{$reg*=_v}, -'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_e 'd'}, -'JINEG' ,sub{_j if $reg<0}, -'JIZERO' ,sub{_j if !$reg}, -'JUMP' ,sub{_j}, -'HALT' ,sub{exit}, -); - -## Parser loop -while(<>) { - ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/; - ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/; - my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2; - die "\n## Unknown command [$cmd @ ",1+@code,"]\n" - unless exists $commands{$cmd}; - push @code, [$cmd,$data//'']; -} -## Execution loop -($commands{$code[$ptr][0]}($_=$code[$ptr][1]),$ptr++) - while--$MAX&& $ptr<@code; -die "\n** Exited without HALT\n"; -``` -# Task 2 - Sequence without 1-on-1 - -***Write a script to generate sequence starting at 1. Consider the increasing -sequence of integers which contain only 1’s, 2’s and 3’s, and do not have any -doublets of 1’s like below. Please accept a positive integer `$N` and print -the `$N`th term in the generated sequence.*** - -``` -1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... -``` +***You are given time `$T` in the format `hh:mm`. Write a script to find the smaller angle formed by the hands of an analog clock at a given time.*** ## The solution -Note this is a first pass solution.... - -To avoid splitting/unsplitting - we will keep the digits as an array. -We then just increment the last entry of the array, and carry over -any bits to the previous entry. Finally if we get to the start of the -array we unshift a 1 in front of it... - -In the outer loop we compute the next number - but we don't increment -the counter if the number has two adjacent 1s. - -```perl -sub no_11_array { - my( $n, $ptr, @v ) = ( shift, undef, 0 ); - for( my $i = 0; $i < $n; ) { - for( $ptr = $#v; $ptr>-1 && ++$v[$ptr]>3; $ptr--) { - $v[$ptr]=1; - } - unshift @v,1 if $ptr < 0; - $i++ unless "@v"=~m{1 1}; - } - return join q(),@v; -} -``` - -Comparing this to the pure script using numbers and filtering out those -that contain any of the digits `0`, `4` through `9` or the string `11` -using a regexp sees an approximately `15x` speed up. If you optimize -the filter using a series of index calls you can double the speed of the -code at the expense of a slightly longer function BUT it is still less -than a seventh of the performance of the array version. - -```perl -sub no_11_filter { - my $n = shift; - my $v = 0; - while(1){ - return $v unless $n; - $v++; - $n-- if 0 > index( $v,'4' ) && 0 > index( $v,'0' ) - && 0 > index( $v,'5' ) && 0 > index( $v,'6' ) - && 0 > index( $v,'7' ) && 0 > index( $v,'8' ) - && 0 > index( $v,'9' ) && 0 > index( $v,'11' ); - } -} - -``` - -### Making the code more *readable*.... - -This is a nice challenge to make the code more "readable", by encapsulating our new "number" string as an object. - -We will overload it's increment operator so that we can generate the sequence - -``` - 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33, 111 -``` - -The object itself is just a blessed array. - -We use `overload` to define both the "increment" operator `++` and the "stringfy" -operator "". - -We then define a simple method to test whether or not the "string" contains a double 1. - -The package code is below - but first the loop in the code: - -```perl -sub no_11_object { - my( $n, $v ) = ( shift, Three->new ); - for( my $i = 0; $i < $n; $v++, $i++ ) { - $v++ while $v->has_double_one; - } - return "$v"; -} -``` - -Although the code is "cleaner" performance is affected - it is about half the speed of the optimal (array) solution but still 4 times as fast as the (non-regex) filter method. -```perl -package Three; - -use overload '++' => 'incr'; -use overload '""' => 'str'; - -sub new { - return bless [], 'Three'; -} - -sub has_double_one { - my( $f, @v ) = @{$_[0]}; - ( $f == 1 && $v[0] == 1 ) ? ( return 1 ) : ( $f = shift @v ) while @v; - return 0; -} - -sub incr { - my($v,$ptr) = (shift,-1); - for( $ptr = $#$v; $ptr>-1 && ++$v->[$ptr]>3; $ptr--) { - $v->[$ptr]=1; - } - unshift @{$v}, 1 if $ptr < 0; -} - -sub str { - return join '',@{$_[0]}; -} -1; -``` -- cgit From cddea5572d8932a82bb6065f363e455bb7c7d564 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:56:44 +0100 Subject: typo --- challenge-119/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md index 663a88f2d2..ee078890c0 100644 --- a/challenge-119/james-smith/README.md +++ b/challenge-119/james-smith/README.md @@ -14,7 +14,7 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/ja # Task 1 - Swap Nibbles -***You are given a positive integer `$N`. Write a script to swap the two nibbles of the binary representation of the given number and print the decimal number of the new binary representation.((( +***You are given a positive integer `$N`. Write a script to swap the two nibbles of the binary representation of the given number and print the decimal number of the new binary representation.*** ## The solution -- cgit From a486123da38802daf76db5248bbab841a70a54eb Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 11:58:06 +0100 Subject: fix --- challenge-120/james-smith/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-120/james-smith/README.md b/challenge-120/james-smith/README.md index 697c8c702a..a45bcb3af2 100644 --- a/challenge-120/james-smith/README.md +++ b/challenge-120/james-smith/README.md @@ -12,13 +12,13 @@ You can find the solutions here on github at: https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/james-smith/perl -# Task 1 - Swap Nibbles +# Task 1 - Swap bits ***You are given a positive integer `$N` less than or equal to `255`. Write a script to swap the odd positioned bit with even positioned bit and print the decimal equivalent of the new binary representation.*** ## The solution -# Task 1 - Clock Angle +# Task 2 - Clock Angle ***You are given time `$T` in the format `hh:mm`. Write a script to find the smaller angle formed by the hands of an analog clock at a given time.*** -- cgit From 21efa6593b05c64b48fe66e0321d9ddba14ced75 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 12:14:37 +0100 Subject: faster 1 liner for clock angle --- challenge-120/james-smith/perl/ch-2.pl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl index f34d666f9b..0e1b595f78 100644 --- a/challenge-120/james-smith/perl/ch-2.pl +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -14,9 +14,14 @@ my @TESTS = ( ); is( clock_angle($_->[0]), $_->[1] ) foreach @TESTS; - +is( clock_angle_1_liner($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); +cmpthese( 2_000_000, { + 'f' => sub { clock_angle($_->[0]) foreach @TESTS }, + '1' => sub { clock_angle_1_liner($_->[0]) foreach @TESTS }, +}); + sub clock_angle { ## The difference is: hr*30+min/2 - min*12 ## Modulo is int based so to avoid issue @@ -29,3 +34,6 @@ sub clock_angle { return $a > 180 ? 360-$a : $a; } +sub clock_angle_1_liner { + 180-abs(abs(60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); +} -- cgit From 56eb2e9290365c1d5e37d8b54bae091efc6acf23 Mon Sep 17 00:00:00 2001 From: Mark A Date: Mon, 5 Jul 2021 05:33:10 -0600 Subject: Challenge 120 Solutions (Raku) --- challenge-120/mark-anderson/raku/ch-1.raku | 24 ++++++++++++++++++++++++ challenge-120/mark-anderson/raku/ch-2.raku | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-120/mark-anderson/raku/ch-1.raku create mode 100644 challenge-120/mark-anderson/raku/ch-2.raku diff --git a/challenge-120/mark-anderson/raku/ch-1.raku b/challenge-120/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..476b9433af --- /dev/null +++ b/challenge-120/mark-anderson/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use Test; +plan 9; + +my @tests = 101, 154, + 18, 33, + 255, 255, + 1, 2, + 170, 85, + 85, 170, + 204, 204, + 9, 6, + 10, 5; + +for @tests -> $a, $b +{ + is swap-odd-even-bits($a), $b +} + +sub swap-odd-even-bits($N where $N ~~ 1..255) +{ + $N.fmt('%08b').comb[ 1,0,3,2,5,4,7,6 ].join.parse-base(2) +} diff --git a/challenge-120/mark-anderson/raku/ch-2.raku b/challenge-120/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..ad2433716b --- /dev/null +++ b/challenge-120/mark-anderson/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use Test; +plan 9; + +my @tests = '03:10', 35, + '04:00', 120, + '09:20', 160, + '09:40', 50, + '10:45', 52.5, + '10:55', 2.5, + '12:00', 0, + '11:59', 5.5, + '12:01', 5.5; + +for @tests -> $a, $b +{ + is clock-angle($a), $b +} + +sub clock-angle($T) +{ + my ($h, $m) = $T.split(':'); + + my $angle = abs($h * 30 + $m / 2 - $m * 6); + + min($angle, 360 - $angle); +} -- cgit From 5818b1052ffabb13453c8bd3df275d89b1b4b88a Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Mon, 5 Jul 2021 12:36:05 +0100 Subject: Challenge #120 --- challenge-120/dave-cross/perl/ch-1.pl | 17 +++++++++++++++++ challenge-120/dave-cross/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-120/dave-cross/perl/ch-1.pl create mode 100644 challenge-120/dave-cross/perl/ch-2.pl diff --git a/challenge-120/dave-cross/perl/ch-1.pl b/challenge-120/dave-cross/perl/ch-1.pl new file mode 100644 index 0000000000..8453e2e8ee --- /dev/null +++ b/challenge-120/dave-cross/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +say oct '0b' . join '', map { scalar reverse } get_number() =~ /(\d\d)/g; + +sub get_number { + my $err = "Give me an integer less than 256\n"; + + die $err unless @ARGV; + die $err if $ARGV[0] =~ /\D/; + die $err if $ARGV[0] >= 256; + + return sprintf '%08b', $ARGV[0]; +} diff --git a/challenge-120/dave-cross/perl/ch-2.pl b/challenge-120/dave-cross/perl/ch-2.pl new file mode 100644 index 0000000000..8fbad6eeed --- /dev/null +++ b/challenge-120/dave-cross/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my ($hr, $min) = get_time(); + +# 30 degrees per hour +my $hr_angle = $hr * 30; +# Plus the bit for minutes past the hour +$hr_angle += ($min / 60) * 30; + +my $min_angle = ($min / 60) * 360; + +my $angle = abs( $hr_angle - $min_angle); + +$angle = 360 - $angle if $angle > 180; + +say $angle; + +sub get_time { + my $err = 'Give me a valid time (HH:MM)'; + + die $err unless @ARGV; + die $err if $ARGV[0] !~ /^\d\d:\d\d$/; + + my ($h, $m) = $ARGV[0] =~ /(\d\d):(\d\d)/; + + die $err if $h > 12 or $m > 59; + + return ($h, $m); +} -- cgit From 5781ed67de237f213f82a66f0c236030abe1aa8d Mon Sep 17 00:00:00 2001 From: Lucas Ransan Date: Mon, 5 Jul 2021 12:45:09 +0200 Subject: Raku week 120 task 1 --- challenge-120/luc65r/raku/ch-1.raku | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 challenge-120/luc65r/raku/ch-1.raku diff --git a/challenge-120/luc65r/raku/ch-1.raku b/challenge-120/luc65r/raku/ch-1.raku new file mode 100755 index 0000000000..2c93aebb8d --- /dev/null +++ b/challenge-120/luc65r/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku + +unit sub MAIN(UInt:D $n where * < 256); + +say $n +& 0x55 +< 1 +| $n +& 0xAA +> 1; -- cgit From f67617ecbd3d666d3f5129679d3215729dfbbaab Mon Sep 17 00:00:00 2001 From: Lucas Ransan Date: Mon, 5 Jul 2021 13:41:40 +0200 Subject: Raku week 120 task 2 --- challenge-120/luc65r/raku/ch-2.raku | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 challenge-120/luc65r/raku/ch-2.raku diff --git a/challenge-120/luc65r/raku/ch-2.raku b/challenge-120/luc65r/raku/ch-2.raku new file mode 100755 index 0000000000..64270d3db0 --- /dev/null +++ b/challenge-120/luc65r/raku/ch-2.raku @@ -0,0 +1,9 @@ +#!/usr/bin/env raku + +unit sub MAIN($time where /^ (\d\d) ':' (\d\d) $/); + +my $m = $1.Int / 60; +my $h = ($0.Int mod 12 + $m) / 12; +my $d = abs($h - $m); + +say "{min($d, 1 - $d) * 360}°"; -- cgit From 127da3751b077475421c1624462d7214cff97d40 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 5 Jul 2021 12:50:02 +0100 Subject: Update README.md --- challenge-120/james-smith/README.md | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/challenge-120/james-smith/README.md b/challenge-120/james-smith/README.md index a45bcb3af2..c87399295c 100644 --- a/challenge-120/james-smith/README.md +++ b/challenge-120/james-smith/README.md @@ -18,11 +18,52 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/ja ## The solution +This is very similar to last weeks challenge - but instead of swapping bits 4-7 with bits 0-3 this time we are swapping even bits with odd bits - so the solution is similar... + +The even bits are found by bit-wise *and*ing the number with `0b01010101` 0r `0x55` (`85` decimal) and the odd bits are found by bit-wise anding with `0b10101010` or `0xaa` (`170` decimal). We then switch them by multiplying or dividing by 2 {which we do with the bit shift operators `<<` & `>>`}. We just have to add the two values together - as we know the bits don't overlap we can use `|` rather than `+`. + +```perl +sub switch_bits { + ($_[0]&0xaa)>>1 | ($_[0]&0x55)<<1; +} +``` + # Task 2 - Clock Angle ***You are given time `$T` in the format `hh:mm`. Write a script to find the smaller angle formed by the hands of an analog clock at a given time.*** ## The solution +Given a time we can compute the position of the hour hand as `$hr*30+$min/2` and the minute hand as `$min*6`. + +We can then get the angle between the two as: `($hr*60+$min-$min*12)/2` + +We want to reduce this modulo `360` to map the value to the range `0` - `360`. **BUT** we note that `$min` may be odd and the value of the angle may therefore may not be a whole number. To get round this we perform the modulus (by `720`) before the divide by `2`. + +This gives us a number between `0` and `360`. But we need a number between `0` and `180`. To map the range `180` - `360` we can either use an IF OR we can note that if we +do `abs(angle - 180)` we get the complementary angle. So we just subtract this from `180` giving the method below... + +```perl +sub clock_angle_1_liner { + 180-abs((60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); +} +``` + +Compared to this (perhaps slightly more readable) method there is about a 60% performance gain [avoiding variables!] +```perl +sub clock_angle { + my($h,$m) = split /:/,shift; + my $t = abs($h*60-$m*11)%720/2; + return $t > 180 ? 360-$t : $t; +} +``` + +**Note** We can gain more speed (90% faster than "simple" method and about 15% faster than the 1-liner) by removing the first `substr` but to do so we need to disable `numeric` warnings - `no warnings qw(numeric)`. +```perl +sub clock_angle_fast { + 180-abs((60*$_[0]-11*substr$_[0],3)%720/2-180); +} +``` +(Without disabling warnings this gives `Argument "04:00" isn't numeric in multiplication (*) at ..` errors) -- cgit From a8590c122cd7cafb611a27ebecf933aa76b1a494 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 12:50:35 +0100 Subject: added speed --- challenge-120/james-smith/perl/ch-2.pl | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl index 0e1b595f78..83f4dc12d6 100644 --- a/challenge-120/james-smith/perl/ch-2.pl +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -7,19 +7,25 @@ use feature qw(say); use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); +no warnings 'numeric'; my @TESTS = ( [ '03:10', 35 ], [ '04:00', 120 ], + [ '08:00', 120 ], + [ '16:00', 120 ], + [ '18:00', 180 ], + [ '20:00', 120 ], ); is( clock_angle($_->[0]), $_->[1] ) foreach @TESTS; is( clock_angle_1_liner($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); -cmpthese( 2_000_000, { - 'f' => sub { clock_angle($_->[0]) foreach @TESTS }, +cmpthese( 1_000_000, { + 's' => sub { clock_angle($_->[0]) foreach @TESTS }, '1' => sub { clock_angle_1_liner($_->[0]) foreach @TESTS }, + 'f' => sub { clock_angle_fast($_->[0]) foreach @TESTS }, }); sub clock_angle { @@ -30,10 +36,14 @@ sub clock_angle { ## If value is > 180 then we subtract from ## 360.... my($h,$m) = split /:/,shift; - my $a = abs($h*60-$m*11)%720/2; - return $a > 180 ? 360-$a : $a; + my $t = abs($h*60-$m*11)%720/2; + return $t > 180 ? 360-$t : $t; } sub clock_angle_1_liner { - 180-abs(abs(60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); + 180-abs((60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); +} + +sub clock_angle_fast { + 180-abs((60*$_[0]-11*substr$_[0],3)%720/2-180); } -- cgit From c3788008190efe2478a582345309ab44096b5bce Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 12:52:20 +0100 Subject: missed tests --- challenge-120/james-smith/perl/ch-2.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl index 83f4dc12d6..d1084ede19 100644 --- a/challenge-120/james-smith/perl/ch-2.pl +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -20,7 +20,8 @@ my @TESTS = ( is( clock_angle($_->[0]), $_->[1] ) foreach @TESTS; is( clock_angle_1_liner($_->[0]), $_->[1] ) foreach @TESTS; -done_testing(); +is( clock_angle_fast($_->[0]), $_->[1] ) foreach @TESTS; +done_testing; cmpthese( 1_000_000, { 's' => sub { clock_angle($_->[0]) foreach @TESTS }, -- cgit From 24fb6d5b8b7efafb92c715c2776b6f7a0e388637 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 5 Jul 2021 14:03:33 +0200 Subject: Task1 done --- challenge-120/luca-ferrari/raku/ch-1.p6 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 challenge-120/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-120/luca-ferrari/raku/ch-1.p6 b/challenge-120/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..b5cf544414 --- /dev/null +++ b/challenge-120/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#!raku + + +sub MAIN( Int $N where { 0 < $N < 255 } ) { + my @bits = '%08s'.sprintf( $N.base( 2 ) ).split( '', :skip-empty ); + + my @rotated-bits; + for my @bits -> $odd, $even { + @rotated-bits.push: $even, $odd; + } + + + @rotated-bits.join.Str.parse-base( 2 ).say; +} -- cgit From 747b4d5343772309e085ecd579a8b34650a31e2d Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 5 Jul 2021 13:07:17 +0100 Subject: Update README.md --- challenge-120/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-120/james-smith/README.md b/challenge-120/james-smith/README.md index c87399295c..4f84100cee 100644 --- a/challenge-120/james-smith/README.md +++ b/challenge-120/james-smith/README.md @@ -10,7 +10,7 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-120/james-smith/perl # Task 1 - Swap bits -- cgit From 52d35e60583bb87606501c9606891bc32a510223 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 5 Jul 2021 13:07:39 +0100 Subject: Create blog.txt --- challenge-120/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-120/james-smith/blog.txt diff --git a/challenge-120/james-smith/blog.txt b/challenge-120/james-smith/blog.txt new file mode 100644 index 0000000000..87ccf73375 --- /dev/null +++ b/challenge-120/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-120/james-smith -- cgit From 9de1c474c3a7eff7bdd83b8cd7d5bf8977fe7bcc Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 5 Jul 2021 14:10:40 +0200 Subject: Task2 done --- challenge-120/luca-ferrari/raku/ch-2.p6 | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 challenge-120/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-120/luca-ferrari/raku/ch-2.p6 b/challenge-120/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..6727ba997b --- /dev/null +++ b/challenge-120/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,12 @@ +#!raku + + +sub MAIN( Str $time where { $time ~~ / ^ \d ** 2 ':' \d ** 2 $ / } ) { + my ( $hour, $minute ) = $time.split( ':', :skip-empty ); + + $hour %= 12; + $minute %= 60; + $minute /= 5; + + say abs( $hour - $minute ) * 30; +} -- cgit From a34551b3241a8ce24e844669ef7a9e96d3b53bfc Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 5 Jul 2021 13:27:54 +0100 Subject: Update README.md --- challenge-120/james-smith/README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/challenge-120/james-smith/README.md b/challenge-120/james-smith/README.md index 4f84100cee..d751eefe8c 100644 --- a/challenge-120/james-smith/README.md +++ b/challenge-120/james-smith/README.md @@ -38,31 +38,31 @@ Given a time we can compute the position of the hour hand as `$hr*30+$min/2` and We can then get the angle between the two as: `($hr*60+$min-$min*12)/2` -We want to reduce this modulo `360` to map the value to the range `0` - `360`. **BUT** we note that `$min` may be odd and the value of the angle may therefore may not be a whole number. To get round this we perform the modulus (by `720`) before the divide by `2`. +To make sure the angle is between `-360` and `360` we have to ensure that the hour is between `0` and `11`, by reducing it module 12. We then get the absolute value so we have a value between `0` and `360` -This gives us a number between `0` and `360`. But we need a number between `0` and `180`. To map the range `180` - `360` we can either use an IF OR we can note that if we +We need a number between `0` and `180`. To map the range `180` - `360` we can either use an IF OR we can note that if we do `abs(angle - 180)` we get the complementary angle. So we just subtract this from `180` giving the method below... ```perl sub clock_angle_1_liner { - 180-abs((60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); + 180-abs(abs((substr$_[0],0,2)%12*30-5.5*substr$_[0],3)-180); } ``` -Compared to this (perhaps slightly more readable) method there is about a 60% performance gain [avoiding variables!] +Compared to this (perhaps slightly more readable) method there is about a 85% performance gain [avoiding variables!] ```perl sub clock_angle { my($h,$m) = split /:/,shift; - my $t = abs($h*60-$m*11)%720/2; + my $t = abs($h%12*30-$m*5.5); return $t > 180 ? 360-$t : $t; } ``` -**Note** We can gain more speed (90% faster than "simple" method and about 15% faster than the 1-liner) by removing the first `substr` but to do so we need to disable `numeric` warnings - `no warnings qw(numeric)`. +**Note** We can gain more speed (110% faster than "simple" method and about 15% faster than the 1-liner) by removing the first `substr` but to do so we need to disable `numeric` warnings - `no warnings qw(numeric)`. ```perl sub clock_angle_fast { - 180-abs((60*$_[0]-11*substr$_[0],3)%720/2-180); + 180-abs(abs($_[0]%12*30-5.5*substr$_[0],3)-180); } ``` -- cgit From e0e48a4be0ef24804b201dc94461b472e9082221 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 5 Jul 2021 13:28:32 +0100 Subject: slightly speed gain --- challenge-120/james-smith/perl/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-120/james-smith/perl/ch-2.pl b/challenge-120/james-smith/perl/ch-2.pl index d1084ede19..7b0406a874 100644 --- a/challenge-120/james-smith/perl/ch-2.pl +++ b/challenge-120/james-smith/perl/ch-2.pl @@ -37,14 +37,14 @@ sub clock_angle { ## If value is > 180 then we subtract from ## 360.... my($h,$m) = split /:/,shift; - my $t = abs($h*60-$m*11)%720/2; + my $t = abs($h%12*30-$m*5.5); return $t > 180 ? 360-$t : $t; } sub clock_angle_1_liner { - 180-abs((60*(substr$_[0],0,2)-11*substr$_[0],3)%720/2-180); + 180-abs(abs((substr$_[0],0,2)%12*30-5.5*substr$_[0],3)-180); } sub clock_angle_fast { - 180-abs((60*$_[0]-11*substr$_[0],3)%720/2-180); + 180-abs(abs($_[0]%12*30-5.5*substr$_[0],3)-180); } -- cgit From c79281808d6b8968f5bd105536df88b345bded7d Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 14:37:17 +0200 Subject: Tests for week 120 --- challenge-120/abigail/t/ctest.ini | 12 ++ challenge-120/abigail/t/input-1-1 | 2 + challenge-120/abigail/t/input-1-2 | 255 +++++++++++++++++++++++++++++++++ challenge-120/abigail/t/input-2-1 | 2 + challenge-120/abigail/t/output-1-1.exp | 2 + challenge-120/abigail/t/output-1-2.exp | 255 +++++++++++++++++++++++++++++++++ challenge-120/abigail/t/output-2-1.exp | 2 + 7 files changed, 530 insertions(+) create mode 100644 challenge-120/abigail/t/ctest.ini create mode 100644 challenge-120/abigail/t/input-1-1 create mode 100644 challenge-120/abigail/t/input-1-2 create mode 100644 challenge-120/abigail/t/input-2-1 create mode 100644 challenge-120/abigail/t/output-1-1.exp create mode 100644 challenge-120/abigail/t/output-1-2.exp create mode 100644 challenge-120/abigail/t/output-2-1.exp diff --git a/challenge-120/abigail/t/ctest.ini b/challenge-120/abigail/t/ctest.ini new file mode 100644 index 0000000000..4f84a004d3 --- /dev/null +++ b/challenge-120/abigail/t/ctest.ini @@ -0,0 +1,12 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +1-2 = Numbers 1 to 255 +2-1 = Given Examples + +[challenges/1/bc] +add_to_input = 0 diff --git a/challenge-120/abigail/t/input-1-1 b/challenge-120/abigail/t/input-1-1 new file mode 100644 index 0000000000..1229000578 --- /dev/null +++ b/challenge-120/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +101 +18 diff --git a/challenge-120/abigail/t/input-1-2 b/challenge-120/abigail/t/input-1-2 new file mode 100644 index 0000000000..99f18623a6 --- /dev/null +++ b/challenge-120/abigail/t/input-1-2 @@ -0,0 +1,255 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 diff --git a/challenge-120/abigail/t/input-2-1 b/challenge-120/abigail/t/input-2-1 new file mode 100644 index 0000000000..2fc5888d95 --- /dev/null +++ b/challenge-120/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +03:10 +04:00 diff --git a/challenge-120/abigail/t/output-1-1.exp b/challenge-120/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..1eaeeb42db --- /dev/null +++ b/challenge-120/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +154 +33 diff --git a/challenge-120/abigail/t/output-1-2.exp b/challenge-120/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..e6bb766d0d --- /dev/null +++ b/challenge-120/abigail/t/output-1-2.exp @@ -0,0 +1,255 @@ +2 +1 +3 +8 +10 +9 +11 +4 +6 +5 +7 +12 +14 +13 +15 +32 +34 +33 +35 +40 +42 +41 +43 +36 +38 +37 +39 +44 +46 +45 +47 +16 +18 +17 +19 +24 +26 +25 +27 +20 +22 +21 +23 +28 +30 +29 +31 +48 +50 +49 +51 +56 +58 +57 +59 +52 +54 +53 +55 +60 +62 +61 +63 +128 +130 +129 +131 +136 +138 +137 +139 +132 +134 +133 +135 +140 +142 +141 +143 +160 +162 +161 +163 +168 +170 +169 +171 +164 +166 +165 +167 +172 +174 +173 +175 +144 +146 +145 +147 +152 +154 +153 +155 +148 +150 +149 +151 +156 +158 +157 +159 +176 +178 +177 +179 +184 +186 +185 +187 +180 +182 +181 +183 +188 +190 +189 +191 +64 +66 +65 +67 +72 +74 +73 +75 +68 +70 +69 +71 +76 +78 +77 +79 +96 +98 +97 +99 +104 +106 +105 +107 +100 +102 +101 +103 +108 +110 +109 +111 +80 +82 +81 +83 +88 +90 +89 +91 +84 +86 +85 +87 +92 +94 +93 +95 +112 +114 +113 +115 +120 +122 +121 +123 +116 +118 +117 +119 +124 +126 +125 +127 +192 +194 +193 +195 +200 +202 +201 +203 +196 +198 +197 +199 +204 +206 +205 +207 +224 +226 +225 +227 +232 +234 +233 +235 +228 +230 +229 +231 +236 +238 +237 +239 +208 +210 +209 +211 +216 +218 +217 +219 +212 +214 +213 +215 +220 +222 +221 +223 +240 +242 +241 +243 +248 +250 +249 +251 +244 +246 +245 +247 +252 +254 +253 +255 diff --git a/challenge-120/abigail/t/output-2-1.exp b/challenge-120/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..7b477e88f2 --- /dev/null +++ b/challenge-120/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +35 +120 -- cgit From 78c841faea1060a3b7ea63a4316e461e490d275e Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 14:38:06 +0200 Subject: Solutions in 15 languages for week 120, part 1. This is basically the same as last weeks challenge... --- challenge-120/abigail/README.md | 1 - challenge-120/abigail/awk/ch-1.awk | 21 +++++++++++++++++++++ challenge-120/abigail/bash/ch-1.sh | 16 ++++++++++++++++ challenge-120/abigail/bc/ch-1.bc | 27 +++++++++++++++++++++++++++ challenge-120/abigail/c/ch-1.c | 22 ++++++++++++++++++++++ challenge-120/abigail/go/ch-1.go | 25 +++++++++++++++++++++++++ challenge-120/abigail/java/ch-1.java | 27 +++++++++++++++++++++++++++ challenge-120/abigail/lua/ch-1.lua | 27 +++++++++++++++++++++++++++ challenge-120/abigail/node/ch-1.js | 16 ++++++++++++++++ challenge-120/abigail/pascal/ch-1.p | 22 ++++++++++++++++++++++ challenge-120/abigail/perl/ch-1.pl | 29 +