From fa5731052dcccec7fd0c77d96266690d2841b815 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 7 Dec 2020 12:06:06 -0800 Subject: Ch90: prep for challenge --- challenge-090/tyler-wardhaugh/clojure/README.md | 10 +++++----- challenge-090/tyler-wardhaugh/clojure/deps.edn | 10 +++------- challenge-090/tyler-wardhaugh/clojure/pom.xml | 24 +++++++----------------- challenge-090/tyler-wardhaugh/lua/README.md | 6 +++--- 4 files changed, 18 insertions(+), 32 deletions(-) (limited to 'challenge-090') diff --git a/challenge-090/tyler-wardhaugh/clojure/README.md b/challenge-090/tyler-wardhaugh/clojure/README.md index f5a89af18d..41000b8199 100644 --- a/challenge-090/tyler-wardhaugh/clojure/README.md +++ b/challenge-090/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c89 +# tw.weekly.c90 -The Weekly Challenge - #089 - Tyler Wardhaugh +The Weekly Challenge - #090 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c89.core + $ clojure -M -m tw.weekly.c90.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c89.t1 N + $ clojure -M -m tw.weekly.c90.t1 DNA Run Task #2: - $ clojure -M -m tw.weekly.c89.t2 + $ clojure -M -m tw.weekly.c90.t2 A B ## Project Template diff --git a/challenge-090/tyler-wardhaugh/clojure/deps.edn b/challenge-090/tyler-wardhaugh/clojure/deps.edn index cfd5d782d1..1fae9ef933 100644 --- a/challenge-090/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-090/tyler-wardhaugh/clojure/deps.edn @@ -1,9 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/core.logic {:mvn/version "1.0.0"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"} - org.clojure/math.numeric-tower {:mvn/version "0.0.4"} - org.clojure/tools.macro {:mvn/version "0.1.2"}} + :deps {org.clojure/clojure {:mvn/version "1.10.1"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} @@ -14,5 +10,5 @@ :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.94"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c88.jar" - "-C" "-m" "tw.weekly.c88"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c90.jar" + "-C" "-m" "tw.weekly.c90"]}}} diff --git a/challenge-090/tyler-wardhaugh/clojure/pom.xml b/challenge-090/tyler-wardhaugh/clojure/pom.xml index 3097f51f8e..bc8a82e4cd 100644 --- a/challenge-090/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-090/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c89 + tw.weekly.c90 0.1.0-SNAPSHOT - tw.weekly.c89 - Challenge #089 - https://github.com/tw.weekly/tw.weekly.c89 + tw.weekly.c90 + Challenge #090 + https://github.com/tw.weekly/tw.weekly.c90 Eclipse Public License @@ -19,9 +19,9 @@ - https://github.com/tw.weekly/tw.weekly.c89 - scm:git:git://github.com/tw.weekly/tw.weekly.c89.git - scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c89.git + https://github.com/tw.weekly/tw.weekly.c90 + scm:git:git://github.com/tw.weekly/tw.weekly.c90.git + scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c90.git HEAD @@ -30,16 +30,6 @@ clojure 1.10.1 - - net.mikera - core.matrix - 0.62.0 - - - org.clojure - math.numeric-tower - 0.0.4 - src diff --git a/challenge-090/tyler-wardhaugh/lua/README.md b/challenge-090/tyler-wardhaugh/lua/README.md index d2f91b15fe..8772dac9c1 100644 --- a/challenge-090/tyler-wardhaugh/lua/README.md +++ b/challenge-090/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #089 - Tyler Wardhaugh +The Weekly Challenge - #090 - Tyler Wardhaugh ## Usage Run Task 1: - $ ./run.lua ch-1 N + $ ./run.lua ch-1 DNA Run Task 2: - $ ./run.lua ch-2 MATRIX-FILE + $ ./run.lua ch-2 A B Run the project's tests (all the samples from the task descriptions plus some others): -- cgit From d6eeb2228b999315263e96d4b00bdf6f8d7c090e Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 7 Dec 2020 14:26:21 -0600 Subject: Challenge 90 Raku solutions --- challenge-090/aaronreidsmith/raku/ch-1.raku | 14 ++++++++++++++ challenge-090/aaronreidsmith/raku/ch-2.raku | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-090/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-090/aaronreidsmith/raku/ch-2.raku (limited to 'challenge-090') diff --git a/challenge-090/aaronreidsmith/raku/ch-1.raku b/challenge-090/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..96adad1541 --- /dev/null +++ b/challenge-090/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env perl6 + +subset ValidDna of Str where { $_ ~~ /^^[A|T|G|C]+$$/ } + +# Since DNA is generally read from 5' to 3', I included the option to find the +# reverse compliment in addition to the complement +sub MAIN($dna where $dna ~~ ValidDna, Bool :rc(:$reverse-complement) = False) { + my $translated = $dna.trans('ATGC' => 'TACG'); + if $reverse-complement { + say "5'-{$translated.reverse}-3'"; + } else { + say "3'-$translated-5'" + } +} diff --git a/challenge-090/aaronreidsmith/raku/ch-2.raku b/challenge-090/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..cc85350dc6 --- /dev/null +++ b/challenge-090/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env perl6 + +subset PositiveInt of Int where { $_ ~~ Int && $_ > 0 } + +sub generate-pairs($a, $b) { + sprintf("%02d, %02d", $a, $b).put; + if $a == 1 { + (($a, $b),); + } else { + (($a, $b), |generate-pairs($a div 2, $b * 2)); + } +} + +sub MAIN(PositiveInt $A, PositiveInt $B) { + say "Input: A: $A, B: $B"; + say "Divide A by 2 (ignoring remainders) until it is 1. Multiply B by 2 as we go:"; + my @pairs = generate-pairs($A, $B); + say "Then, wherever A is odd, we add the Bs together:"; + my @odd-bs = @pairs.grep(-> @pair { !(@pair[0] %% 2) }).map(-> @pair { @pair[1] }); + say "{@odd-bs.join(' + ')} = {@odd-bs.sum}"; +} -- cgit From f3bc094d3c6a7cfafdcdcb64298cb078a90e6d66 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 7 Dec 2020 14:53:35 -0600 Subject: reverse -> flip --- challenge-090/aaronreidsmith/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/aaronreidsmith/raku/ch-1.raku b/challenge-090/aaronreidsmith/raku/ch-1.raku index 96adad1541..b0c53e239a 100644 --- a/challenge-090/aaronreidsmith/raku/ch-1.raku +++ b/challenge-090/aaronreidsmith/raku/ch-1.raku @@ -7,7 +7,7 @@ subset ValidDna of Str where { $_ ~~ /^^[A|T|G|C]+$$/ } sub MAIN($dna where $dna ~~ ValidDna, Bool :rc(:$reverse-complement) = False) { my $translated = $dna.trans('ATGC' => 'TACG'); if $reverse-complement { - say "5'-{$translated.reverse}-3'"; + say "5'-{$translated.flip}-3'"; } else { say "3'-$translated-5'" } -- cgit From 0876e01a876b7e4a1cc523d2af1524663106f22f Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 7 Dec 2020 15:18:07 -0600 Subject: Format output --- challenge-090/aaronreidsmith/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/aaronreidsmith/raku/ch-2.raku b/challenge-090/aaronreidsmith/raku/ch-2.raku index cc85350dc6..fa5fe375c6 100644 --- a/challenge-090/aaronreidsmith/raku/ch-2.raku +++ b/challenge-090/aaronreidsmith/raku/ch-2.raku @@ -12,7 +12,7 @@ sub generate-pairs($a, $b) { } sub MAIN(PositiveInt $A, PositiveInt $B) { - say "Input: A: $A, B: $B"; + say "Input: A=$A, B=$B"; say "Divide A by 2 (ignoring remainders) until it is 1. Multiply B by 2 as we go:"; my @pairs = generate-pairs($A, $B); say "Then, wherever A is odd, we add the Bs together:"; -- cgit From 42e642d8c70a32352d990d8490a46fe10c0c9b86 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 7 Dec 2020 15:43:03 -0600 Subject: Add input stats --- challenge-090/aaronreidsmith/raku/ch-1.raku | 3 +++ 1 file changed, 3 insertions(+) (limited to 'challenge-090') diff --git a/challenge-090/aaronreidsmith/raku/ch-1.raku b/challenge-090/aaronreidsmith/raku/ch-1.raku index b0c53e239a..3c12a719c9 100644 --- a/challenge-090/aaronreidsmith/raku/ch-1.raku +++ b/challenge-090/aaronreidsmith/raku/ch-1.raku @@ -5,6 +5,9 @@ subset ValidDna of Str where { $_ ~~ /^^[A|T|G|C]+$$/ } # Since DNA is generally read from 5' to 3', I included the option to find the # reverse compliment in addition to the complement sub MAIN($dna where $dna ~~ ValidDna, Bool :rc(:$reverse-complement) = False) { + say "Input stats:\n{$dna.comb.Bag.Hash}\n"; + + say "Complement:"; my $translated = $dna.trans('ATGC' => 'TACG'); if $reverse-complement { say "5'-{$translated.flip}-3'"; -- cgit From 38277932edb4d62b034b9bc189fafc6fb432f1c4 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 7 Dec 2020 16:50:03 -0600 Subject: Add blog link --- challenge-090/aaronreidsmith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-090/aaronreidsmith/blog.txt (limited to 'challenge-090') diff --git a/challenge-090/aaronreidsmith/blog.txt b/challenge-090/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..c0bf9276a7 --- /dev/null +++ b/challenge-090/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-090/ \ No newline at end of file -- cgit From 4e09c2a1bcfc3828ec9855f7f9a3fa15529118ba Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 7 Dec 2020 22:45:47 -0500 Subject: Challenge 90 in Perl --- challenge-090/dave-jacoby/perl/ch-1.pl | 22 ++++++++++++++++++++ challenge-090/dave-jacoby/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 challenge-090/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-090/dave-jacoby/perl/ch-2.pl (limited to 'challenge-090') diff --git a/challenge-090/dave-jacoby/perl/ch-1.pl b/challenge-090/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..ed5f5e8cc6 --- /dev/null +++ b/challenge-090/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +my $sequence = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG' ; +my $complement = $sequence; +$complement =~ tr/TACG/ATGC/; + +say <<"END"; + Sequence: $sequence + Complement: $complement +END + +for my $i ( qw( A T C G ) ) { + my $n = scalar grep {/$i/} split // , $sequence; + say qq( $i: $n ); +} + + diff --git a/challenge-090/dave-jacoby/perl/ch-2.pl b/challenge-090/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..2973f52f1d --- /dev/null +++ b/challenge-090/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use Scalar::Util qw{looks_like_number}; + +my ( $m, $n ) = map { abs $_ } grep { looks_like_number $_ } @ARGV; + +$m //= 17; +$n //= 38; + +say <<"END"; + + m $m + n $n +END + +say egyptian( $m, $n ); +say $m * $n; + +exit; + +sub egyptian ( $m, $n ) { + my $o = 0; + my $i = 1; + do { + my $e = $m % 2 != 0 ? 1 : 0; + say join "\t", $o, $e, $i, $m, $n; + $o += $n if $e; + $i *= 2; + $m = int $m / 2; + $n = $n * 2; + } while ( $m > 0 ); + return $o; +} -- cgit From 52e0077613ff011285f9e381f774bb9ce56be99a Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 7 Dec 2020 23:42:33 -0500 Subject: Blogged It! --- challenge-090/dave-jacoby/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-090/dave-jacoby/blog.txt (limited to 'challenge-090') diff --git a/challenge-090/dave-jacoby/blog.txt b/challenge-090/dave-jacoby/blog.txt new file mode 100644 index 0000000000..b692db20a0 --- /dev/null +++ b/challenge-090/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/12/08/multiplication-and-dna-perl-weekly-challenge-90.html \ No newline at end of file -- cgit From b687fe1babaa737f2d4d409338262c2f9ad1618c Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Tue, 8 Dec 2020 13:39:52 +0100 Subject: add solutions for challenge-090 in perl --- challenge-090/alexander-pankoff/perl/ch-1.pl | 17 ++++++++++++++ challenge-090/alexander-pankoff/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 challenge-090/alexander-pankoff/perl/ch-1.pl create mode 100755 challenge-090/alexander-pankoff/perl/ch-2.pl (limited to 'challenge-090') diff --git a/challenge-090/alexander-pankoff/perl/ch-1.pl b/challenge-090/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..15f2c016bc --- /dev/null +++ b/challenge-090/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +{ + my ( $sequence ) = @ARGV; + die 'need dna sequence' unless $sequence; + my %complements = ( T => 'A', G => 'C' ); + %complements = ( %complements, reverse %complements ); + + say 'Nucleiobase count: ' . length( $sequence ); + say 'Complementary sequence: ' . join( '', map { $complements{$_} } split( '', $sequence ) ); +} diff --git a/challenge-090/alexander-pankoff/perl/ch-2.pl b/challenge-090/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..51f636cc6f --- /dev/null +++ b/challenge-090/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +use List::Util qw(sum0); + +use Test::Simple tests => 20; + +{ + for ( 0 .. 19 ) { + my ( $a, $b ) = map { int( rand( 1000 ) ) + 1 } 0 .. 1; + my $expected = $a * $b; + ok( ethopian_mul( $a, $b ) == $expected, "ethopian_mul($a, $b) = $a * $b = $expected" ); + } +} + +sub ethopian_mul ( $a, $b ) { + return sum0( map { $_->[1] } grep { odd( $_->[0] ) } ethopian_mul_chain( $a, $b ) ); +} + +sub ethopian_mul_chain ( $a, $b ) { + return [ $a, $b ] if $a <= 1; + + # using bit shifts to avoid use of multiplication + return ( [ $a, $b ], ethopian_mul_chain( $a >> 1, $b << 1 ) ); +} + +sub odd($x) { + $x != ( ( $x >> 1 ) << 1 ); +} -- cgit From 958a003d916036a4f72aa87fa5a3d5ea2717d952 Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Tue, 8 Dec 2020 14:10:52 +0100 Subject: reimplement odd --- challenge-090/alexander-pankoff/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/alexander-pankoff/perl/ch-2.pl b/challenge-090/alexander-pankoff/perl/ch-2.pl index 51f636cc6f..eb588779f8 100755 --- a/challenge-090/alexander-pankoff/perl/ch-2.pl +++ b/challenge-090/alexander-pankoff/perl/ch-2.pl @@ -30,5 +30,5 @@ sub ethopian_mul_chain ( $a, $b ) { } sub odd($x) { - $x != ( ( $x >> 1 ) << 1 ); + $x & 1; } -- cgit From 92d41f92a59d5ebad74d12edec19e86ad1d53aed Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Tue, 8 Dec 2020 14:11:00 +0100 Subject: add verbose mode --- challenge-090/alexander-pankoff/perl/ch-2.pl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/alexander-pankoff/perl/ch-2.pl b/challenge-090/alexander-pankoff/perl/ch-2.pl index eb588779f8..a66c4feea7 100755 --- a/challenge-090/alexander-pankoff/perl/ch-2.pl +++ b/challenge-090/alexander-pankoff/perl/ch-2.pl @@ -10,6 +10,8 @@ use List::Util qw(sum0); use Test::Simple tests => 20; +use constant VERBOSE => $ENV{VERBOSE} // 0; + { for ( 0 .. 19 ) { my ( $a, $b ) = map { int( rand( 1000 ) ) + 1 } 0 .. 1; @@ -19,7 +21,16 @@ use Test::Simple tests => 20; } sub ethopian_mul ( $a, $b ) { - return sum0( map { $_->[1] } grep { odd( $_->[0] ) } ethopian_mul_chain( $a, $b ) ); + my @chain = ethopian_mul_chain( $a, $b ); + verbose( "halving $a, doubling $b, till $a becomes 1" ); + verbose( $_->[0] . ' & ' . $_->[1] ) for @chain; + + my @filtered = map { $_->[1] } grep { odd( $_->[0] ) } @chain; + verbose( "taking right values where left value is odd" ); + verbose( $_ ) for @filtered; + my $product = sum0( @filtered ); + verbose( "product is $product" ); + return $product; } sub ethopian_mul_chain ( $a, $b ) { -- cgit From e14c1c849dc1988a9c93e08413c86c5105753fcd Mon Sep 17 00:00:00 2001 From: Cris-HD Date: Tue, 8 Dec 2020 15:22:52 +0100 Subject: Added challenge 90 solution --- challenge-090/cristian-heredia/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-090/cristian-heredia/perl/ch-2.pl (limited to 'challenge-090') diff --git a/challenge-090/cristian-heredia/perl/ch-2.pl b/challenge-090/cristian-heredia/perl/ch-2.pl new file mode 100644 index 0000000000..2aeac8b209 --- /dev/null +++ b/challenge-090/cristian-heredia/perl/ch-2.pl @@ -0,0 +1,38 @@ +=begin + + TASK #2 › Ethiopian Multiplication + Submitted by: Mohammad S Anwar + You are given two positive numbers $A and $B. + + Write a script to demonstrate Ethiopian Multiplication using the given numbers. + +=end +=cut + +use strict; +use warnings; + +#variables: +my $A = 14; +my $B = 12; +my $result = 0; +print "The result for $A * $B is: "; +calculation(); + +sub calculation { + checkOdd($A); + while ($A != 1) { + use integer; + $A = $A/2; + $B = $B*2; + checkOdd($A); + } + print "$result\n"; +} + +sub checkOdd { + my $number = shift; + if ($number%2) { + $result = $result + $B; + } +} \ No newline at end of file -- cgit From a1e3221c5747a2960ce1b9045333c309434c431c Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 8 Dec 2020 22:33:57 +0530 Subject: Add challenge-090 task 2 solution --- challenge-090/andinus/README | 94 +++++++++++++++++++++++------------- challenge-090/andinus/README.org | 75 ++++++++++++++++++++++++++++ challenge-090/andinus/blog-2.txt | 1 + challenge-090/andinus/raku/ch-2.raku | 38 +++++++++++++++ 4 files changed, 175 insertions(+), 33 deletions(-) create mode 100644 challenge-090/andinus/README.org create mode 100644 challenge-090/andinus/blog-2.txt create mode 100755 challenge-090/andinus/raku/ch-2.raku (limited to 'challenge-090') diff --git a/challenge-090/andinus/README b/challenge-090/andinus/README index b372bec8d3..b662f1cb50 100644 --- a/challenge-090/andinus/README +++ b/challenge-090/andinus/README @@ -1,67 +1,95 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 086 + CHALLENGE 090 Andinus ━━━━━━━━━━━━━━━ - 2020-11-15 + 2020-12-08 Table of Contents ───────────────── -1. Task 1 - Pair Difference -.. 1. Perl +1. Task 2 - Ethiopian Multiplication +.. 1. Raku -1 Task 1 - Pair Difference -══════════════════════════ +1 Task 2 - Ethiopian Multiplication +═══════════════════════════════════ - You are given an array of integers @N and an integer $A. + You are given two positive numbers $A and $B. - Write a script to find find if there exists a pair of elements in the - array whose difference is $A. + Write a script to demonstrate [Ethiopian Multiplication] using the + given numbers. - Print 1 if exists otherwise 0. +[Ethiopian Multiplication] + -1.1 Perl +1.1 Raku ──────── - • Program: + • Program: - @N & $A are taken from stdin, $A is the last argument. + Start by taking `$A' & `$B' which are defined to be `Int' & positive. ┌──── - │ die "usage: ./ch-1.pl \n" - │ unless scalar @ARGV >= 3; - │ - │ my $A = pop @ARGV; - │ my @N = @ARGV; + │ sub MAIN ( + │ #= positive numbers + │ Int $A is copy where * > 0, + │ Int $B is copy where * > 0 + │ ) { + │ ... + │ } └──── - We just loop over @N over a loop over @N & find the difference. If - it's equal to `$A' or `-$A' then we print `1' & exit. The first loop - is `shift''ing the numbers out of array `@N' because we are matching - for both `$A' & `-$A' so we don't need the number again. + Here's relevant part from the link that was given above: + Start with the two numbers on top. Halve one, ignoring any + remainders or fractions, and double the other, stopping + when you get to 1. - For example, if `@N = [1, 2]' & we don't `shift' in first loop then - we'll perform 2 subtraction operations: `1 - 2' & `2 - 1' & we won't - have to match for `-$A' but if we just match for `-$A' then we can use - `shift' & we'll only have to perform 1 subtraction operation `1 - 2'. + 14 & 12 7 & 24 3 & 48 [See how I ignored the fact that + halving 7 leaves 1 left over?] 1 & 96 <— Stop here. - We assume subtraction costs more than matching with `-$A', that makes - this more efficient. But it doesn't matter much. + Now look at the numbers on the right. Some are across from + an even number: in this case, 12 is across from the + original 14. Ignore those, and add the rest. So we’ll add + 24, 48, and 96, which were across from odd numbers, and + get 168. And that’s the product! Isn’t that cool? + + We do the same thing & also print the instructions. ┌──── - │ while (my $int = shift @N) { - │ foreach (@N) { - │ my $diff = $int - $_; - │ print "1\n" and exit 0 if ($diff == $A or $diff == -$A); + │ my %sets; + │ + │ say "Ethopian Multiplication.\n"; + │ say "Start with $A, $B."; + │ say "Divide $A by 2 & multiple $B by 2 at each step."; + │ say "Continue until $A equals 1. Drop the remainder, both should be Integer.\n"; + │ + │ say "$A, $B"; + │ while True { + │ %sets{$A} = $B.Int; + │ $A = ($A / 2).Int; + │ $B = ($B * 2).Int; + │ last if $A < 1; + │ say "$A, $B"; + │ } + │ + │ say "\nNow to find the product, simply add all the numbers on right side of ','."; + │ say "But skip those numbers which have an even number on the left side.\n"; + │ + │ my Int $product = 0; + │ for %sets.sort({.key.Int}).reverse -> $pair { + │ if $pair.key % 2 != 0 { + │ $product += $pair.value; + │ say "- Adding ", $pair.value, " to product."; + │ } else { + │ say "- Skipping ", $pair.value, " because ", $pair.key, " is even."; │ } │ } - │ print "0\n"; │ + │ say "\nProduct: $product"; └──── diff --git a/challenge-090/andinus/README.org b/challenge-090/andinus/README.org new file mode 100644 index 0000000000..c4f5da3378 --- /dev/null +++ b/challenge-090/andinus/README.org @@ -0,0 +1,75 @@ +#+SETUPFILE: ~/.emacs.d/org-templates/level-2.org +#+HTML_LINK_UP: ../index.html +#+OPTIONS: toc:2 +#+EXPORT_FILE_NAME: index +#+DATE: 2020-12-08 +#+TITLE: Challenge 090 + +* Task 2 - Ethiopian Multiplication +You are given two positive numbers $A and $B. + +Write a script to demonstrate [[https://threesixty360.wordpress.com/2009/06/09/ethiopian-multiplication/][Ethiopian Multiplication]] using the given +numbers. +** Raku +- Program: [[file:perl/ch-2.raku]] + +Start by taking =$A= & =$B= which are defined to be =Int= & positive. +#+BEGIN_SRC raku +sub MAIN ( + #= positive numbers + Int $A is copy where * > 0, + Int $B is copy where * > 0 +) { + ... +} +#+END_SRC + +Here's relevant part from the link that was given above: +#+BEGIN_QUOTE +Start with the two numbers on top. Halve one, ignoring any remainders or +fractions, and double the other, stopping when you get to 1. + +14 & 12 +7 & 24 +3 & 48 [See how I ignored the fact that halving 7 leaves 1 left over?] +1 & 96 <— Stop here. + +Now look at the numbers on the right. Some are across from an even +number: in this case, 12 is across from the original 14. Ignore those, +and add the rest. So we’ll add 24, 48, and 96, which were across from +odd numbers, and get 168. And that’s the product! Isn’t that cool? +#+END_QUOTE + +We do the same thing & also print the instructions. +#+BEGIN_SRC raku +my %sets; + +say "Ethopian Multiplication.\n"; +say "Start with $A, $B."; +say "Divide $A by 2 & multiple $B by 2 at each step."; +say "Continue until $A equals 1. Drop the remainder, both should be Integer.\n"; + +say "$A, $B"; +while True { + %sets{$A} = $B.Int; + $A = ($A / 2).Int; + $B = ($B * 2).Int; + last if $A < 1; + say "$A, $B"; +} + +say "\nNow to find the product, simply add all the numbers on right side of ','."; +say "But skip those numbers which have an even number on the left side.\n"; + +my Int $product = 0; +for %sets.sort({.key.Int}).reverse -> $pair { + if $pair.key % 2 != 0 { + $product += $pair.value; + say "- Adding ", $pair.value, " to product."; + } else { + say "- Skipping ", $pair.value, " because ", $pair.key, " is even."; + } +} + +say "\nProduct: $product"; +#+END_SRC diff --git a/challenge-090/andinus/blog-2.txt b/challenge-090/andinus/blog-2.txt new file mode 100644 index 0000000000..5b667faed0 --- /dev/null +++ b/challenge-090/andinus/blog-2.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-090/ diff --git a/challenge-090/andinus/raku/ch-2.raku b/challenge-090/andinus/raku/ch-2.raku new file mode 100755 index 0000000000..5a30ae90b4 --- /dev/null +++ b/challenge-090/andinus/raku/ch-2.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku + +sub MAIN ( + #= positive numbers + Int $A is copy where * > 0, + Int $B is copy where * > 0 +) { + my %sets; + + say "Ethopian Multiplication.\n"; + say "Start with $A, $B."; + say "Divide $A by 2 & multiple $B by 2 at each step."; + say "Continue until $A equals 1. Drop the remainder, both should be Integer.\n"; + + say "$A, $B"; + while True { + %sets{$A} = $B.Int; + $A = ($A / 2).Int; + $B = ($B * 2).Int; + last if $A < 1; + say "$A, $B"; + } + + say "\nNow to find the product, simply add all the numbers on right side of ','."; + say "But skip those numbers which have an even number on the left side.\n"; + + my Int $product = 0; + for %sets.sort({.key.Int}).reverse -> $pair { + if $pair.key % 2 != 0 { + $product += $pair.value; + say "- Adding ", $pair.value, " to product."; + } else { + say "- Skipping ", $pair.value, " because ", $pair.key, " is even."; + } + } + + say "\nProduct: $product"; +} -- cgit From fa883cb3f3225a37216f0ff9c6eaf1cc5426b827 Mon Sep 17 00:00:00 2001 From: ziameraj16 Date: Tue, 8 Dec 2020 18:08:50 +0000 Subject: Java Solution for Ethiopian Multiplication --- challenge-090/ziameraj16/README.md | 1 + .../ziameraj16/java/EthiopianMultiplication.java | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 challenge-090/ziameraj16/README.md create mode 100755 challenge-090/ziameraj16/java/EthiopianMultiplication.java (limited to 'challenge-090') diff --git a/challenge-090/ziameraj16/README.md b/challenge-090/ziameraj16/README.md new file mode 100644 index 0000000000..e04a1c63a3 --- /dev/null +++ b/challenge-090/ziameraj16/README.md @@ -0,0 +1 @@ +Solution by Mohammad Meraj Zia diff --git a/challenge-090/ziameraj16/java/EthiopianMultiplication.java b/challenge-090/ziameraj16/java/EthiopianMultiplication.java new file mode 100755 index 0000000000..da39b823e5 --- /dev/null +++ b/challenge-090/ziameraj16/java/EthiopianMultiplication.java @@ -0,0 +1,25 @@ + +import java.util.Scanner; + +public class EthiopianMultiplication { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter first number"); + final int first = scanner.nextInt(); + System.out.println("Enter second number"); + final int second = scanner.nextInt(); + System.out.println("Result using Ethiopian Multiplication: " + multiply(first, second)); + } + + public static int multiply(int first, int second) { + int sum = 0; + for (int i = first; i >= 1; i = i/2) { + if (i % 2 != 0) { + sum += second; + } + second = second * 2; + } + return sum; + } +} -- cgit From 085764e526d26c50a09a2c0c18da809268e32f33 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 7 Dec 2020 12:39:22 -0800 Subject: Ch90 (Clojure): Task 1 & Task 2 --- .../clojure/src/tw/weekly/c90/core.clj | 12 ++++++++++ .../clojure/src/tw/weekly/c90/t1.clj | 28 ++++++++++++++++++++++ .../clojure/src/tw/weekly/c90/t2.clj | 22 +++++++++++++++++ .../clojure/test/tw/weekly/c90_test.clj | 16 +++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj create mode 100644 challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj create mode 100644 challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj create mode 100644 challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj (limited to 'challenge-090') diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj new file mode 100644 index 0000000000..296c2c6051 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c90.core + (:require [tw.weekly.c90.t1 :as t1]) + (:require [tw.weekly.c90.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj new file mode 100644 index 0000000000..e739241345 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj @@ -0,0 +1,28 @@ +(ns tw.weekly.c90.t1 + (:require [clojure.string :as str]) + (:require [clojure.pprint :refer [cl-format]]) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › DNA Sequence +;;; + +(def DEFAULT-DNA + "GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG") + +(defn process-dna + "Process the DNA sequence, counting nucleiobases and determining the + complementary sequence." + [dna] + (let [complement-dna (fn [s] (str/escape s {\T \A, \A \T, \C \G, \G \C}))] + ((juxt frequencies complement-dna) dna))) + +(defn -main + "Run Task 1 with a given DNA sequence, defaulting to the example given in + the task description." + [& args] + (let [dna (or (some-> args first edn/read-string) DEFAULT-DNA) + [freqs complement-seq] (process-dna dna)] + (cl-format true "~12a: ~a~%~12a: ~a~%" + "Counts" freqs + "Complement" complement-seq))) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj new file mode 100644 index 0000000000..ab767f91fc --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj @@ -0,0 +1,22 @@ +(ns tw.weekly.c90.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Ethiopian Multiplication +;;; + +(defn ethiopian-multiply + "Compute the product of two positive integers using Ethiopian Multiplication." + [a b] + (let [source (map vector (iterate #(quot % 2) a) (iterate #(* 2 %) b)) + xf (comp + (take-while (fn [[a _]] (pos? a))) + (keep (fn [[a b]] (when (odd? a) b))))] + (transduce xf + source))) + +(defn -main + "Run Task 2 with the given positive integers, defaulting to the example + given in the explanation page linked from the task description." + [& args] + (let [[A B] (or (some->> args (take 2) (map edn/read-string)) [12 14])] + (println (ethiopian-multiply A B)))) diff --git a/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj new file mode 100644 index 0000000000..a09788f137 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj @@ -0,0 +1,16 @@ +(ns tw.weekly.c90-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c90.t1 :refer [DEFAULT-DNA process-dna]] + [tw.weekly.c90.t2 :refer [ethiopian-multiply]])) + +(def COMPLEMENT-DEFAULT-DNA + "CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC") + +(deftest task-1 + (testing "Task 1, DNA Sequence" + (is (= [{\G 13, \T 22, \A 14, \C 18}, COMPLEMENT-DEFAULT-DNA] + (process-dna DEFAULT-DNA))))) + +(deftest task-2 + (testing "Task 2, Ethiopian Multiplication" + (is (= 168 (ethiopian-multiply 12 14))))) -- cgit From ec53034d57fe6d3a652c29ad0f6b6ba33d8ed848 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Tue, 8 Dec 2020 13:07:11 -0800 Subject: Ch90 (Lua): Tasks 1 & 2 --- challenge-090/tyler-wardhaugh/lua/ch-1.lua | 35 ++++++++++++++++++++++++++++++ challenge-090/tyler-wardhaugh/lua/ch-2.lua | 27 +++++++++++++++++++++++ challenge-090/tyler-wardhaugh/lua/run.lua | 9 ++++++++ challenge-090/tyler-wardhaugh/lua/test.lua | 21 ++++++++++++++++++ challenge-090/tyler-wardhaugh/lua/util.lua | 14 ++++++++++++ 5 files changed, 106 insertions(+) create mode 100755 challenge-090/tyler-wardhaugh/lua/ch-1.lua create mode 100755 challenge-090/tyler-wardhaugh/lua/ch-2.lua create mode 100755 challenge-090/tyler-wardhaugh/lua/run.lua create mode 100755 challenge-090/tyler-wardhaugh/lua/test.lua create mode 100644 challenge-090/tyler-wardhaugh/lua/util.lua (limited to 'challenge-090') diff --git a/challenge-090/tyler-wardhaugh/lua/ch-1.lua b/challenge-090/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..e964ce9f6d --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,35 @@ +#!/usr/bin/env lua + +local t1 = {} +local util = require'util' + +t1.DEFAULT_DNA = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG' + +function t1.process_dna(dna) + freq = {} + for c in dna:gmatch'.' do + freq[c] = (freq[c] or 0) + 1 + end + + local replacement = {A='T', T='A', C='G', G='C'} + local complement = dna:gsub('(%w)', function (c) return replacement[c] end) + + return freq, complement +end + +function t1.run(args) + local dna = args[1] or t1.DEFAULT_DNA + local freq, complement = t1.process_dna(dna) + + io.write('Counts = { ') + local seq = util.dict_to_seq(freq) + for i, v in ipairs(seq) do + io.write(v[1] .. ': ' .. v[2]) + if i ~= #seq then io.write(', ') end + end + print(' }\n') + + print('Complement: ' .. complement) +end + +return t1 diff --git a/challenge-090/tyler-wardhaugh/lua/ch-2.lua b/challenge-090/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..85c69b7be8 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,27 @@ +#!/usr/bin/env lua + +local t2 = {} + +function t2.ethiopian_multiply(a, b) + local product = 0 + + while a > 0 do + if a % 2 ~= 0 then + product = product + b + end + + a = a // 2 + b = b * 2 + end + + return product +end + +function t2.run(args) + local A = tonumber(args[1]) or 12 + local B = tonumber(args[2]) or 14 + local product = t2.ethiopian_multiply(A, B) + print(product) +end + +return t2 diff --git a/challenge-090/tyler-wardhaugh/lua/run.lua b/challenge-090/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/run.lua @@ -0,0 +1,9 @@ +#!/usr/bin/env lua + +local filename = arg[1] +local run_args = table.move(arg, 2, #arg, 1, {}) + +io.write(string.format("Running task from '%s' with {%s}:\n", + filename, table.concat(run_args, ", "))) + +require(filename).run(run_args) diff --git a/challenge-090/tyler-wardhaugh/lua/test.lua b/challenge-090/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..7dffde2f53 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,21 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, DNA Sequence", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + local freq, complement = t1.process_dna(t1.DEFAULT_DNA) + assert.are.same({G=13, T=22, A=14, C=18 }, freq) + assert.are.same('CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC', + complement) + end) +end) + +describe("Task 2, Ethiopian Multiplication", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(168, t2.ethiopian_multiply(12, 14)) + assert.are.same(1554, t2.ethiopian_multiply(37, 42)) + end) +end) diff --git a/challenge-090/tyler-wardhaugh/lua/util.lua b/challenge-090/tyler-wardhaugh/lua/util.lua new file mode 100644 index 0000000000..1623b04824 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/util.lua @@ -0,0 +1,14 @@ +#!/usr/bin/env lua + +util = {} + +--[[ table ]]-- +function util.dict_to_seq(coll) + local result = {} + for k, v in pairs(coll) do + table.insert(result, { k, v }) + end + return result +end + +return util -- cgit From b2142f9dbfe0bec3146ea3ef6bd76241438b167e Mon Sep 17 00:00:00 2001 From: Garrett Goebel Date: Tue, 8 Dec 2020 18:43:48 -0500 Subject: challenge-090 Raku --- challenge-090/garrett-goebel/README | 1 + challenge-090/garrett-goebel/raku/ch-1.raku | 2 ++ challenge-090/garrett-goebel/raku/ch-2.raku | 2 ++ 3 files changed, 5 insertions(+) create mode 100644 challenge-090/garrett-goebel/README create mode 100644 challenge-090/garrett-goebel/raku/ch-1.raku create mode 100644 challenge-090/garrett-goebel/raku/ch-2.raku (limited to 'challenge-090') diff --git a/challenge-090/garrett-goebel/README b/challenge-090/garrett-goebel/README new file mode 100644 index 0000000000..a8aa246dbb --- /dev/null +++ b/challenge-090/garrett-goebel/README @@ -0,0 +1 @@ +Solution by Garrett Goebel diff --git a/challenge-090/garrett-goebel/raku/ch-1.raku b/challenge-090/garrett-goebel/raku/ch-1.raku new file mode 100644 index 0000000000..7c034427ea --- /dev/null +++ b/challenge-090/garrett-goebel/raku/ch-1.raku @@ -0,0 +1,2 @@ +#!/usr/bin/env raku +'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'.comb.elems.say; diff --git a/challenge-090/garrett-goebel/raku/ch-2.raku b/challenge-090/garrett-goebel/raku/ch-2.raku new file mode 100644 index 0000000000..b955dd9446 --- /dev/null +++ b/challenge-090/garrett-goebel/raku/ch-2.raku @@ -0,0 +1,2 @@ +#!/usr/bin/env raku +say TR/TAGC/ATCG/ with 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'; -- cgit From e37762846b7c44fdcafb9d58edf3a7b5f2246747 Mon Sep 17 00:00:00 2001 From: Garrett Goebel Date: Tue, 8 Dec 2020 18:53:48 -0500 Subject: challenge-090 Task 1 Raku --- challenge-090/garrett-goebel/raku/ch-1.raku | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/garrett-goebel/raku/ch-1.raku b/challenge-090/garrett-goebel/raku/ch-1.raku index 7c034427ea..00c9d0df57 100644 --- a/challenge-090/garrett-goebel/raku/ch-1.raku +++ b/challenge-090/garrett-goebel/raku/ch-1.raku @@ -1,2 +1,5 @@ #!/usr/bin/env raku -'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'.comb.elems.say; +'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'.&{ + say 'Nucleiobase count: ' ~ .comb.elems; + say 'Complementary sequence: ' ~ TR/TAGC/ATCG/; +} -- cgit From 07d9a1cb90b8268487449bc1a109a001787ce3f1 Mon Sep 17 00:00:00 2001 From: Garrett Goebel Date: Tue, 8 Dec 2020 19:33:20 -0500 Subject: challenge-090 Task 2 Raku --- challenge-090/garrett-goebel/raku/ch-2.raku | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'challenge-090') diff --git a/challenge-090/garrett-goebel/raku/ch-2.raku b/challenge-090/garrett-goebel/raku/ch-2.raku index b955dd9446..35324621de 100644 --- a/challenge-090/garrett-goebel/raku/ch-2.raku +++ b/challenge-090/garrett-goebel/raku/ch-2.raku @@ -1,2 +1,17 @@ #!/usr/bin/env raku -say TR/TAGC/ATCG/ with 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'; + +unit sub MAIN (Int $A where {$A > 0} = 14, Int $B where {$B > 0} = 12); + +my ($a, $b, $r) = ($A, $B, 0); # command line args are immutable + +say "$a & $b"; + +if ($a > 1) { + repeat { + $r += $b if $a mod 2; + say "{ $a div= 2 } & { $b *= 2 } | r: $r"; + } while $a > 1; +} +$r += $b; + +say "r: $r"; -- cgit From 84fd44fc915d5ea833e83d2a91114553c18e9438 Mon Sep 17 00:00:00 2001 From: Garrett Goebel Date: Tue, 8 Dec 2020 19:51:18 -0500 Subject: challenge-090 Task 2 Raku tweaks --- challenge-090/garrett-goebel/raku/ch-2.raku | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'challenge-090') diff --git a/challenge-090/garrett-goebel/raku/ch-2.raku b/challenge-090/garrett-goebel/raku/ch-2.raku index 35324621de..dede70ec31 100644 --- a/challenge-090/garrett-goebel/raku/ch-2.raku +++ b/challenge-090/garrett-goebel/raku/ch-2.raku @@ -1,17 +1,19 @@ #!/usr/bin/env raku -unit sub MAIN (Int $A where {$A > 0} = 14, Int $B where {$B > 0} = 12); +unit sub MAIN ( + Int $A is copy where $A > 0 = 14, + Int $B is copy where $B > 0 = 12 +); -my ($a, $b, $r) = ($A, $B, 0); # command line args are immutable +my Int $r = 0; +my $format = "%10d & %10d | product: %10d\n"; +$format.printf($A,$B, $r); -say "$a & $b"; - -if ($a > 1) { +if ($A > 1) { repeat { - $r += $b if $a mod 2; - say "{ $a div= 2 } & { $b *= 2 } | r: $r"; - } while $a > 1; + $r += $B if $A mod 2; + $format.printf($A div= 2, $B *= 2, $r); + } while $A > 1; } -$r += $b; - -say "r: $r"; +$r += $B; +~(' ' x 24 ~ "| product: %10d\n").printf($r); -- cgit From 7a81a293415612144e83f99a11c04190727814d6 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 9 Dec 2020 04:47:45 +0000 Subject: - Added solutions by Pete Houston. --- challenge-090/pete-houston/perl/ch-1.pl | 27 ++++++++++++ challenge-090/pete-houston/perl/ch-2.pl | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 challenge-090/pete-houston/perl/ch-1.pl create mode 100644 challenge-090/pete-houston/perl/ch-2.pl (limited to 'challenge-090') diff --git a/challenge-090/pete-houston/perl/ch-1.pl b/challenge-090/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..e35a0078df --- /dev/null +++ b/challenge-090/pete-houston/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 9001.pl +# +# USAGE: ./9001.pl STRING +# +# DESCRIPTION: DNA nucleobase counts and complementary sequence +# +# NOTES: STRING must contain uppercase A, C, G and T characters +# BUGS: uses string eval *shudder* +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 07/12/20 +#=============================================================================== + +use strict; +use warnings; + +my $dna = shift; +for my $base (qw/A C G T/) { + printf "Count of $base is %i\n", eval "\$dna =~ tr/$base/$base/;"; +} +print "Original sequence is $dna\n"; +$dna =~ tr/ACGT/TGCA/; +print "Complementary sequence is $dna\n"; diff --git a/challenge-090/pete-houston/perl/ch-2.pl b/challenge-090/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..ba80f52e08 --- /dev/null +++ b/challenge-090/pete-houston/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 9002.pl +# +# USAGE: ./9002.pl [-q] N M +# +# DESCRIPTION: Ethiopian multiplication showing working out. +# +# OPTIONS: -q to hide working out +# REQUIREMENTS: Getopt::Std +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 07/12/20 +#=============================================================================== + +use strict; +use warnings; + +use Getopt::Std; +$Getopt::Std::STANDARD_HELP_VERSION = 1; +*HELP_MESSAGE = \&usage; +our $VERSION = '1.00'; + +# Get option +my %opts; +getopts ('q', \%opts) or usage (); + +# Validate arguments +my ($n, $m) = @ARGV; +for ($n, $m) { + usage () unless defined && /^[0-9]+$/ && $_ >= 1; +} + +# Process the multiplication +verbose ("Starting with $n and $m we iterate by halving and doubling\n"); + +my $tot = 0; +my $target = $n * $m; +my $prodstr = "$n * $m"; + +while (1) { + verbose (sprintf "%5i, %5i\n", $n, $m); + if ($n % 2) { + $tot += $m; + verbose (" $n is odd so add this value of $m " . + "to give a running total of $tot\n"); + } else { + verbose (" $n is even so ignore this value of $m\n"); + + } + if ($n == 1) { + verbose (" n is $n, so we are finished and the result is $tot\n"); + print "$prodstr = $tot\n"; + if ($target != $tot) { + die q#Hey, wait just a minute. That's not right, # . + "it should be $tot.\n" . + "You've managed to break Ethiopian Multiplication!\n"; + } + exit; + } + $n = int ($n / 2); + $m *= 2; +} + +sub usage { + die < Date: Wed, 9 Dec 2020 04:55:53 +0000 Subject: - Added solutions by Laurent Rosenfeld. --- challenge-090/laurent-rosenfeld/blog.txt | 1 + challenge-090/laurent-rosenfeld/perl/ch-1.pl | 12 ++++++++++++ challenge-090/laurent-rosenfeld/perl/ch-2.pl | 12 ++++++++++++ challenge-090/laurent-rosenfeld/raku/ch-1.raku | 13 +++++++++++++ challenge-090/laurent-rosenfeld/raku/ch-2.raku | 10 ++++++++++ 5 files changed, 48 insertions(+) create mode 100644 challenge-090/laurent-rosenfeld/blog.txt create mode 100644 challenge-090/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-090/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-090/laurent-rosenfeld/raku/ch-1.raku create mode 100644 challenge-090/laurent-rosenfeld/raku/ch-2.raku (limited to 'challenge-090') diff --git a/challenge-090/laurent-rosenfeld/blog.txt b/challenge-090/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..fbb7836743 --- /dev/null +++ b/challenge-090/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2020/12/perl-weekly-challenge-90-dna-sequence-and-ethiopian-multiplication.html diff --git a/challenge-090/laurent-rosenfeld/perl/ch-1.pl b/challenge-090/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..bb0ca420cb --- /dev/null +++ b/challenge-090/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; +use feature "say"; + +my $dna = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'; +# count +my %histogram; +$histogram{$_}++ for split '', $dna; +say "$_: $histogram{$_}" for keys %histogram; + +# Complementary sequence +say for "Complement:", $dna =~ tr/TAGC/ATCG/r; diff --git a/challenge-090/laurent-rosenfeld/perl/ch-2.pl b/challenge-090/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..0b8ef08383 --- /dev/null +++ b/challenge-090/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; +use feature "say"; + +my ($c, $d) = @ARGV; +my $result = $c % 2 ? $d : 0; +while ($c > 1) { + $c = $c >> 1; # right shift 1 bit = div by 2 + $d *= 2; + $result += $d if $c % 2; +} +say $result; diff --git a/challenge-090/laurent-rosenfeld/raku/ch-1.raku b/challenge-090/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..16c141c593 --- /dev/null +++ b/challenge-090/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,13 @@ +use v6; + +my $dna = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'; + +# count +my %histo; +%histo{$_}++ for $dna.comb; +say "Histogram:"; +.say for %histo.pairs; + +# Complementary sequence +my %complement = T => 'A', A => 'T', G => 'C', C => 'G'; +.say for "Complement:", $dna.comb.map({%complement{$_}}).join: ''; diff --git a/challenge-090/laurent-rosenfeld/raku/ch-2.raku b/challenge-090/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..8c1d413537 --- /dev/null +++ b/challenge-090/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,10 @@ +use v6; + +my ($a, $b) = map {$_.Int}, @*ARGS; +my $result = $a % 2 ?? $b !! 0; +while $a > 1 { + $a div= 2; + $b *= 2; + $result += $b if $a % 2; +} +say $result; -- cgit