From 95db0bb6b8cd9762c26d8392cda04b3553b38bb4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 5 Jul 2021 22:57:40 +0100 Subject: Add solutions to challenge 120 --- challenge-120/paulo-custodio/ada/ch_1.adb | 50 ++++++++++++++++++++++ challenge-120/paulo-custodio/ada/ch_2.adb | 64 ++++++++++++++++++++++++++++ challenge-120/paulo-custodio/awk/ch-1.awk | 44 +++++++++++++++++++ challenge-120/paulo-custodio/awk/ch-2.awk | 43 +++++++++++++++++++ challenge-120/paulo-custodio/basic/ch-1.bas | 43 +++++++++++++++++++ challenge-120/paulo-custodio/basic/ch-2.bas | 53 +++++++++++++++++++++++ challenge-120/paulo-custodio/bc/ch-1.bc | 48 +++++++++++++++++++++ challenge-120/paulo-custodio/bc/ch-2.bc | 42 ++++++++++++++++++ challenge-120/paulo-custodio/c/ch-1.c | 46 ++++++++++++++++++++ challenge-120/paulo-custodio/c/ch-2.c | 52 ++++++++++++++++++++++ challenge-120/paulo-custodio/cpp/ch-1.cpp | 46 ++++++++++++++++++++ challenge-120/paulo-custodio/cpp/ch-2.cpp | 57 +++++++++++++++++++++++++ challenge-120/paulo-custodio/d/ch_1.d | 46 ++++++++++++++++++++ challenge-120/paulo-custodio/d/ch_2.d | 55 ++++++++++++++++++++++++ challenge-120/paulo-custodio/forth/ch-1.fs | 38 +++++++++++++++++ challenge-120/paulo-custodio/forth/ch-2.fs | 64 ++++++++++++++++++++++++++++ challenge-120/paulo-custodio/lua/ch-1.lua | 44 +++++++++++++++++++ challenge-120/paulo-custodio/lua/ch-2.lua | 63 +++++++++++++++++++++++++++ challenge-120/paulo-custodio/pascal/ch-1.pas | 50 ++++++++++++++++++++++ challenge-120/paulo-custodio/pascal/ch-2.pas | 56 ++++++++++++++++++++++++ challenge-120/paulo-custodio/perl/ch-1.pl | 44 +++++++++++++++++++ challenge-120/paulo-custodio/perl/ch-2.pl | 43 +++++++++++++++++++ challenge-120/paulo-custodio/python/ch-1.py | 43 +++++++++++++++++++ challenge-120/paulo-custodio/python/ch-2.py | 40 +++++++++++++++++ challenge-120/paulo-custodio/t/test-1.yaml | 30 +++++++++++++ challenge-120/paulo-custodio/t/test-2.yaml | 40 +++++++++++++++++ challenge-120/paulo-custodio/test.pl | 4 ++ 27 files changed, 1248 insertions(+) create mode 100644 challenge-120/paulo-custodio/ada/ch_1.adb create mode 100644 challenge-120/paulo-custodio/ada/ch_2.adb create mode 100644 challenge-120/paulo-custodio/awk/ch-1.awk create mode 100644 challenge-120/paulo-custodio/awk/ch-2.awk create mode 100644 challenge-120/paulo-custodio/basic/ch-1.bas create mode 100644 challenge-120/paulo-custodio/basic/ch-2.bas create mode 100644 challenge-120/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-120/paulo-custodio/bc/ch-2.bc create mode 100644 challenge-120/paulo-custodio/c/ch-1.c create mode 100644 challenge-120/paulo-custodio/c/ch-2.c create mode 100644 challenge-120/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-120/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-120/paulo-custodio/d/ch_1.d create mode 100644 challenge-120/paulo-custodio/d/ch_2.d create mode 100644 challenge-120/paulo-custodio/forth/ch-1.fs create mode 100644 challenge-120/paulo-custodio/forth/ch-2.fs create mode 100644 challenge-120/paulo-custodio/lua/ch-1.lua create mode 100644 challenge-120/paulo-custodio/lua/ch-2.lua create mode 100644 challenge-120/paulo-custodio/pascal/ch-1.pas create mode 100644 challenge-120/paulo-custodio/pascal/ch-2.pas create mode 100644 challenge-120/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-120/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-120/paulo-custodio/python/ch-1.py create mode 100644 challenge-120/paulo-custodio/python/ch-2.py create mode 100644 challenge-120/paulo-custodio/t/test-1.yaml create mode 100644 challenge-120/paulo-custodio/t/test-2.yaml create mode 100644 challenge-120/paulo-custodio/test.pl diff --git a/challenge-120/paulo-custodio/ada/ch_1.adb b/challenge-120/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..8e7cf38977 --- /dev/null +++ b/challenge-120/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,50 @@ +-- Challenge 120 +-- +-- TASK #1 - 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. + +with Ada.Command_Line; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_1 is + package CL renames Ada.Command_Line; + + function swapBits(n1: Integer) return Integer is + n: Integer := n1; + result: Integer := 0; + shift: Integer := 1; + begin + while n > 0 loop + if n mod 2 /= 0 then result := result + 2*shift; end if; + n := n / 2; + if n mod 2 /= 0 then result := result + 1*shift; end if; + n := n / 2; + shift := shift*4; + end loop; + return result; + end swapBits; + + n : Integer := Integer'Value(CL.Argument(1)); +begin + n := swapBits(n); + Put_Line(Integer'Image(n)); +end ch_1; diff --git a/challenge-120/paulo-custodio/ada/ch_2.adb b/challenge-120/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..35ae82ce1d --- /dev/null +++ b/challenge-120/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,64 @@ +-- Challenge 120 +-- +-- TASK #2 › 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 + +with Ada.Command_Line; +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; + +procedure ch_2 is + package CL renames Ada.Command_Line; + + procedure parseTime(hhmm: String; hh, mm: out Integer) is + pos: Integer; + begin + pos := Index(hhmm, ":"); + hh := Integer'Value(hhmm(hhmm'First..pos-1)); + mm := Integer'Value(hhmm(pos+1..hhmm'Last)); + end parseTime; + + procedure clockAngles(hh, mm: Integer; hh_angle, mm_angle: out Integer) is + begin + mm_angle := mm*360/60; + hh_angle := (hh mod 12)*360/12 + mm_angle*(360/12)/360; + end clockAngles; + + function clockAngle(hh, mm: Integer) return Integer is + hh_angle, mm_angle, angle: Integer; + begin + clockAngles(hh, mm, hh_angle, mm_angle); + angle := abs(hh_angle - mm_angle); + if angle > 180 then + angle := 360 - angle; + end if; + return angle; + end clockAngle; + + hhmm : String := CL.Argument(1); + hh, mm, angle: Integer; +begin + parseTime(hhmm, hh, mm); + angle := clockAngle(hh, mm); + Put_Line(Integer'Image(angle)); +end ch_2; diff --git a/challenge-120/paulo-custodio/awk/ch-1.awk b/challenge-120/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..8c3eeb3cb3 --- /dev/null +++ b/challenge-120/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,44 @@ +#!/usr/bin/env gawk + +# Challenge 120 +# +# TASK #1 - 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. + +function swap_bits(n, out, bits) { + out = 0 + bits = 0 + while (n > 0) { + if (and(n, 1) != 0) { out = or(out, lshift(2, bits)) } + if (and(n, 2) != 0) { out = or(out, lshift(1, bits)) } + n = rshift(n, 2) + bits = bits + 2; + } + return out +} + +BEGIN { + n = ARGV[1] + n = swap_bits(n) + print n + exit +} diff --git a/challenge-120/paulo-custodio/awk/ch-2.awk b/challenge-120/paulo-custodio/awk/ch-2.awk new file mode 100644 index 0000000000..26a0840386 --- /dev/null +++ b/challenge-120/paulo-custodio/awk/ch-2.awk @@ -0,0 +1,43 @@ +#!/usr/bin/env gawk + +# Challenge 120 +# +# TASK #2 › 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 + +function abs(v) {return v < 0 ? -v : v} + +function clock_angle(hh, mm, hh_angle, mm_angle, angle) { + mm_angle = mm/60*360 + hh_angle = (hh % 12)/12*360 + mm_angle/360*1/12*360 + angle = abs(hh_angle - mm_angle) + if (angle > 180) { angle = 360 - angle } + return angle +} + +BEGIN { + split(ARGV[1], a, ":") + angle = clock_angle(a[1], a[2]) + print angle + exit +} diff --git a/challenge-120/paulo-custodio/basic/ch-1.bas b/challenge-120/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..615a05bf31 --- /dev/null +++ b/challenge-120/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,43 @@ +' Challenge 120 +' +' TASK #1 - 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. + +function swapBits(n as Integer) as Integer + dim result as Integer, shift as Integer + + shift = 1 + do while n > 0 + if n mod 2 <> 0 then result = result + 2*shift + n = n \ 2 + if n mod 2 <> 0 then result = result + 1*shift + n = n \ 2 + shift = shift*4 + loop + swapBits = result +end function + +dim n as Integer + +n = val(Command(1)) +n = swapBits(n) +print n diff --git a/challenge-120/paulo-custodio/basic/ch-2.bas b/challenge-120/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..a9e158fca9 --- /dev/null +++ b/challenge-120/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,53 @@ +' Challenge 120 +' +' TASK #2 - 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 + +sub parseTime(hhmm as String, byref hh as Integer, byref mm as Integer) + dim p as Integer + + p = InStr(hhmm, ":") + hh = val(left(hhmm, p-1)) + mm = val(mid(hhmm, p+1)) +end sub + +sub clockAngles(hh as Integer, mm as Integer, _ + byref hh_angle as Integer, byref mm_angle as Integer) + mm_angle = mm*360\60 + hh_angle = (hh mod 12)*360\12 + mm_angle*(360\12)\360 +end sub + +function clockAngle(hh as Integer, mm as Integer) as Integer + dim hh_angle as Integer, mm_angle as Integer, angle as Integer + + clockAngles hh, mm, hh_angle, mm_angle + angle = abs(hh_angle - mm_angle) + if angle > 180 then angle = 360 - angle + clockAngle = angle +end function + +dim hh as Integer, mm as Integer, angle as Integer + +parseTime Command(1), hh, mm +angle = clockAngle(hh, mm) +print angle diff --git a/challenge-120/paulo-custodio/bc/ch-1.bc b/challenge-120/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..b9eb2f6f6b --- /dev/null +++ b/challenge-120/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,48 @@ +#!/usr/bin/bc -ql + +/* +Challenge 120 + +TASK #1 - 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. +*/ + +scale = 0 +n = read() + +define swap_bits(n) { + auto out, shift + out = 0 + shift = 1 + while (n > 0) { + if ((n % 2) != 0) { out = out + (2 * shift); } + n = n / 2 + if ((n % 2) != 0) { out = out + (1 * shift); } + n = n / 2 + shift = shift * 4; + } + return out; +} + +out = swap_bits(n) +out +quit diff --git a/challenge-120/paulo-custodio/bc/ch-2.bc b/challenge-120/paulo-custodio/bc/ch-2.bc new file mode 100644 index 0000000000..bd7ae9179e --- /dev/null +++ b/challenge-120/paulo-custodio/bc/ch-2.bc @@ -0,0 +1,42 @@ +#!/usr/bin/bc -ql + +/* +Challenge 120 + +TASK #2 › 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 +*/ + +scale = 0 +hh = read() +mm = read() + +mm_angle = mm*360/60 +hh_angle = (hh % 12)*360/12 + mm_angle*(360/12)/360 + +if (hh_angle > mm_angle) { angle = (hh_angle - mm_angle) +} else { angle = (mm_angle - hh_angle) } +if (angle > 180) { angle = 360 - angle } + +angle +quit diff --git a/challenge-120/paulo-custodio/c/ch-1.c b/challenge-120/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..0bb2545b9c --- /dev/null +++ b/challenge-120/paulo-custodio/c/ch-1.c @@ -0,0 +1,46 @@ +/* +Challenge 120 + +TASK #1 - 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. +*/ + +#include +#include + +int swap_bits(int n) { + int out = 0; + int shift = 0; + while (n > 0) { + if ((n & 1) != 0) { out |= 2 << shift; } + if ((n & 2) != 0) { out |= 1 << shift; } + n >>= 2; + shift += 2; + } + return out; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + printf("%d\n", swap_bits(n)); +} diff --git a/challenge-120/paulo-custodio/c/ch-2.c b/challenge-120/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..b6d92b0cab --- /dev/null +++ b/challenge-120/paulo-custodio/c/ch-2.c @@ -0,0 +1,52 @@ +/* +Challenge 120 + +TASK #2 › 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 +*/ + +#include +#include +#include + +bool parse_time(const char* hhmm, int* hh, int* mm) { + *hh = *mm = 0; + if (sscanf(hhmm, "%d:%d", hh, mm) != 2) return false; + return true; +} + +int clock_angle(int hh, int mm) { + int mm_angle = mm*360/60; + int hh_angle = (hh % 12)*360/12 + mm_angle/12; + int angle = abs(hh_angle - mm_angle); + if (angle > 180) { angle = 360 - angle; } + return angle; +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + int hh, mm; + if (!parse_time(argv[1], &hh, &mm)) return EXIT_FAILURE; + int angle = clock_angle(hh, mm); + printf("%d\n", angle); +} diff --git a/challenge-120/paulo-custodio/cpp/ch-1.cpp b/challenge-120/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..7d25a239ec --- /dev/null +++ b/challenge-120/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,46 @@ +/* +Challenge 120 + +TASK #1 - 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. +*/ + +#include +using namespace std; + +int swap_bits(int n) { + int out = 0; + int shift = 0; + while (n > 0) { + if ((n & 1) != 0) { out |= 2 << shift; } + if ((n & 2) != 0) { out |= 1 << shift; } + n >>= 2; + shift += 2; + } + return out; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + cout << swap_bits(n) << endl; +} diff --git a/challenge-120/paulo-custodio/cpp/ch-2.cpp b/challenge-120/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..c3325269ed --- /dev/null +++ b/challenge-120/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,57 @@ +/* +Challenge 120 + +TASK #2 › 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 +*/ + +#include +#include +#include +using namespace std; + +bool parse_time(const string& hhmm, int* hh, int* mm) { + *hh = *mm = 0; + istringstream iss(hhmm); + char c; + if (!(iss >> *hh)) return false; + if (!(iss >> c) || c != ':') return false; + if (!(iss >> *mm)) return false; + return true; +} + +int clock_angle(int hh, int mm) { + int mm_angle = mm*360/60; + int hh_angle = (hh % 12)*360/12 + mm_angle/12; + int angle = abs(hh_angle - mm_angle); + if (angle > 180) { angle = 360 - angle; } + return angle; +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + int hh, mm; + if (!parse_time(argv[1], &hh, &mm)) return EXIT_FAILURE; + int angle = clock_angle(hh, mm); + cout << angle << endl; +} diff --git a/challenge-120/paulo-custodio/d/ch_1.d b/challenge-120/paulo-custodio/d/ch_1.d new file mode 100644 index 0000000000..6960784c83 --- /dev/null +++ b/challenge-120/paulo-custodio/d/ch_1.d @@ -0,0 +1,46 @@ +/* +Challenge 120 + +TASK #1 - 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. +*/ + +import std.stdio; +import std.conv; + +int swap_bits(int n) { + int result = 0; + int shift = 0; + while (n > 0) { + if ((n & 1) != 0) { result |= 2 << shift; } + if ((n & 2) != 0) { result |= 1 << shift; } + n >>= 2; + shift += 2; + } + return result; +} + +void main(string[] args) { + int n = 0; + if (args.length == 2) n = to!int(args[1]); + writeln(swap_bits(n)); +} diff --git a/challenge-120/paulo-custodio/d/ch_2.d b/challenge-120/paulo-custodio/d/ch_2.d new file mode 100644 index 0000000000..faf4b0f163 --- /dev/null +++ b/challenge-120/paulo-custodio/d/ch_2.d @@ -0,0 +1,55 @@ +/* +Challenge 120 + +TASK #2 - 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 +*/ + +import std.stdio; +import std.conv; +import std.array : split; +import std.math : abs; + +bool parse_time(string hhmm, int* hh, int* mm) { + string[] a = hhmm.split(":"); + if (a.length != 2) return false; + *hh = to!int(a[0]); + *mm = to!int(a[1]); + return true; +} + +int clock_angle(int hh, int mm) { + int mm_angle = mm*360/60; + int hh_angle = (hh % 12)*360/12 + mm_angle/12; + int angle = abs(hh_angle - mm_angle); + if (angle > 180) { angle = 360 - angle; } + return angle; +} + +void main(string[] args) { + if (args.length != 2) return; + int hh, mm; + if (!parse_time(args[1], &hh, &mm)) return; + int angle = clock_angle(hh, mm); + writeln(angle); +} diff --git a/challenge-120/paulo-custodio/forth/ch-1.fs b/challenge-120/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..4bc5f6e65b --- /dev/null +++ b/challenge-120/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,38 @@ +#! /usr/bin/env gforth + +\ Challenge 120 +\ +\ TASK #1 - 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. + +: swap_bits { n -- n } + 0 0 { out shift } + BEGIN n 0> WHILE + n 1 AND IF 2 shift LSHIFT out OR TO out THEN + n 2 AND IF 1 shift LSHIFT out OR TO out THEN + n 2 RSHIFT TO n + shift 2 + TO shift + REPEAT + out ; + +NEXT-ARG S>NUMBER? 0= THROW DROP +swap_bits . CR BYE diff --git a/challenge-120/paulo-custodio/forth/ch-2.fs b/challenge-120/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..078dbfbc19 --- /dev/null +++ b/challenge-120/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,64 @@ +#! /usr/bin/env gforth + +\ Challenge 120 +\ +\ TASK #2 › 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 + +: parse_time ( addr len -- hh mm ) + 0. 2SWAP ( 0 0 addr len ) + >NUMBER ( hh 0 addr len ) \ convert hh + 1 /STRING \ skip ':' + 0 -ROT ( hh 0 0 addr len ) + >NUMBER ( hh mm 0 addr len ) + 2DROP DROP ; + +: clock_angle { hh mm -- angle } + mm 360 60 */ { mm_angle } + hh 12 MOD 360 12 */ mm_angle 12 / + { hh_angle } + hh_angle mm_angle - ABS ( angle ) + DUP 180 > IF 360 SWAP - THEN ; + +NEXT-ARG parse_time clock_angle . CR BYE + +\ #include +\ #include +\ #include +\ +\ +\ int clock_angle(int hh, int mm) { +\ int mm_angle = mm*360/60; +\ int hh_angle = (hh % 12)*360/12 + mm_angle/12; +\ int angle = abs(hh_angle - mm_angle); +\ if (angle > 180) { angle = 360 - angle; } +\ return angle; +\ } +\ +\ int main(int argc, char* argv[]) { +\ if (argc != 2) return EXIT_FAILURE; +\ int hh, mm; +\ if (!parse_time(argv[1], &hh, &mm)) return EXIT_FAILURE; +\ int angle = clock_angle(hh, mm); +\ printf("%d\n", angle); +\ } +\ diff --git a/challenge-120/paulo-custodio/lua/ch-1.lua b/challenge-120/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..a4290510be --- /dev/null +++ b/challenge-120/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,44 @@ +#!/usr/bin/env lua + +--[[ +Challenge 120 + +TASK #1 - 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. +--]] + +function swap_bits(n) + local result = 0 + local bits = 1 + while n > 0 do + if n % 2 ~= 0 then result = result + 2*bits end + n = math.floor(n / 2) + if n % 2 ~= 0 then result = result + 1*bits end + n = math.floor(n / 2) + bits = bits*4 + end + return result +end + +n = tonumber(arg[1]) +n = swap_bits(n) +io.write(n, "\n") diff --git a/challenge-120/paulo-custodio/lua/ch-2.lua b/challenge-120/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..69af0280f6 --- /dev/null +++ b/challenge-120/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,63 @@ +#!/usr/bin/env lua + +--[[ +Challenge 120 + +TASK #2 - 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 +--]] + +function split(inputstr, sep) + if sep == nil then + sep = "%s" + end + local t = {} + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + table.insert(t, str) + end + return t +end + +function parse_time(hhmm) + local a = split(hhmm, ":") + local hh = tonumber(a[1]) + local mm = tonumber(a[2]) + return hh, mm +end + +function clock_angles(hh, mm) + local mm_angle = math.floor(mm*360/60) + local hh_angle = math.floor((hh % 12)*360/12) + math.floor(mm_angle/12) + return hh_angle, mm_angle +end + +function clock_angle(hh, mm) + local hh_angle, mm_angle = clock_angles(hh, mm) + local angle = math.abs(hh_angle - mm_angle) + if angle > 180 then angle = 360 - angle end + return angle +end + +hh, mm = parse_time(arg[1]) +angle = clock_angle(hh, mm) +io.write(angle, "\n") diff --git a/challenge-120/paulo-custodio/pascal/ch-1.pas b/challenge-120/paulo-custodio/pascal/ch-1.pas new file mode 100644 index 0000000000..7e4aa8695b --- /dev/null +++ b/challenge-120/paulo-custodio/pascal/ch-1.pas @@ -0,0 +1,50 @@ +(* +Challenge 120 + +TASK #1 - 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. +*) + +program ch1(input, output); +uses sysutils; +function swapBits(n: Integer): Integer; + var + result, shift: Integer; + begin + result := 0; + shift := 1; + while n > 0 do begin + if (n mod 2) <> 0 then result := result + 2*shift; + n := n div 2; + if (n mod 2) <> 0 then result := result + 1*shift; + n := n div 2; + shift := shift*4; + end; + swapBits := result; + end; +var + n: Integer; +begin + n := StrToInt(paramStr(1)); + n := swapBits(n); + WriteLn(n); +end. diff --git a/challenge-120/paulo-custodio/pascal/ch-2.pas b/challenge-120/paulo-custodio/pascal/ch-2.pas new file mode 100644 index 0000000000..7fb3b419a4 --- /dev/null +++ b/challenge-120/paulo-custodio/pascal/ch-2.pas @@ -0,0 +1,56 @@ +(* +Challenge 120 + +TASK #2 - 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 +*) + +program ch2(input, output); +uses sysutils, strutils; +function parseTime(hhmm: String): Integer; + var + p, hh, mm: Integer; + begin + p := PosEx(':', hhmm); + hh := StrToInt(LeftStr(hhmm, p-1)); + mm := StrToInt(MidStr(hhmm, p+1, length(hhmm))); + parseTime := hh*100+mm; + end; +function clockAngle(hhmm: Integer): Integer; + var + hh, mm, hh_angle, mm_angle, angle: Integer; + begin + hh := hhmm div 100; + mm := hhmm mod 100; + mm_angle := mm*360 div 60; + hh_angle := (hh mod 12)*360 div 12 + mm_angle div 12; + angle := abs(hh_angle - mm_angle); + if angle > 180 then angle := 360 - angle; + clockAngle := angle; + end; +var + angle: Integer; +begin + angle := clockAngle(parseTime(paramStr(1))); + WriteLn(angle); +end. diff --git a/challenge-120/paulo-custodio/perl/ch-1.pl b/challenge-120/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..1f5768a304 --- /dev/null +++ b/challenge-120/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 120 +# +# TASK #1 - 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. + +use Modern::Perl; + +my $n = shift || 0; +say swap_bits($n); + +sub swap_bits { + my($n) = @_; + my $out = 0; + my $shift = 0; + while ($n > 0) { + if (($n & 1) != 0) { $out |= 2 << $shift; } + if (($n & 2) != 0) { $out |= 1 << $shift; } + $n >>= 2; + $shift += 2; + } + return $out; +} + diff --git a/challenge-120/paulo-custodio/perl/ch-2.pl b/challenge-120/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..82426be84d --- /dev/null +++ b/challenge-120/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 120 +# +# TASK #2 › 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 + +use Modern::Perl; + +my $hhmm = shift || "0:0"; +my($hh, $mm) = split(/:/, $hhmm, 2); +my($hh_angle, $mm_angle) = clock_angles($hh, $mm); +my $angle = abs($hh_angle - $mm_angle); +if ($angle > 180) { $angle = 360 - $angle; } + +say $angle; + +sub clock_angles { + my($hh, $mm) = @_; + my $mm_angle = $mm/60*360; + my $hh_angle = ($hh % 12)/12*360 + $mm_angle/360*1/12*360; + return ($hh_angle, $mm_angle); +} diff --git a/challenge-120/paulo-custodio/python/ch-1.py b/challenge-120/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..74603af153 --- /dev/null +++ b/challenge-120/paulo-custodio/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Challenge 120 +# +# TASK #1 - 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. + +import sys + +def swap_bits(n): + out = 0 + shift = 0 + while n > 0: + if (n & 1) != 0: + out |= 2 << shift + if (n & 2) != 0: + out |= 1 << shift + n >>= 2 + shift += 2 + return out + +n = int(sys.argv[1]) +n = swap_bits(n) +print(n) diff --git a/challenge-120/paulo-custodio/python/ch-2.py b/challenge-120/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9cf0c89da7 --- /dev/null +++ b/challenge-120/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +# Challenge 120 +# +# TASK #2 - 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 + +import sys + +def clock_angles(hh, mm): + mm_angle = mm * 360 // 60 + hh_angle = (hh % 12) * 360 // 12 + mm_angle // 12 + return hh_angle, mm_angle + +hh, mm = [int(x) for x in sys.argv[1].split(':')] +hh_angle, mm_angle = clock_angles(hh, mm) +angle = abs(hh_angle - mm_angle) +if angle > 180: + angle = 360 - angle +print(angle) diff --git a/challenge-120/paulo-custodio/t/test-1.yaml b/challenge-120/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..b29ac2f365 --- /dev/null +++ b/challenge-120/paulo-custodio/t/test-1.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 101 + input: + output: 154 +- setup: + cleanup: + args: 154 + input: + output: 101 +- setup: + cleanup: + args: 18 + input: + output: 33 +- setup: + cleanup: + args: 33 + input: + output: 18 +- setup: + cleanup: + args: 85 + input: + output: 170 +- setup: + cleanup: + args: 170 + input: + output: 85 diff --git a/challenge-120/paulo-custodio/t/test-2.yaml b/challenge-120/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..4a97a221ad --- /dev/null +++ b/challenge-120/paulo-custodio/t/test-2.yaml @@ -0,0 +1,40 @@ +- setup: + cleanup: + args: "03:10" + input: + output: 35 +- setup: + cleanup: + args: "03:20" + input: + output: 20 +- setup: + cleanup: + args: "03:40" + input: + output: 130 +- setup: + cleanup: + args: "03:50" + input: + output: 175 +- setup: + cleanup: + args: "04:00" + input: + output: 120 +- setup: + cleanup: + args: "00:00" + input: + output: 0 +- setup: + cleanup: + args: "12:00" + input: + output: 0 +- setup: + cleanup: + args: "24:00" + input: + output: 0 diff --git a/challenge-120/paulo-custodio/test.pl b/challenge-120/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-120/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; -- cgit