diff options
39 files changed, 2781 insertions, 26 deletions
diff --git a/challenge-120/abigail/README.md b/challenge-120/abigail/README.md index 2fc20d5e68..84e672283a 100644 --- a/challenge-120/abigail/README.md +++ b/challenge-120/abigail/README.md @@ -1,40 +1,34 @@ # Solutions by Abigail -## [Swap Nibbles](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK1) +## [Swap Odd/Even Bits](https://perlweeklychallenge.org/blog/perl-weekly-challenge-120/#TASK1) -> You are given a positive integer `$N`. -> -> 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`. +> 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. ### Examples ~~~~ 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`. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) * [Go](go/ch-1.go) * [Java](java/ch-1.java) @@ -49,26 +43,49 @@ decimal `33`. * [Tcl](tcl/ch-1.tcl) ### Blog -[Swap Nibbles](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-1.html) +[Swap Odd/Even Bits](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-1.html) -## [Sequence without 1-on-1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) +## [Clock Angle](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) -> 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 `$N`th term in the generated sequence. +> You are given time `$T` in the format `hh:mm`. > -> > 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... +> Write a script to find the smaller angle formed by the hands of an +> analog clock at a given time.<br> +> **HINT: A analog clock is divided up into `12` sectors. One sector +> represents `30` degree (`360/12 = 30`).** + +### Examples +~~~~ +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 +~~~~ ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) +* [Befunge-93](befunge-93/ch-2.bf93) * [C](c/ch-2.c) +* [Go](go/ch-2.go) +* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) +* [Tcl](tcl/ch-2.tcl) ### Blog -[Sequence without 1-on-1](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-2.html) +[Clock Angle](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-2.html) diff --git a/challenge-120/abigail/awk/ch-1.awk b/challenge-120/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..2f7e4310d2 --- /dev/null +++ b/challenge-120/abigail/awk/ch-1.awk @@ -0,0 +1,21 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + out = 0 + num = $1 + for (i = 0; i < 8; i ++) { + bit = int ((num - (num % 2 ^ i)) / 2 ^ i) % 2; + if (bit) { + out += 2 ^ (i + (i % 2 ? -1 : 1)) + } + } + print out +} diff --git a/challenge-120/abigail/awk/ch-2.awk b/challenge-120/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..f7a32c7ee0 --- /dev/null +++ b/challenge-120/abigail/awk/ch-2.awk @@ -0,0 +1,25 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +BEGIN { + FS = ":" + DIFF_PER_MINUTE = 11 + MIN_PER_HOUR = 60 + FULL_CIRCLE = 720 +} + +{ + angle = (DIFF_PER_MINUTE * ($1 * MIN_PER_HOUR + $2)) % FULL_CIRCLE + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle + } + + print (angle / 2) +} diff --git a/challenge-120/abigail/bash/ch-1.sh b/challenge-120/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..8ecc50d5dc --- /dev/null +++ b/challenge-120/abigail/bash/ch-1.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read num +do echo $(( (num & 0x55) << 1 + | (num & 0xAA) >> 1 )) +done diff --git a/challenge-120/abigail/bash/ch-2.sh b/challenge-120/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..62540af529 --- /dev/null +++ b/challenge-120/abigail/bash/ch-2.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +IFS=":" + +DIFF_PER_MINUTE=11 # Half degrees +MIN_PER_HOUR=60 +FULL_CIRCLE=720 # Half degrees + +while read hours minutes +do # + # Bash is going to interpret an hour (or minute) of the form "08" + # or "09" as an illegal octal number. So, we're going to use a + # trick: we prepend a 1, and subtract 100. + # + ((hours = "1$hours" - 100)) + ((minutes = "1$minutes" - 100)) + ((angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE)) + if ((2 * angle >= FULL_CIRCLE)) + then ((angle = FULL_CIRCLE - angle)) + fi + + printf "%d" $((angle / 2)) + if ((angle % 2 == 1)) + then printf ".5" + fi + echo +done diff --git a/challenge-120/abigail/bc/ch-1.bc b/challenge-120/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..8a1f52c022 --- /dev/null +++ b/challenge-120/abigail/bc/ch-1.bc @@ -0,0 +1,27 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + +while (1) { + num = read () + if (num == 0) { + break + } + out = 0 + for (i = 0; i < 8; i ++) { + bit = ((num - (num % 2 ^ i)) / 2 ^ i) % 2 + if (bit == 1) { + if (i % 2 == 1) { + out += 2 ^ (i - 1) + } + if (i % 2 == 0) { + out += 2 ^ (i + 1) + } + } + } + out +} diff --git a/challenge-120/abigail/bc/ch-2.bc b/challenge-120/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..6897ea94a8 --- /dev/null +++ b/challenge-120/abigail/bc/ch-2.bc @@ -0,0 +1,44 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc < input-file +# + +diff_per_minute = 11 +min_per_hour = 60 +full_circle = 720 + +while (1) { + hours = read () + if (hours == -1) { + break + } + minutes = read () + + # + # Result scale to 0, otherwise % acts weirdly. + # + scale = 0 + angle = (diff_per_minute * (hours * min_per_hour + minutes)) % full_circle + if (2 * angle >= full_circle) { + angle = full_circle - angle + } + + # + # If the angle is an odd number of half degrees, we want + # a result ending in ".5", but if it's an even number of + # half degrees, we don't want to end in ".0" + # + scale = angle % 2 + + # + # Little hack to print a half as "0.5" + # + if (angle == 1) { + "0" + } + + angle / 2 +} diff --git a/challenge-120/abigail/befunge-93/ch-2.bf93 b/challenge-120/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..103f2830bf --- /dev/null +++ b/challenge-120/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,12 @@ +> & :1+!#@_ ~$ 543*** &+ 65+ * 65432**** % : 6543*** `#v_v +^ v v +^ v<<<<<<<<<<<<<<<<<<<<<<<<<<<</2 : < -\ ****23456 : < v +^ v ^<<<<<<<<<<<<<<<<<<<< +^ v +^ > : 56+9* `!#v_ : 554** / "0"+, 554** % > : 55+ / "0"+, > 55+% "0"+, v +^ v ^ ^ v +^ >>>>>>>>>>>>>>>>>>> : 9 `#^_ ^ v +^ v +^ v ,,".5" < v +^ v ^ v +^<<<<<<<<<<<<<<<<<<<<<<<<<< , +55 <<<<<<<<<<<<<<<<<<<< _^# !%2 <<< diff --git a/challenge-120/abigail/c/ch-1.c b/challenge-120/abigail/c/ch-1.c new file mode 100644 index 0000000000..ae21fa18d5 --- /dev/null +++ b/challenge-120/abigail/c/ch-1.c @@ -0,0 +1,22 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + int num; + + while (scanf ("%d", &num) == 1) { + printf ("%d\n", (num & 0x55) << 1 + | (num & 0xAA) >> 1); + } + + return (0); +} diff --git a/challenge-120/abigail/c/ch-2.c b/challenge-120/abigail/c/ch-2.c new file mode 100644 index 0000000000..56e1872d0d --- /dev/null +++ b/challenge-120/abigail/c/ch-2.c @@ -0,0 +1,33 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +# define DIFF_PER_MINUTE 11 /* Half degrees */ +# define MIN_PER_HOUR 60 +# define FULL_CIRCLE 720 /* Half degrees */ + +int main (void) { + int hours, minutes; + + while (scanf ("%d:%d", &hours, &minutes) == 2) { + int angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE; + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + printf ("%d", angle / 2); + if (angle % 2) { + printf (".5"); + } + printf ("\n"); + } + return (0); +} diff --git a/challenge-120/abigail/go/ch-1.go b/challenge-120/abigail/go/ch-1.go new file mode 100644 index 0000000000..e5ebd26ebb --- /dev/null +++ b/challenge-120/abigail/go/ch-1.go @@ -0,0 +1,25 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +) + +func main () { + for { + var n, num int + n, _ = fmt . Scanf ("%d", &num) + if (n != 1) { + break + } + fmt . Printf ("%d\n", (num & 0x55) << 1 | + (num & 0xAA) >> 1) + } +} diff --git a/challenge-120/abigail/go/ch-2.go b/challenge-120/abigail/go/ch-2.go new file mode 100644 index 0000000000..a4a6f4ce95 --- /dev/null +++ b/challenge-120/abigail/go/ch-2.go @@ -0,0 +1,38 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" +) + +var DIFF_PER_MINUTE = 11; +var MIN_PER_HOUR = 60; +var FULL_CIRCLE = 720; + +func main () { + var hours, minutes int; + for { + var n, err = fmt . Scanf ("%d:%d", &hours, &minutes) + if (err != nil || n != 2) { + break; + } + var angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE; + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + + fmt . Print (angle / 2); + if (angle % 2 == 1) { + fmt . Print (".5") + } + fmt . Print ("\n") + } +} diff --git a/challenge-120/abigail/java/ch-1.java b/challenge-120/abigail/java/ch-1.java new file mode 100644 index 0000000000..66cf3ee4e7 --- /dev/null +++ b/challenge-120/abigail/java/ch-1.java @@ -0,0 +1,27 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + try { + while (true) { + int num = scanner . nextInt (); + System . out . println ( (num & 0x55) << 1 + | (num & 0xAA) >> 1); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-120/abigail/java/ch-2.java b/challenge-120/abigail/java/ch-2.java new file mode 100644 index 0000000000..9ffeb322ee --- /dev/null +++ b/challenge-120/abigail/java/ch-2.java @@ -0,0 +1,45 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + + +public class ch2 { + public static void main (String [] args) { + int DIFF_PER_MINUTE = 11; // Half degrees + int MIN_PER_HOUR = 60; + int FULL_CIRCLE = 720; // Half degrees + Scanner scanner = new Scanner (System . in); + try { + while (true) { + String line = scanner . nextLine (); + String [] parts = line . split (":"); + int hours = Integer . parseInt (parts [0]); + int minutes = Integer . parseInt (parts [1]); + + int angle = (DIFF_PER_MINUTE * + (hours * MIN_PER_HOUR + minutes)) % FULL_CIRCLE; + + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + + System . out . print (angle / 2); + if (angle % 2 == 1) { + System . out . print (".5"); + } + System . out . print ("\n"); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-120/abigail/lua/ch-1.lua b/challenge-120/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..0d5896f59c --- /dev/null +++ b/challenge-120/abigail/lua/ch-1.lua @@ -0,0 +1,27 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + num = tonumber (line) + out = 0 + for i = 0, 7 do + bit = math . floor ((num - (num % (2 ^ i))) / (2 ^ i)) % 2 + if bit == 1 + then + if i % 2 == 1 + then + out = out + 2 ^ (i - 1) + else + out = out + 2 ^ (i + 1) + end + end + end + print (out) +end diff --git a/challenge-120/abigail/lua/ch-2.lua b/challenge-120/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..31c18d0b5e --- /dev/null +++ b/challenge-120/abigail/lua/ch-2.lua @@ -0,0 +1,26 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local DIFF_PER_MINUTE = 11 +local MIN_PER_HOUR = 60 +local FULL_CIRCLE = 720 + +for line in io . lines () do + local _, _, hours, minutes = line : find ("([0-9][0-9]):([0-9][0-9])") + hours = tonumber (hours) + minutes = tonumber (minutes) + local angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE + if 2 * angle >= FULL_CIRCLE + then angle = FULL_CIRCLE - angle + end + + print (angle / 2) +end diff --git a/challenge-120/abigail/node/ch-1.js b/challenge-120/abigail/node/ch-1.js new file mode 100644 index 0000000000..b40fd0d3ac --- /dev/null +++ b/challenge-120/abigail/node/ch-1.js @@ -0,0 +1,16 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', (num) => { + console . log ( (+num & 0x55) << 1 + | (+num & 0xAA) >> 1) +}) diff --git a/challenge-120/abigail/node/ch-2.js b/challenge-120/abigail/node/ch-2.js new file mode 100644 index 0000000000..4159eafa6d --- /dev/null +++ b/challenge-120/abigail/node/ch-2.js @@ -0,0 +1,26 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +let DIFF_PER_MINUTE = 11 +let MIN_PER_HOUR = 60 +let FULL_CIRCLE = 720 + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [hours, minutes] = line . trim () . split (":") + angle = (DIFF_PER_MINUTE * (+hours * MIN_PER_HOUR + +minutes)) % + FULL_CIRCLE + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle + } + + console . log (angle / 2) +}) diff --git a/challenge-120/abigail/pascal/ch-1.p b/challenge-120/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..b16ebde46e --- /dev/null +++ b/challenge-120/abigail/pascal/ch-1.p @@ -0,0 +1,22 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var num: integer; + +begin + while true do begin + readln (num); + if num = 0 then begin + break; + end; + writeln ((num and $55) shl 1 or + (num and $AA) shr 1); + end +end. diff --git a/challenge-120/abigail/perl/ch-1.pl b/challenge-120/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..b5aa1b4b9d --- /dev/null +++ b/challenge-120/abigail/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# This is just week 119 with different constants +# + + +while (<>) { + say + ($_ & 0x55) << 1 # Odd bits shifted one the right + | ($_ & 0xAA) >> 1 # Even bits shifted one to the left +} + diff --git a/challenge-120/abigail/perl/ch-2.pl b/challenge-120/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..b533e7d8a6 --- /dev/null +++ b/challenge-120/abigail/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +my $MIN_PER_HOUR = 60; +my $DIFF_PER_MINUTE = 11; # Half degrees +my $FULL_CIRCLE = 720; # Half degrees + +while (<>) { + my ($hours, $minutes) = /[0-9]+/g; + # + # Every minute, the angle between the hour and minute hand + # increases by 5.5 degrees. So, we will calculate how many + # minutes have passed since 00:00, multiply this by 11, giving + # us the number of half degrees between two hands. + # We normalize the angle by modding it by 720. To get the smaller + # angle, if the angle is more than 360, we subtract the angle + # from 720 (the full circle). + # + # Finally, we divide by 2, to get the answer in degrees. + # + + # + # Note that this is going to work regardless whether the + # time is given in 12 hour format, a 24 hour format, or + # the silly format Americans use. + # + + my $angle = ($DIFF_PER_MINUTE * ($hours * $MIN_PER_HOUR + $minutes)) % + $FULL_CIRCLE; + $angle = $FULL_CIRCLE - $angle if 2 * $angle >= $FULL_CIRCLE; + + say $angle / 2; +} + +__END__ diff --git a/challenge-120/abigail/python/ch-1.py b/challenge-120/abigail/python/ch-1.py new file mode 100644 index 0000000000..510ce33d10 --- /dev/null +++ b/challenge-120/abigail/python/ch-1.py @@ -0,0 +1,16 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + num = int (line) + print ( (num & 0x55) << 1 + | (num & 0xAA) >> 1) diff --git a/challenge-120/abigail/python/ch-2.py b/challenge-120/abigail/python/ch-2.py new file mode 100644 index 0000000000..5c01644600 --- /dev/null +++ b/challenge-120/abigail/python/ch-2.py @@ -0,0 +1,27 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +DIFF_PER_MINUTE = 11 +MIN_PER_HOUR = 60 +FULL_CIRCLE = 720 + +for line in fileinput . input (): + hours, minutes = line . strip () . split (":") + angle = (DIFF_PER_MINUTE * (int (hours) * MIN_PER_HOUR + int (minutes))) \ + % FULL_CIRCLE + if 2 * angle >= FULL_CIRCLE: + angle = FULL_CIRCLE - angle + + print ("{}" . format (int (angle / 2)), end = '') + if angle % 2: + print (".5", end = '') + print ("") diff --git a/challenge-120/abigail/r/ch-1.r b/challenge-120/abigail/r/ch-1.r new file mode 100644 index 0000000000..b276380d71 --- /dev/null +++ b/challenge-120/abigail/r/ch-1.r |
