diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-11 17:49:09 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-11 17:49:09 +0100 |
| commit | c5b6045dd780f65174f9eecae20162b6fc68e72a (patch) | |
| tree | 86aeff593567857358c13c6b54bef00ecd7c6d1c /challenge-120 | |
| parent | 3e62fa72548921721998a8b05220a83753f2130f (diff) | |
| download | perlweeklychallenge-club-c5b6045dd780f65174f9eecae20162b6fc68e72a.tar.gz perlweeklychallenge-club-c5b6045dd780f65174f9eecae20162b6fc68e72a.tar.bz2 perlweeklychallenge-club-c5b6045dd780f65174f9eecae20162b6fc68e72a.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-120')
| -rw-r--r-- | challenge-120/colin-crain/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-120/colin-crain/perl/ch-2.pl | 113 | ||||
| -rw-r--r-- | challenge-120/colin-crain/raku/ch-1.raku | 44 | ||||
| -rw-r--r-- | challenge-120/colin-crain/raku/ch-2.raku | 29 |
4 files changed, 254 insertions, 0 deletions
diff --git a/challenge-120/colin-crain/perl/ch-1.pl b/challenge-120/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..a8abd8c68d --- /dev/null +++ b/challenge-120/colin-crain/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# get-oddly-even.pl
+#
+# Swap Odd/Even bits
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+#
+# method
+# we know the number is less than 256 and greater than 0, so it will
+# fit cleanly into an 8-bit binary representation. The first order
+# of action is to convert it into this set number of 0 and 1 digits,
+# signifying bits, using the "%08b" format specifier. Now the fun
+# begins.
+#
+# We know the length of the string to be 8 characters exactly. By
+# applying a global regular expression match against pairings of 1s
+# and 0s we can divide our 8 characters into 4 groups of 2, returned
+# as a list. In this list we use `reverse` in a scalar context to
+# map each string to a reversed version of itself, swapping the
+# characters. These mutated string bits are then rejoined with a
+# "0b" prefix to indicate to `oct` that we wish to parse a string as
+# though it were a binary number. This returns our altered bit
+# string to the decimal number requested for output.
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+my $num = shift // 18;
+
+my $bin = sprintf "%08b", $num;
+my $res = oct(join '', '0b', map { scalar reverse "$_" } $bin =~ /([01]{2})/g );
+
+say<<"END";
+input: $num
+output: $res
+
+END
diff --git a/challenge-120/colin-crain/perl/ch-2.pl b/challenge-120/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..1e9e247bdf --- /dev/null +++ b/challenge-120/colin-crain/perl/ch-2.pl @@ -0,0 +1,113 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# what-angle-we-at.pl
+#
+# Clock Angle
+# Submitted by: Mohammad S Anwar
+# 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
+
+# method:
+# Rolex wristwatches are known for their "sweep" second hand
+# movement; that is to say the hand does not move incrementally,
+# once a second, from one pip to the next, but rather is seen to
+# move in a continuous flow around the dial. This action is
+# remarkably difficult to achieve in a mechanical movement where the
+# fundamental timekeeping period is based on an oscillating
+# vibration, such as a pendulum. In fact it is ultimately
+# illusionary, based on a rapidly moving pendulum buffered by
+# springs to appear more fluid.
+#
+# With the introduction of inexpensive, accurate electric motors
+# much of the panache of the sweep hand has gone away, and the Rolex
+# has had to rely more on its rugged good looks and maybe the notion that
+# it's body is machined from a giant block of gold. But I digress.
+# In a pendulum-driven clock movement the action is commonly
+# designed to move with a two-second period: one across, and one
+# back. This can be precisely tuned by adjusting the length of the
+# pendulum arm, and forms the basis of all the timekeeping: each
+# half-swing is one tick, 60 of these comprise a minute, and three
+# thouand six hundred to an hour.
+#
+# It's not hard to make a clock with a sweep minute hand, where the
+# hand makes one revolution per hour in as many increments as the
+# pendulum allows. But this is not always advantagous: in a clock
+# without a second hand, such as one would find in a clock-tower,
+# having the minute hand fall between marks leads to ambiguity in
+# reading. One person may see 43 minutes past the hour, another 42.
+# It is often more desirable to have an intenal mechanism rachet
+# along counting pendulum swings on a gear, and only once a
+# revolution have a pin engage a mechanism and move the minute hand
+# one place. In this way the clock hand stays at 42 until the moment
+# arrives, when in moves all at onece to 43 and remains for another
+# minute until it moves again.
+#
+# On the other, other hand, no mechanical clock does this for the hour
+# hand. We go about our day and normally deal with time in a finer
+# granularity than the hour, and hence knowing that it's somewhere
+# between 1 and 2 is useful information in itself to know.
+#
+# So ignoring the second hand, in just the hours and minutes of many
+# common clocks we have
+# two modes of moving the hand: continuous and discrete. With
+# respect to an angle swept from midnight or noon, the minute hand
+# will have moved six degrees every minute, always. As without
+# further information we have no
+# idea how many seconds have elapsed since the last minute tick
+# we cannot improve on this
+# calculation even if we wanted to.
+#
+# For the hours, back to one of our previous other hands,
+# the hand moves a set amount for
+# each hour, but also takes a submovement between the pips based on
+# dividing the interval between two hour points into 60 minutes. The
+# final sweep amount for the hand at any given time will be the sum
+# of the two partial motions.
+
+
+
+
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my $timestr = shift // qw ( 9:49);
+
+my ($h, $m) = $timestr =~ /(\d?\d):(\d\d)/;
+$h %= 12;
+my $mdeg = $m * 6;
+my $hdeg = $h * 30 + $m * 0.5;
+my $ang = abs( $hdeg - $mdeg );
+$ang = 360 - $ang if $ang > 180;
+
+say "time: $timestr";
+say "$ang degrees";
+
diff --git a/challenge-120/colin-crain/raku/ch-1.raku b/challenge-120/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..db69ccfcee --- /dev/null +++ b/challenge-120/colin-crain/raku/ch-1.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env perl6 +# +# +# get-oddly-even.raku +# +# Swap Odd/Even bits +# Submitted by: Mohammad S Anwar +# 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. + +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN ( Int $num where 0 < $num < 256 = 18 ) ; + +$num.fmt("%08b") + .comb(2) + .map(*.flip) + .join + .parse-base(2) + .say; diff --git a/challenge-120/colin-crain/raku/ch-2.raku b/challenge-120/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..d74f9a25c1 --- /dev/null +++ b/challenge-120/colin-crain/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env perl6 +# +# +# .raku +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN (Str $time = "12:55" ); +say $time; + +unless $time ~~ /^ (\d?\d) \: (\d\d) $/ { + die "usage:\n time input in the form hh:mm" +} + +my $h-ang = ( $0 % 12 ) * 30 + $1 * 0.5; +my $m-ang = $1 * 6; +my $ang = ( $h-ang - $m-ang ).abs; +$ang = 360 - $ang if $ang > 180; +say $ang; + + + + + |
