aboutsummaryrefslogtreecommitdiff
path: root/challenge-120
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-120')
-rw-r--r--challenge-120/duncan-c-white/README59
-rwxr-xr-xchallenge-120/duncan-c-white/perl/ch-1.pl48
-rwxr-xr-xchallenge-120/duncan-c-white/perl/ch-2.pl62
3 files changed, 139 insertions, 30 deletions
diff --git a/challenge-120/duncan-c-white/README b/challenge-120/duncan-c-white/README
index fde1701c16..55af529741 100644
--- a/challenge-120/duncan-c-white/README
+++ b/challenge-120/duncan-c-white/README
@@ -1,54 +1,53 @@
-Task 1: "Swap Nibbles
+Task 1: "Swap Odd/Even bits
-You are given a positive integer $N.
+You are given a positive integer $N less than or equal to 255.
-Write a script to swap the two nibbles of the binary representation
-of the given number and print the decimal number of the new binary
-representation.
-
-A nibble is a four-bit aggregation, or half an octet.
-
-To keep the task simple, we only allow integer 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: 86
+ Output: 154
- Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
- The swapped nibbles would be (0101)(0110) same as decimal 86.
+ 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 decimal 18 is 10010 or as 2 nibbles (0001)(0010).
- The swapped nibbles would be (0010)(0001) same as decimal 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.
"
-My notes: trivial.
+My notes: trivial again.
+
+Task 2: "Clock Angle
-Task 2: "Sequence without 1-on-1
+You are given time $T in the format hh:mm.
-Write a script to generate sequence starting at 1. Consider the increasing
-sequence of integers which contain only 1's, 2's and 3's, and
-do not have any doublets of 1's like below. Please accept a positive
-integer $N and print the $Nth term in the generated sequence.
+Write a script to find the smaller angle formed by the hands of an analog
+clock at a given time.
- 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ...
+HINT: A analog clock is divided up into 12 sectors. One sector represents
+30 degree (360/12 = 30).
Example
- Input: $N = 5
- Output: 13
+ Input: $T = '03:10'
+ Output: 35 degree
- Input: $N = 10
- Output: 32
+ 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: $N = 60
- Output: 2223
+ Input: $T = '04:00'
+ Output: 120 degree
"
-My notes: hmm, depends on what we mean by "doublet", as in "no doublets of 1".
-I'm choosing to assume that it means "no two sequential 1s". should be
-pretty easy.
+My notes: sounds pretty easy.
diff --git a/challenge-120/duncan-c-white/perl/ch-1.pl b/challenge-120/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..ff9c157aa5
--- /dev/null
+++ b/challenge-120/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+#
+# Task 1: "Swap Odd/Even bits
+#
+# 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.
+# "
+#
+# My notes: trivial again.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+#use Data::Dumper;
+
+my $debug = 0;
+die "Usage: swap-odd-even-bits N\n" unless @ARGV == 1;
+my $n = shift;
+die "swap-odd-even-bits: N ($n) must be 0..255\n" unless $n>=0 && $n<=255;
+
+my @swap = ( 0, 2, 1, 3 ); # 2-bit integer index -> reverse-2-bit value
+
+my $a = $n & 3;
+my $b = ( $n >> 2 ) & 3;
+my $c = ( $n >> 4 ) & 3;
+my $d = ( $n >> 6 ) & 3;
+
+my $rev = $swap[$a] | ($swap[$b]<<2) | ($swap[$c]<<4) | ($swap[$d]<<6);
+say $rev;
diff --git a/challenge-120/duncan-c-white/perl/ch-2.pl b/challenge-120/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..07a55815d4
--- /dev/null
+++ b/challenge-120/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+#
+# Task 2: "Clock Angle
+#
+# 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
+# "
+#
+# My notes: sounds pretty easy.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Function::Parameters;
+use Getopt::Long;
+use Data::Dumper;
+
+my $debug = 0;
+die "Usage: clock-angle HH:MM\n" unless
+ GetOptions( "debug" => \$debug ) && @ARGV==1;
+my $t = shift;
+
+die "clock-angle: bad time $t, should be of format HH:MM\n" unless
+ $t =~ /^(\d\d):(\d\d)$/;
+my( $h, $m ) = ( $1, $2 );
+
+die "clock-angle: bad hour $h\n" unless $h >= 0 && $h <= 23;
+$h %= 12;
+
+die "clock-angle: bad minute $m\n" unless $h >= 0 && $m <= 59;
+
+#say "h=$h, m=$m";
+
+# hour angle correction: adjust for how far through the hour we are
+my $hang = 30 * ($h + $m/60);
+my $mang = 6 * $m;
+
+#say "h=$h, m=$m, hang=$hang, mang=$mang";
+
+my $angle = abs($hang-$mang); # angle between hands
+
+say "h=$h, m=$m, hang=$hang, mang=$mang, angle=$angle";