aboutsummaryrefslogtreecommitdiff
path: root/challenge-120
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-120')
-rw-r--r--challenge-120/cheok-yin-fung/perl/ch-1.pl52
-rw-r--r--challenge-120/cheok-yin-fung/perl/ch-2.pl34
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-120/cheok-yin-fung/perl/ch-1.pl b/challenge-120/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..6888bbce4e
--- /dev/null
+++ b/challenge-120/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+=pod
+https://en.wikipedia.org/wiki/Bitwise_operation
+ #Truth_table_for_all_binary_logical_operators
+
+11 -> 11 3->3 | 1 1
+10 -> 01 2->1 | 0 1
+01 -> 10 1->2 | 1 0
+00 -> 00 0->0 | 0 0
+pq q p
+=cut
+
+# The Weekly Challenge - 120
+# Task 1 Swap Odd/Even Bits
+# Usage: $ ch-1.pl N
+use strict;
+use warnings;
+use Test::More tests => 4;
+
+my $N = $ARGV[0];
+
+sub qp {
+ my $y = $_[0];
+ my $a = int $y/2;
+ my $b = $y % 2;
+ return $b*2+$a;
+}
+
+sub soeb {
+ my $x = $_[0];
+ my @ans_arr;
+ while ($x != 0) {
+ unshift @ans_arr, qp($x % 4);
+ $x = int $x / 4;
+ }
+ my $ans = 0;
+ for (@ans_arr) {
+ $ans <<= 2;
+ $ans += $_;
+ }
+ return $ans;
+}
+
+
+print soeb($N), "\n";
+
+
+ok (soeb(1) == 2, "N=1, output=2");
+ok (soeb(101) == 154, "Example 1");
+ok (soeb(18) == 33, "Example 2");
+ok (soeb(128) == 64, "N=128, output=64");
diff --git a/challenge-120/cheok-yin-fung/perl/ch-2.pl b/challenge-120/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..7b607cc8c2
--- /dev/null
+++ b/challenge-120/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# The Weekly Challenge - 120
+# Task 2 Clock Angle
+# Usage: $ ch-2.pl "hh:mm"
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+my $T = $ARGV[0] || 0;
+
+unless ($T =~ m/^(\d\d):(\d\d)$/ && $1 <= 24 && $2 <= 59) {
+ die "Please input time in the format \"hh:mm\". \n";
+}
+
+#unit: degree(s) per minute
+my $hour_hand_rate = 0.5;
+my $minute_hand_rate = 6;
+
+print clock_angle($T), " degree", "\n";
+
+sub clock_angle {
+ my $time = $_[0];
+ my $h = substr($time,0,2);
+ my $m = substr($time,-2,2);
+ my $deg = abs( ($h*30+$hour_hand_rate*$m - $minute_hand_rate*$m)) % 360;
+ return $deg > 180 ? 360-$deg : $deg;
+}
+
+
+ok ( clock_angle("03:10") == 35, "Example 1");
+ok ( clock_angle("04:00") == 120 , "Example 2");
+ok ( clock_angle("12:00") == 0 , "noon");
+ok ( clock_angle("06:00") == 180, "6 o'clock");
+ok ( clock_angle("09:00") == 90, "9 o'clock");