diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-05 22:57:40 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-05 22:57:40 +0100 |
| commit | 95db0bb6b8cd9762c26d8392cda04b3553b38bb4 (patch) | |
| tree | bc5af7a96d8d474e7259d1f235ee64fb9516dbb3 | |
| parent | 39253b822e4bfe790553c560fd7c377f92f6246c (diff) | |
| download | perlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.tar.gz perlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.tar.bz2 perlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.zip | |
Add solutions to challenge 120
27 files changed, 1248 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> + +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 <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +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 <iostream> +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 <iostream> +#include <sstream> +#include <string> +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 <stdio.h> +\ #include <stdlib.h> +\ #include <stdbool.h> +\ +\ +\ 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< |
