diff options
| author | Alexander Pankoff <ccntrq@screenri.de> | 2020-12-08 13:39:52 +0100 |
|---|---|---|
| committer | Alexander Pankoff <ccntrq@screenri.de> | 2020-12-08 13:39:52 +0100 |
| commit | b687fe1babaa737f2d4d409338262c2f9ad1618c (patch) | |
| tree | 0ebf2cea250fd1538a0fc441e77fbd927a65bea8 | |
| parent | 10b020ec45585fac7ef5bca14e0f365ff91bd76d (diff) | |
| download | perlweeklychallenge-club-b687fe1babaa737f2d4d409338262c2f9ad1618c.tar.gz perlweeklychallenge-club-b687fe1babaa737f2d4d409338262c2f9ad1618c.tar.bz2 perlweeklychallenge-club-b687fe1babaa737f2d4d409338262c2f9ad1618c.zip | |
add solutions for challenge-090 in perl
| -rwxr-xr-x | challenge-090/alexander-pankoff/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-090/alexander-pankoff/perl/ch-2.pl | 34 |
2 files changed, 51 insertions, 0 deletions
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 ); +} |
