aboutsummaryrefslogtreecommitdiff
path: root/challenge-142
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-07 16:40:48 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-07 16:40:48 +0000
commitee821caa5d4ce419bac9f9c200243dd1643fb325 (patch)
treeda20ba90977c38dabe73f240a42d1b71089c0a7c /challenge-142
parent822dff5d9cc1a3daf3a800068f26040f9929e918 (diff)
downloadperlweeklychallenge-club-ee821caa5d4ce419bac9f9c200243dd1643fb325.tar.gz
perlweeklychallenge-club-ee821caa5d4ce419bac9f9c200243dd1643fb325.tar.bz2
perlweeklychallenge-club-ee821caa5d4ce419bac9f9c200243dd1643fb325.zip
- Added solutions by Robert DiCicco.
Diffstat (limited to 'challenge-142')
-rw-r--r--challenge-142/robert-dicicco/perl/ch-1.pl73
-rw-r--r--challenge-142/robert-dicicco/perl/ch-2.pl55
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-142/robert-dicicco/perl/ch-1.pl b/challenge-142/robert-dicicco/perl/ch-1.pl
new file mode 100644
index 0000000000..2b9cfd7ee7
--- /dev/null
+++ b/challenge-142/robert-dicicco/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!perl.exe
+
+use strict;
+use warnings;
+use ntheory qw/ divisors /;
+use IO::Prompter;
+
+### AUTHOR: Robert DiCicco
+### DATE: 06-DEC-2021
+### Challenge #142 Divisor Last Digit
+
+my @outlist = ();
+
+my $fnum = prompt 'Input the first number : ', -integer => [ 1 .. 99999 ];
+chomp($fnum);
+$fnum = int($fnum);
+
+my $snum = prompt 'Input the second number (last digit) : ',
+ -integer => [ 0 .. 9 ];
+chomp($snum);
+$snum = int($snum);
+
+# Get list of divisors for $fnum
+my @d = divisors($fnum);
+
+# And get rid of the last entry, which is $fnum
+pop(@d);
+
+# Check to see if we have saved anything to our array
+if ( scalar(@d) ) {
+ print("The divisors of $fnum are : @d\n");
+}
+else {
+ die "There are no divisors. Aborting\n";
+}
+
+# Foreach divisor in our array
+# get its last digit and save it in the outlist array
+
+foreach my $n (@d) {
+ my $retval = lastdigit($n);
+ if ( ( $retval == $snum ) and ( $fnum != $snum ) ) {
+ push( @outlist, $n );
+ }
+}
+
+# Print the count of those divisors that have the proper last digit,
+# and the list of those divisors
+
+if ( scalar(@outlist) ) {
+ print( "There are only "
+ . scalar(@outlist)
+ . " divisor\(s\) having last digit $snum\n" );
+ print("They are : @outlist\n");
+}
+else {
+ die "There are no divisors that have a last digit of $snum\n";
+}
+
+# Given an integer, return its last digit
+
+sub lastdigit {
+ my $len = length( $_[0] ) - 1;
+ # Return the number if it is only one digit long
+ if ( $len == 0 ) {
+ return ( $_[0] );
+ }
+ else {
+ # split into digits
+ my @spl = split( //, $_[0] );
+ return ( $spl[$len] );
+ }
+}
diff --git a/challenge-142/robert-dicicco/perl/ch-2.pl b/challenge-142/robert-dicicco/perl/ch-2.pl
new file mode 100644
index 0000000000..d75acee3a8
--- /dev/null
+++ b/challenge-142/robert-dicicco/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!perl.exe
+
+use strict;
+use warnings;
+use threads;
+use Time::HiRes qw/ usleep /;
+
+### AUTHOR: Robert DiCicco
+### DATE: 07-DEC-2021
+### Challenge #142 Sleep Sort
+
+my $start = Time::HiRes::time();
+print("\nStart Time : $start\n\n");
+
+# Original array to be sorted
+my @arr = qw/ 9 8 z g R o p x u a b 0 /;
+
+my @threads = ();
+
+# '0' = 48
+my $offset = 48;
+
+my $item = '';
+print("Array to be sorted : ");
+
+print("@arr\n");
+
+print("Sorted array : ");
+
+# Create a thread for each item in the source array
+# Send the item and its adjusted value to the sub
+
+foreach $item ( @arr ){
+ my $t = threads->create(\&sub1, $item,ord($item) - $offset);
+ # push the thread id into an array
+ push(@threads, $t);
+}
+
+# Join threads, read from array
+
+foreach (@threads) {
+ $_->join;
+}
+
+my $end = Time::HiRes::time();
+
+print("\n\nEnd Time : $end\n");
+print("\nElapsed Time : " . ($end - $start) . "\n");
+
+sub sub1 {
+ my $message = $_[0];
+ # HiRes microseconds are too quick, adjusted by multiplying by 10000
+ usleep($_[1] * 10000);
+ print(" $message");
+}