aboutsummaryrefslogtreecommitdiff
path: root/challenge-120
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-07-11 02:35:46 -0400
committerAdam Russell <ac.russell@live.com>2021-07-11 02:35:46 -0400
commite1b058a3c85a437d4f58de8c1091fa1719037e68 (patch)
treed5b5fba6b00088797c0666a8c5da7d529564fffa /challenge-120
parent091011ae56b19db7de27d84018d2c1cb015e3b4e (diff)
downloadperlweeklychallenge-club-e1b058a3c85a437d4f58de8c1091fa1719037e68.tar.gz
perlweeklychallenge-club-e1b058a3c85a437d4f58de8c1091fa1719037e68.tar.bz2
perlweeklychallenge-club-e1b058a3c85a437d4f58de8c1091fa1719037e68.zip
initial commit
Diffstat (limited to 'challenge-120')
-rw-r--r--challenge-120/adam-russell/blog.txt1
-rw-r--r--challenge-120/adam-russell/blog1.txt1
-rw-r--r--challenge-120/adam-russell/perl/ch-1.pl25
-rw-r--r--challenge-120/adam-russell/perl/ch-2.pl21
-rw-r--r--challenge-120/adam-russell/prolog/ch-1.p36
-rw-r--r--challenge-120/adam-russell/prolog/ch-2.p13
6 files changed, 97 insertions, 0 deletions
diff --git a/challenge-120/adam-russell/blog.txt b/challenge-120/adam-russell/blog.txt
new file mode 100644
index 0000000000..e58a800817
--- /dev/null
+++ b/challenge-120/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/07/11
diff --git a/challenge-120/adam-russell/blog1.txt b/challenge-120/adam-russell/blog1.txt
new file mode 100644
index 0000000000..d288a64822
--- /dev/null
+++ b/challenge-120/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/07/11
diff --git a/challenge-120/adam-russell/perl/ch-1.pl b/challenge-120/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..47a60bdc7a
--- /dev/null
+++ b/challenge-120/adam-russell/perl/ch-1.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+##
+# You are given a positive integer $N less than or equal to 255.
+# Write a script to swap the odd positioned bits with the even
+# positioned bits and print the decimal equivalent of the new binary representation.
+##
+sub swap_bits{
+ my($n) = @_;
+ my $bits = substr(unpack("B32", pack("N", shift)), 24, 8);
+ my @bits = split(//, $bits);
+ for(my $i = 0; $i < @bits; $i += 2){
+ @bits[$i, $i + 1] = @bits[$i + 1, $i];
+ }
+ my $swapped_decimal = unpack("N", pack("B32", substr("0" x 32 . join("", @bits), -32)));
+ return $swapped_decimal;
+}
+
+MAIN:{
+ my $N;
+ $N = 101;
+ print swap_bits($N) . "\n";
+ $N = 18;
+ print swap_bits($N) . "\n";
+}
diff --git a/challenge-120/adam-russell/perl/ch-2.pl b/challenge-120/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..142826e27d
--- /dev/null
+++ b/challenge-120/adam-russell/perl/ch-2.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+sub clock_angle{
+ my($h, $m) = split(/:/, $_[0]);
+ my $angle = abs(0.5 * (60 * $h - 11 * $m));
+ $angle = 360 - $angle if $angle > 180;
+ return $angle;
+}
+
+MAIN:{
+ my $T;
+ $T = "03:10";
+ print clock_angle($T) . "\n";
+ $T = "04:00";
+ print clock_angle($T) . "\n";
+}
diff --git a/challenge-120/adam-russell/prolog/ch-1.p b/challenge-120/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..f5a9fe7dbc
--- /dev/null
+++ b/challenge-120/adam-russell/prolog/ch-1.p
@@ -0,0 +1,36 @@
+:-initialization(main).
+
+pad(Bits, Padded):-
+ length(Bits, L),
+ PadLength is 8 - L,
+ length(Padding, PadLength),
+ maplist(=(0), Padding),
+ append(Padding, Bits, Padded).
+
+bits(N, Bits):-
+ bits(N, [], Bits).
+bits(0, Bits, Bits).
+bits(N, Bit_Accum, Bits):-
+ B is N /\ 1,
+ N0 is N >> 1,
+ bits(N0, [B|Bit_Accum], Bits).
+
+swap([], []).
+swap([H0, H1|T], [H1, H0|ST]):-
+ swap(T, ST).
+
+decimal(Bits, Decimal):-
+ decimal(Bits, 0, Decimal).
+decimal([], Decimal, Decimal).
+decimal([H|T], DecimalAccum, Decimal):-
+ length([H|T], B),
+ D is (H * 2 ** (B - 1)) + DecimalAccum,
+ decimal(T, D, Decimal).
+
+main:-
+ bits(18, B),
+ pad(B, Padded),
+ swap(Padded, Swapped),
+ decimal(Swapped, Decimal),
+ write(Decimal), nl,
+ halt.
diff --git a/challenge-120/adam-russell/prolog/ch-2.p b/challenge-120/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..e18b6fd4e5
--- /dev/null
+++ b/challenge-120/adam-russell/prolog/ch-2.p
@@ -0,0 +1,13 @@
+:-initialization(main).
+
+clock_angle(Time, Angle):-
+ append(H, [58|M], Time),
+ number_codes(Hour, H),
+ number_codes(Minutes, M),
+ A is abs(0.5 * (60 * Hour - 11 * Minutes)),
+ ((A > 180, Angle is 360 - A); Angle = A).
+
+main:-
+ clock_angle("03:10", Angle),
+ write(Angle), nl,
+ halt.