diff options
| author | Jared Martin <jaredor+github@gmail.com> | 2021-07-11 16:49:57 -0500 |
|---|---|---|
| committer | Jared Martin <jaredor+github@gmail.com> | 2021-07-11 16:49:57 -0500 |
| commit | c11f00e32b34c3443d836eba8ca3e9adff115f4e (patch) | |
| tree | 767a598537c9c7566987553c4d30dedf9d429995 /challenge-120 | |
| parent | c267eed789455556db7dd555def186acd3409982 (diff) | |
| download | perlweeklychallenge-club-c11f00e32b34c3443d836eba8ca3e9adff115f4e.tar.gz perlweeklychallenge-club-c11f00e32b34c3443d836eba8ca3e9adff115f4e.tar.bz2 perlweeklychallenge-club-c11f00e32b34c3443d836eba8ca3e9adff115f4e.zip | |
Final draft
Diffstat (limited to 'challenge-120')
| -rwxr-xr-x | challenge-120/jaredor/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-120/jaredor/perl/ch-2.pl | 50 |
2 files changed, 64 insertions, 54 deletions
diff --git a/challenge-120/jaredor/perl/ch-1.pl b/challenge-120/jaredor/perl/ch-1.pl index 9d7027db3e..0ef303c245 100755 --- a/challenge-120/jaredor/perl/ch-1.pl +++ b/challenge-120/jaredor/perl/ch-1.pl @@ -10,8 +10,7 @@ use Pod::Usage; # For this challenge -use bigint; # To allow for arbitrarily long hexstrings -use List::Util qw(all); # To check all the input args +# use Data::Dump qw(pp); # Validate Input @@ -37,32 +36,48 @@ sub run { my @errors; - push @errors, - "This script requires at least one non-negative integer as an argument." - unless @_; + push @errors, "This script requires one positive integer as an argument." + unless @_ == 1; - push @errors, "Not all arguments are non-negative decimal integers." - unless all { /\A (?:0 | [1-9] \d*) \Z/xms } @_; + push @errors, "The argument is not a positive decimal integers" + unless $_[0] =~ /\A \d+ \Z/xms; pod2usage( join "\n", map { "ERROR: " . $_ } @errors ) if @errors; # Get the solution. - output_results( main_algo(@_) ); + output_results( bit_swap(@_) ); } exit; # End of main script. # The main algorithm. -sub main_algo { +sub bit_swap { + use bigint; + + # It came out in testing that the Number '2' had to be copied + # as a bigint object when creating $bmask. It would work the + # first time, but then fail the second, otherwise. + + my $num = $_[0] + 0; + my ( $num_e, $num_o, $bmask, ) = ( $num->copy(), $num->copy(), 2->copy() ); + $bmask->blsft(2)->bior(2) until $bmask->bge($num); + $num_e->band($bmask); + $bmask->brsft(1); + $num_o->band($bmask); + $num_e->brsft(1); + $num_o->blsft(1); + return $num_o->bior($num_e); } # Report to STDOUT from user command line input. sub output_results { + say @_; + } # Built in test for the algorithm function. @@ -72,24 +87,19 @@ sub test { use Test::More; my $input; - $input = [ 101, ]; - is_deeply( nybble_swap( @{$input} ), [ 86, ], "First example: 101 -> 86" ); - - $input = [ 18, ]; - is_deeply( nybble_swap( @{$input} ), [ 33, ], "Second example: 18 -> 33" ); + $input = 101; + is_deeply( bit_swap($input), 154, "First example: 101 -> 154" ); - $input = [ 0 .. 255 ]; - is_deeply( nybble_swap( @{ nybble_swap( @{$input} ) } ), - $input, "Composition is identity." ); + $input = 18; + is_deeply( bit_swap($input), 33, "Second example: 18 -> 33" ); - $input = [ map { 16 * $_ + $_ } 0 .. 15 ]; - is_deeply( nybble_swap( @{$input} ), - $input, "Bytes of twin nybbles are unchanged." ); + $input = 51; + is_deeply( bit_swap($input), 51, + "Identity for numbers with 'twinned bits': 51." ); - my $p = 1279; - $input = [ 2**( $p - 1 ) * ( 2**$p - 1 ) ]; - is_deeply( nybble_swap( @{ nybble_swap( @{$input} ) } ), - $input, "Handles a special 770 digit number." ); + $input = 2**16 - 1; + is_deeply( bit_swap($input), 65535, + "Identity for numbers with 'twinned bits': 65535." ); done_testing(); } @@ -98,13 +108,13 @@ __END__ =head1 NAME -TWC 119, TASK #1 : Swap Nibbles +TWC 120, TASK #1 : Swap Odd/Even bits =head1 SYNOPSIS - ch-1.pl [options] nonnegint [nonnegint ...] + ch-1.pl [options] nonnegint - Description: Nybble-swap the binary representation of non-negative integers. + Description: bit-swap the binary representation of positive integers. Options: --help Brief help @@ -112,7 +122,7 @@ TWC 119, TASK #1 : Swap Nibbles --test Run embedded test Arguments: - A non-empty list of non-negative integers + A non-empty list of positive integers =head1 OPTIONS @@ -164,6 +174,6 @@ The decimal equivalent of 100001 is 33. =head1 INTERPRETATION -The Resolve grey areas in problem statement. +Used bigint to extend the range of input numbers. =cut diff --git a/challenge-120/jaredor/perl/ch-2.pl b/challenge-120/jaredor/perl/ch-2.pl index 4c04d9f85f..ce4a5dcf3f 100755 --- a/challenge-120/jaredor/perl/ch-2.pl +++ b/challenge-120/jaredor/perl/ch-2.pl @@ -42,7 +42,7 @@ sub run { my $time = $_[0]; if ( $time =~ /\A \d \d : \d \d \Z/xms ) { - my ( $h, $m ) = ($time =~ /\A ( 0? \d+ ) : ( 0? \d+ ) \Z/xms); + my ( $h, $m ) = ( $time =~ /\A ( 0? \d+ ) : ( 0? \d+ ) \Z/xms ); push @errors, "Hours are out-of-range: 01 - 12." unless 1 <= $h and $h <= 12; @@ -59,29 +59,29 @@ sub run { # Get the solution. - output_results( minute_hand_hour_hand_angle($time) ); + output_results( clock_hands_angle($time) ); } exit; # End of main script. # The main algorithm. -sub minute_hand_hour_hand_angle { +sub clock_hands_angle { - my ( $hours, $minutes ) = ($_[0] =~ /\A ( 0? \d+ ) : ( 0? \d+ ) \Z/xms); + my ( $hours, $minutes ) = ( $_[0] =~ /\A ( 0? \d+ ) : ( 0? \d+ ) \Z/xms ); # Degrees are measured from 12 o'clock position, clockwise. - $hours = 0 if $hours == 12; # Make 12 o'clock now 0 o'clock. + $hours = 0 if $hours == 12; # Make 12 o'clock now 0 o'clock. - my $mdeg = 6 * $minutes; # Each minute is 6 degrees. + my $mdeg = 6 * $minutes; # Each minute is 6 degrees. # Each hour is 30 degrees plus ... # ... 30 degrees times the ratio of current minutes to an hour of minutes. - my $hdeg = 30 * ($hours + $minutes / 60); + my $hdeg = 30 * ( $hours + $minutes / 60 ); - my $angle = abs($hdeg - $mdeg); + my $angle = abs( $hdeg - $mdeg ); return $angle > 180 ? 360 - $angle : $angle; @@ -100,24 +100,24 @@ sub test { use Test::More; my $input; - $input = [ 101, ]; - is_deeply( nybble_swap( @{$input} ), [ 86, ], "First example: 101 -> 86" ); + $input = '03:10'; + is_deeply( clock_hands_angle($input), + 35, "First example: 03:10 is 35 degrees." ); - $input = [ 18, ]; - is_deeply( nybble_swap( @{$input} ), [ 33, ], "Second example: 18 -> 33" ); + $input = '04:00'; + is_deeply( clock_hands_angle($input), + 120, "Second example: 04:10 is 120 degrees." ); - $input = [ 0 .. 255 ]; - is_deeply( nybble_swap( @{ nybble_swap( @{$input} ) } ), - $input, "Composition is identity." ); + $input = '12:00'; + is_deeply( clock_hands_angle($input), + 0, "Twelve o'clock, the hands are coincident." ); - $input = [ map { 16 * $_ + $_ } 0 .. 15 ]; - is_deeply( nybble_swap( @{$input} ), - $input, "Bytes of twin nybbles are unchanged." ); + $input = '06:00'; + is_deeply( clock_hands_angle($input), 180, "Six o'clock, 180 degrees." ); - my $p = 1279; - $input = [ 2**( $p - 1 ) * ( 2**$p - 1 ) ]; - is_deeply( nybble_swap( @{ nybble_swap( @{$input} ) } ), - $input, "Handles a special 770 digit number." ); + $input = '06:01'; + is_deeply( clock_hands_angle($input), + 174.5, "One minute after six o'clock, 179.5 degrees." ); done_testing(); } @@ -130,7 +130,7 @@ TWC 120, TASK #2 : Clock Angle =head1 SYNOPSIS - ch-1.pl [options] <hh:mm> + ch-1.pl [options] "hh:mm" Description: Return angle between two hands of an analog clock. @@ -162,7 +162,7 @@ Run the embedded test suite for this script. =head1 DESCRIPTION -B<L<The Weekly Challenge, TASK #2 E<gt> Clock Angle|https://theweeklychallenge.org/blog/perl-weekly-challenge-120/#TASK1>> +B<L<The Weekly Challenge, TASK #2 E<gt> Clock Angle|https://theweeklychallenge.org/blog/perl-weekly-challenge-120/#TASK2>> I<Submitted by: Mohammad S Anwar> @@ -190,6 +190,6 @@ Output: 120 degree =head1 INTERPRETATION -The Resolve grey areas in problem statement. +Problem statement and examples are straightforward. =cut |
