diff options
| -rw-r--r-- | challenge-120/wanderdoc/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-120/wanderdoc/perl/ch-2.pl | 47 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-120/wanderdoc/perl/ch-1.pl b/challenge-120/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..306c6013ba --- /dev/null +++ b/challenge-120/wanderdoc/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a positive integer $N less than or equal to 255. Write a script to swap the odd positioned bit with even positioned bit and print the decimal equivalent of the new binary representation. +Example + +Input: $N = 101 Output: 154 +Binary representation of the given number is 01 10 01 01. +The new binary representation after the odd/even swap is 10 01 10 10. +The decimal equivalent of 10011010 is 154. + +Input: $N = 18 Output: 33 +Binary representation of the given number is 00 01 00 10. +The new binary representation after the odd/even swap is 00 10 00 01. +The decimal equivalent of 100001 is 33. +=cut + + +use Scalar::Util qw(looks_like_number); + +sub swap_bits +{ + my $num = $_[0]; + + die "Number in range 1 to 255?$/" + unless looks_like_number($num) and + $num == int($num) and + ($num > 0 and $num < 256); + my $binary = dec_to_bin($num); + $binary = length($binary) % 2 ? 0 . $binary : $binary; + my @bits = split(//, $binary); + @bits[((grep {$_ % 2 == 0} 0 .. $#bits), (grep {$_ % 2 == 1} 0 .. $#bits))] = + @bits[((grep {$_ % 2 == 1} 0 .. $#bits), (grep {$_ % 2 == 0} 0 .. $#bits))]; + $binary = join('', @bits); + $num = bin_to_dec($binary); + return $num; + + +} + + + +for my $n ( 1 .. 255 ) +{ + print join(' -> ', $n, swap_bits($n)), $/; +} + +sub dec_to_bin { return sprintf("%b",$_[0]); } +sub bin_to_dec { return oct("0b" . $_[0]); }
\ No newline at end of file diff --git a/challenge-120/wanderdoc/perl/ch-2.pl b/challenge-120/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..028e461619 --- /dev/null +++ b/challenge-120/wanderdoc/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given time $T in the format hh:mm. Write a script to find the smaller angle formed by the hands of an analog clock at a given time. HINT: A analog clock is divided up into 12 sectors. One sector represents 30 degree (360/12 = 30). +Example +Input: $T = '03:10' Output: 35 degree +The distance between the 2 and the 3 on the clock is 30 degree. For the 10 minutes i.e. 1/6 of an hour that have passed. +The hour hand has also moved 1/6 of the distance between the 3 and the 4, which adds 5 degree (1/6 of 30). +The total measure of the angle is 35 degree. + +Input: $T = '04:00' Output: 120 degree +=cut + + + + + +my %MINUTES; +@MINUTES{0 .. 59} = map $_ * 6, 0 .. 59; + +my %HOURS; +@HOURS{0 .. 11} = map $_ * 30, 0 .. 11; + + +sub clock_angle +{ + my $time = $_[0]; + my ($hour, $min) = map $_ * 1, split(/:/, $time); + $hour = $hour >= 12 ? $hour - 12 : $hour; + my $hour_hand = $HOURS{$hour} + ($min/60) * 30; + my $minute_hand = $MINUTES{$min}; + my $angle = abs($hour_hand - $minute_hand); + $angle = $angle > 180 ? 360 - $angle : $angle; + return $angle; +} + + + +for my $time ( '0000' .. '2359' ) +{ + next if unpack("xxA2", $time) > 59; + + my $time_formatted = join(':',unpack("A2A2",$time)); + print join(' -> ', $time_formatted, clock_angle($time_formatted)), $/; +}
\ No newline at end of file |
