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 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(-) 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(-) 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(+) 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 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