aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-28 21:46:42 +0000
committerGitHub <noreply@github.com>2021-11-28 21:46:42 +0000
commit285434168ac7cd95b30940aee74449abfae2ebd4 (patch)
tree131e95ecd6adce1ec041c8b1c7e4cd07b8bc1165
parent7a5ffcddd1614787ea7b6370a5f2ee0b82966024 (diff)
parentadb5b230f648d650fa42c27c26d0361cbb198a06 (diff)
downloadperlweeklychallenge-club-285434168ac7cd95b30940aee74449abfae2ebd4.tar.gz
perlweeklychallenge-club-285434168ac7cd95b30940aee74449abfae2ebd4.tar.bz2
perlweeklychallenge-club-285434168ac7cd95b30940aee74449abfae2ebd4.zip
Merge pull request #5295 from dcw803/master
imported my solutions to this week's tasks…
-rw-r--r--challenge-140/duncan-c-white/README76
-rwxr-xr-xchallenge-140/duncan-c-white/perl/ch-1.pl73
-rwxr-xr-xchallenge-140/duncan-c-white/perl/ch-2.pl72
3 files changed, 194 insertions, 27 deletions
diff --git a/challenge-140/duncan-c-white/README b/challenge-140/duncan-c-white/README
index d2eacb517a..90ddabae47 100644
--- a/challenge-140/duncan-c-white/README
+++ b/challenge-140/duncan-c-white/README
@@ -1,45 +1,67 @@
-TASK #1 - JortSort
+TASK #1 - Add Binary
-You are given a list of numbers.
+You are given two decimal-coded binary numbers, $a and $b.
-Write a script to implement JortSort. It should return true/false
-depending if the given list of numbers are already sorted.
+Write a script to simulate the addition of the given binary numbers.
-Example 1:
+Example 1
- Input: @n = (1,2,3,4,5)
- Output: 1
+ Input: $a = 11; $b = 1;
+ Output: 100
- Since the array is sorted, it prints 1.
+Example 2
-Example 2:
+ Input: $a = 101; $b = 1;
+ Output: 110
- Input: @n = (1,3,2,4,5)
- Output: 0
+Example 3
- Since the array is NOT sorted, it prints 0.
+ Input: $a = 100; $b = 11;
+ Output: 111
-MY NOTES: Very easy. Don't know what "JortSort" means, but this is
-just the linear "IsSorted" function..
+MY NOTES: Very easy. Extract least significant binary digit via $a % 10,
+implement full adder (bit,bit,carryin)->(bit,carryout), recurse and recombine.
-TASK #2 - Long Primes
+TASK #2 - Multiplication Table
-Write a script to generate first 5 Long Primes.
+You are given 3 positive integers, $i, $j and $k.
-A prime number (p) is called Long Prime if (1/p) has an infinite decimal expansion repeating every (p-1) digits.
+Write a script to print the $kth element in the sorted multiplication
+table of $i and $j.
-Example
+Example 1
- 7 is a long prime since 1/7 = 0.142857142857...
- The repeating part (142857) size is 6 i.e. one less than the prime number 7.
+ Input: $i = 2; $j = 3; $k = 4
+ Output: 3
- Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647...
- The repeating part (0588235294117647) size is 16 i.e. one less than the prime number 17.
+Since the multiplication of 2 x 3 is as below:
- Another example, 2 is not a long prime as 1/2 = 0.5.
- There is no repeating part in this case.
+ 1 2 3
+ 2 4 6
-MY NOTES: Sounds pretty easy. First generate some primes (have code to do that using Sieve of
- Erathosthenes). Then check each prime p to see if 1/p's 2*p long decimal fraction
- contains the same sequence p-1 digits long.
+The sorted multiplication table:
+
+ 1 2 2 3 4 6
+
+Now the 4th element in the table is "3".
+
+Example 2
+
+ Input: $i = 3; $j = 3; $k = 6
+ Output: 4
+
+Since the multiplication of 3 x 3 is as below:
+
+ 1 2 3
+ 2 4 6
+ 3 6 9
+
+The sorted multiplication table:
+
+ 1 2 2 3 3 4 6 6 9
+
+Now the 6th element in the table is "4".
+
+
+MY NOTES: Sounds pretty easy. The table's ith row is (1..$j) * $i.
diff --git a/challenge-140/duncan-c-white/perl/ch-1.pl b/challenge-140/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..1c0525f47e
--- /dev/null
+++ b/challenge-140/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+#
+# TASK #1 - Add Binary
+#
+# You are given two decimal-coded binary numbers, $a and $b.
+#
+# Write a script to simulate the addition of the given binary numbers.
+#
+# Example 1
+#
+# Input: $a = 11; $b = 1;
+# Output: 100
+#
+# Example 2
+#
+# Input: $a = 101; $b = 1;
+# Output: 110
+#
+# Example 3
+#
+# Input: $a = 100; $b = 11;
+# Output: 111
+#
+# MY NOTES: Very easy. Extract least significant binary digit via $a % 10,
+# implement full adder (bit,bit,carryin)->(bit,carryout), recurse and recombine.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Function::Parameters;
+#use Data::Dumper;
+
+
+my $debug=0;
+die "Usage: dcb-add x y\n" unless
+ GetOptions( "debug"=>\$debug ) && @ARGV==2;
+my( $x, $y ) = @ARGV;
+
+die "dcb-add: $x not a decimal coded binary value\n"
+ if $x eq '' || $x =~ /[^01]/;
+
+die "dcb-add: $y not a decimal coded binary value\n"
+ if $y eq '' || $y =~ /[^01]/;
+
+#
+# my $result = dcb_add( $x, $y, $carry );
+# Perform a binary addition of $x and $y, 2 decimal-encoded
+# binary values (i.e. in which every digit is either 0 and 1),
+# and the 0 or 1 $carry, and return the decimal-encoded result.
+#
+# I could make this iterative if desired, using a sum-so-far, and
+# a current-power-of-10 to multiply by.
+#
+fun dcb_add( $x, $y, $carry )
+{
+ my $lxd = $x % 10;
+ my $lyd = $y % 10;
+ my $sum = $lxd + $lyd + $carry;
+ $carry = int($sum/2);
+ $sum %= 2;
+ $x = int($x/10);
+ $y = int($y/10);
+ return $sum if $x == 0 && $y == 0 && $carry == 0;
+
+ my $result = dcb_add( $x, $y, $carry );
+ return 10 * $result + $sum;
+}
+
+
+my $result = dcb_add( $x, $y, 0 );
+say $result;
diff --git a/challenge-140/duncan-c-white/perl/ch-2.pl b/challenge-140/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..68663befcd
--- /dev/null
+++ b/challenge-140/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+#
+# TASK #2 - Multiplication Table
+#
+# You are given 3 positive integers, $i, $j and $k.
+#
+# Write a script to print the $kth element in the sorted multiplication
+# table of $i and $j.
+#
+# Example 1
+#
+# Input: $i = 2; $j = 3; $k = 4
+# Output: 3
+#
+# Since the multiplication of 2 x 3 is as below:
+#
+# 1 2 3
+# 2 4 6
+#
+# The sorted multiplication table:
+#
+# 1 2 2 3 4 6
+#
+# Now the 4th element in the table is "3".
+#
+# Example 2
+#
+# Input: $i = 3; $j = 3; $k = 6
+# Output: 4
+#
+# Since the multiplication of 3 x 3 is as below:
+#
+# 1 2 3
+# 2 4 6
+# 3 6 9
+#
+# The sorted multiplication table:
+#
+# 1 2 2 3 3 4 6 6 9
+#
+# Now the 6th element in the table is "4".
+#
+# MY NOTES: Sounds pretty easy. The table's ith row is (1..$j) * $i.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Function::Parameters;
+use Getopt::Long;
+use Data::Dumper;
+
+my $debug = 0;
+
+die "Usage: m-t [-d|--debug] I J K\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV==3;
+my( $i, $j, $k ) = @ARGV;
+
+my @table;
+
+foreach my $row (1..$i)
+{
+ foreach my $col (1..$j)
+ {
+ push @table, $col*$row;
+ }
+}
+#die Dumper \@table;
+
+@table = sort @table;
+
+say $table[$k-1];