diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-07 08:47:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-07 08:47:02 +0100 |
| commit | 3620d32add8dfe870243c4f10f8b6d6846e80ef8 (patch) | |
| tree | 21e773cf0101320e8aab6ce941a8ad8734a2027e | |
| parent | abb82fbd7526ae4ebb0937054c0d9c7093de3dd2 (diff) | |
| parent | a912b3094c0d7d4a43c41d24727c7d6512f539ee (diff) | |
| download | perlweeklychallenge-club-3620d32add8dfe870243c4f10f8b6d6846e80ef8.tar.gz perlweeklychallenge-club-3620d32add8dfe870243c4f10f8b6d6846e80ef8.tar.bz2 perlweeklychallenge-club-3620d32add8dfe870243c4f10f8b6d6846e80ef8.zip | |
Merge pull request #4450 from choroba/ech120
Add solutions to 120: Swap Odd/Even Bits & Clock Angle by E. Choroba
| -rwxr-xr-x | challenge-120/e-choroba/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-120/e-choroba/perl/ch-2.pl | 28 |
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-120/e-choroba/perl/ch-1.pl b/challenge-120/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..2176fb2f97 --- /dev/null +++ b/challenge-120/e-choroba/perl/ch-1.pl @@ -0,0 +1,18 @@ +#! /usr/bin/perl +use warnings; +use strict; + +sub swap_odd_even_bits { + my ($n) = @_; + no warnings 'portable'; + oct '0b' . sprintf('%064b', $n) =~ s/(..)/reverse $1/ger +} + +use Test2::V0; +plan(3); + +is swap_odd_even_bits(101), 154, 'Example 1'; +is swap_odd_even_bits(18), 33, 'Example 2'; + +# 255? Bah! +is swap_odd_even_bits(~0), ~0, 'Large value'; diff --git a/challenge-120/e-choroba/perl/ch-2.pl b/challenge-120/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..4458247675 --- /dev/null +++ b/challenge-120/e-choroba/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub clock_angle { + my ($time) = @_; + my ($h, $m) = split /:/, $time; + + my $angle = abs(($m / 60 + $h) * 30 - $m * 6); + $angle = 360 - $angle if $angle > 180; + $angle *= -1 if $angle < 0; + return $angle +} + +use Test2::V0; +plan 10; + +is clock_angle('03:10'), 35, 'Example 1'; +is clock_angle('04:00'), 120, 'Example 2'; + +is clock_angle('12:00'), 0, 'Midnight/Noon'; +is clock_angle( '1:00'), 30, "One o'clock"; +is clock_angle( '2:00'), 60, "Two o'clock"; +is clock_angle('18:00'), 180, 'Six PM'; +is clock_angle( '6:30'), 15, 'Half past 6'; +is clock_angle( '6:33'), 1.5, 'Half past 6'; +is clock_angle( '2:11'), 0.5, 'Smallest angle'; +is clock_angle('11:55'), 27.5, 'Five to twelve'; |
