From 58f1f03fc111f73ae044b0a24859bf6cc7a12043 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Tue, 6 Jul 2021 13:50:41 +0800 Subject: beware of 5.5 deg --- challenge-120/cheok-yin-fung/bash/ch-1.sh | 36 +++++++++++++++++++++++ challenge-120/cheok-yin-fung/bash/ch-2.sh | 47 +++++++++++++++++++++++++++++++ challenge-120/cheok-yin-fung/perl/ch-2.pl | 24 ++++++++++++---- 3 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 challenge-120/cheok-yin-fung/bash/ch-1.sh create mode 100644 challenge-120/cheok-yin-fung/bash/ch-2.sh (limited to 'challenge-120') diff --git a/challenge-120/cheok-yin-fung/bash/ch-1.sh b/challenge-120/cheok-yin-fung/bash/ch-1.sh new file mode 100644 index 0000000000..d29d92261f --- /dev/null +++ b/challenge-120/cheok-yin-fung/bash/ch-1.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# The Weekly Challenge - 120 +# Task 1 Swap Odd/Even Bits +# Usage: $ chmod +x ch-1.sh +# $ ./ch-1.sh N + +N=$1 + +if [ $N -gt 255 ] || [ $N -lt 1 ] ; +then + echo "Integer within 1 to 255." + exit ; +fi + +x=$N + +qp () { + A=$(($1/2)); + B=$(($1%2)); + echo $(($B*2+$A)) +} + +while [ $x -ne 0 ] +do + arr+=($(qp $x%4)) + x=$(($x/4)) +done + +ANS=0 + +for ((i=$((${#arr[@]}-1)); i>=0; i=$i-1)) +do + ANS=$(($ANS*4+$[arr[$i]])) +done + +echo $ANS diff --git a/challenge-120/cheok-yin-fung/bash/ch-2.sh b/challenge-120/cheok-yin-fung/bash/ch-2.sh new file mode 100644 index 0000000000..26207114da --- /dev/null +++ b/challenge-120/cheok-yin-fung/bash/ch-2.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# The Weekly Challenge - 120 +# Task 2 Clock Angle +# Usage: $ chmod +x ch-2.sh +# $ ./ch-2.sh hh mm + +# current time: date +"%H:%M" + +hr_hand_inc_per_deg=2 #unit: minute per degree + +minute_hand_rate=6 #unit: degree per minute + +h=$1 +m=$2 + +deg_h=$((($h*30+$m/$hr_hand_inc_per_deg)%360)) +deg_m=$((($minute_hand_rate*$m)%360 )) +half=0 +if [ $(($m%$hr_hand_inc_per_deg)) -eq 1 ]; +then + half=5 + if [ $deg_h -gt $deg_m ]; + then + deg_h=$(($deg_h)) + else + deg_h=$(($deg_h+1)) + fi +fi + +if [ $deg_h -ge $deg_m ]; +then + deg=$(($deg_h-$deg_m)) +else + deg=$(($deg_m-$deg_h)) +fi + +if [ $deg -gt 180 ]; +then + if [ $half -eq 5 ]; + then + deg=$((361-$deg)) + else + deg=$((360-$deg)) + fi +fi + +echo $deg.$half deg diff --git a/challenge-120/cheok-yin-fung/perl/ch-2.pl b/challenge-120/cheok-yin-fung/perl/ch-2.pl index 7b607cc8c2..b626643592 100644 --- a/challenge-120/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-120/cheok-yin-fung/perl/ch-2.pl @@ -4,25 +4,33 @@ # Usage: $ ch-2.pl "hh:mm" use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 9; -my $T = $ARGV[0] || 0; +my @now = localtime(time); -unless ($T =~ m/^(\d\d):(\d\d)$/ && $1 <= 24 && $2 <= 59) { +my $T = $ARGV[0] || "$now[2]:$now[1]"; + +unless ($T =~ m/^(\d){1,2}:(\d){1,2}$/ && $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; +my $minute_hand_rate = 6.0; -print clock_angle($T), " degree", "\n"; +print "The clock angle of NOW ($T): " if !defined($ARGV[0]); +print clock_angle($T), " deg"; +print " ± 5.5 deg" if !defined($ARGV[0]); +print "\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; + my $deg = abs(($h*30+$hour_hand_rate*$m - $minute_hand_rate*$m)); + while ($deg >= 360) { + $deg-=360; + } return $deg > 180 ? 360-$deg : $deg; } @@ -32,3 +40,7 @@ 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"); +ok ( clock_angle("11:59") == 5.5, "11:59"); +ok ( clock_angle("12:01") == 5.5, "12:01"); +ok ( clock_angle("09:27") == 121.5, "09:27"); +ok ( clock_angle("16:11") == 59.5, "16:11"); -- cgit