diff options
Diffstat (limited to 'challenge-142')
47 files changed, 1077 insertions, 32 deletions
diff --git a/challenge-142/abigail/README.md b/challenge-142/abigail/README.md index 0ebe05c131..d589af7261 100644 --- a/challenge-142/abigail/README.md +++ b/challenge-142/abigail/README.md @@ -2,35 +2,4 @@ ## Part 1 -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [Bc](bc/ch-1.bc) -* [C](c/ch-1.c) -* [Go](go/ch-1.go) -* [Java](java/ch-1.java) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [R](r/ch-1.r) -* [Ruby](ruby/ch-1.rb) -* [Tcl](tcl/ch-1.tcl) -* [Scheme](scheme/ch-1.scm) - -## Part 2 - -* [AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [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) -* [Pascal](pascal/ch-2.p) -* [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) -* [Scheme](scheme/ch-2.scm) diff --git a/challenge-142/abigail/blog.txt b/challenge-142/abigail/blog.txt new file mode 100644 index 0000000000..be8e3db343 --- /dev/null +++ b/challenge-142/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-142-1.html diff --git a/challenge-142/abigail/blog1.txt b/challenge-142/abigail/blog1.txt new file mode 100644 index 0000000000..c20dd656a2 --- /dev/null +++ b/challenge-142/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-142-2.html diff --git a/challenge-142/abigail/perl/ch-1.pl b/challenge-142/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..dcc92f41db --- /dev/null +++ b/challenge-142/abigail/perl/ch-1.pl @@ -0,0 +1,10 @@ +# +# This is the third week in a row where me need the divisors +# of a number. Kind of boring.... +# +# This is just a one liner, taking input from standard input. +# +# Math::Prime::divisors gives us the divisors of the first number; +# then it's a matter of grepping the ones ending with the second number. +# +perl -MMath::Prime::Util=divisors -pale '$_ = grep {/$F[1]$/} divisors $F[0]' diff --git a/challenge-142/abigail/perl/ch-2.pl b/challenge-142/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..90be35c6a8 --- /dev/null +++ b/challenge-142/abigail/perl/ch-2.pl @@ -0,0 +1,16 @@ +# +# To do this, we need two things: +# +# 1. A perl with threads compiled in. +# 2. A machine with a real-time OS (Windows, and almost all Unices, +# including Linux and MacOs will not do). +# +# 2. is necessary because the algorithm critically depends on sleeping +# for an exact amount of time. Most OSses cannot do this. I don't even +# know whether perl runs on any OS which can sleep an exact amount of time. +# +# I'm not even going to bother recompiling my perl to implement some +# stupid joke. Let alone buy another box. +# + +die "$^O will not do.\n"; diff --git a/challenge-142/e-choroba/perl/ch-1.pl b/challenge-142/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..86fe8a13cd --- /dev/null +++ b/challenge-142/e-choroba/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw{ signatures }; + +use List::Util qw{ uniq }; + +sub divisor_last_digit ($m, $n) { + my $count = 0; + for my $d_small (1 .. sqrt $m) { + next unless 0 == $m % $d_small; + + for my $d (uniq($d_small, $m / $d_small)) { + ++$count if $n eq substr $d, -1; + } + } + return $count +} + +use Test2::V0; +plan 3; + +# 2 12 +is divisor_last_digit(24, 2), 2, 'Example 1'; + +# 5 15 +is divisor_last_digit(30, 5), 2, 'Example 2'; + +# 10 20 40 50 80 100 200 250 400 500 1000 2000 +is divisor_last_digit(2000, 0), 12, 'Two thousand'; diff --git a/challenge-142/e-choroba/perl/ch-2.pl b/challenge-142/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..4e7fce46d3 --- /dev/null +++ b/challenge-142/e-choroba/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use threads; +use threads::shared; + +use Test2::V0; +plan 1; + +sub sleep_sort { + my @sorted :shared; + my @threads; + for my $n (@_) { + push @threads, + 'threads'->create(sub { sleep $n; push @sorted, $n }); + } + $_->join for @threads; + return @sorted +} + +my @numbers = map 1 + int rand 20, 1 .. 50; +is [sleep_sort(@numbers)], [sort { $a <=> $b } @numbers], 'same'; diff --git a/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm b/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm Binary files differnew file mode 100755 index 0000000000..865d40e9de --- /dev/null +++ b/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm diff --git a/challenge-142/eric-cheung/excel-vba/ch-1.bas b/challenge-142/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..aa90bb1542 --- /dev/null +++ b/challenge-142/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,42 @@ +Attribute VB_Name = "ModTask_01"
+Option Explicit
+
+Public Const strMyTitle As String = "Eric Cheung"
+
+Sub Task_01()
+
+ '' Example 1
+ '' Const nNumInput As Integer = 24
+ '' Const nLastDigit As Integer = 2
+
+ '' Example 2
+ Const nNumInput As Integer = 30
+ Const nLastDigit As Integer = 5
+
+ Dim nLoop As Integer
+ Dim nCount As Integer
+
+ nCount = 0
+
+ If nLastDigit = 1 Then
+ nCount = nCount + 1
+ End If
+
+ If nNumInput Mod 10 = nLastDigit Then
+ nCount = nCount + 1
+ End If
+
+ For nLoop = 2 To nNumInput - 2
+ If _
+ nNumInput Mod nLoop = 0 _
+ And nNumInput Mod 10 = nLastDigit _
+ Then
+ nCount = nCount + 1
+ End If
+ Next nLoop
+
+ MsgBox nCount, vbOKOnly, strMyTitle
+
+End Sub
+
+
diff --git a/challenge-142/eric-cheung/python/ch-2.py b/challenge-142/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..bc6bf07135 --- /dev/null +++ b/challenge-142/eric-cheung/python/ch-2.py @@ -0,0 +1,19 @@ +## Sleep Sort (For Positive Numbers)
+## Credit: https://gist.github.com/armorasha/47c7236cdfe25a928080692324d7f035
+## Credit: https://iq.opengenus.org/sleep-sort/
+## Python 3
+
+import _thread
+from time import sleep
+
+arrItem = [2, 4, 5, 2.5, 1, 7]
+
+def FuncSleepSort(nNum):
+ sleep(nNum)
+ print(nNum)
+
+
+for nItem in arrItem:
+ argItem = (nItem,)
+ _thread.start_new_thread(FuncSleepSort, argItem)
+
diff --git a/challenge-142/james-smith/README.md b/challenge-142/james-smith/README.md index 33593d9731..90c0a8816c 100644 --- a/challenge-142/james-smith/README.md +++ b/challenge-142/james-smith/README.md @@ -1 +1,80 @@ -Solutions by James Smith +# Perl Weekly Challenge #142 + +You can find more information about this weeks, and previous weeks challenges at: + + https://theweeklychallenge.org/ + +If you are not already doing the challenge - it is a good place to practise your +**perl** or **raku**. If it is not **perl** or **raku** you develop in - you can +submit solutions in whichever language you feel comfortable with. + +You can find the solutions here on github at: + +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-142/james-smith/perl + +# Challenge 1 - Divisor Last Digit + +***You are given positive integers, `$m` and `$n`. Write a script to find total count of divisors of `$m` having last digit `$n`.*** + +## The solution + +```perl +sub divisor_last_digit { + my($m,$n)=@_; + ($n==1?1:0)+grep{$_%10==$n} + map{$m%$_?():$m==$_*$_?($_):($_,$m/$_)} + 2..sqrt$m; +} +``` + + * First we find all the factors - by looping over all values between `2` and the square root of `$m`. If the value is a factor, so is `$m/$_`. + * We have a special case when `$m` is a square to avoid including the square root twice. + * We then `grep` to obtain those which have the correct last digit. + * There is one extra special case if `$n` is `1` we have to add `1` as `1` is a factor which we miss out in our calculations (so we don't + equally get `$m` as a factor). + +# Challenge 2 - Sleep sort + +***Another joke sort similar to JortSort suggested by champion Adam Russell. You are given a list of numbers. Write a script to implement Sleep Sort.*** + +To perform a sleep sort - we loop through the list of numbers, sleeping for `$value` seconds and updating the list of results with `$value` + +## The solution + +We need to parallelise this process + +There are different ways of doing this `fork`, `threads`, `Promises`. + +We will go for the `threads` approach as it easier to implement that `Promises` but doesn't eat at memory by forking lots of times. + +```perl +use threads; +use threads::shared; +use Time::HiRes qw(sleep); + +my @res :shared; +my @list=map{0.001*int rand 3000}1..20; + +say "@list"; + +sub sleeper {sleep$_[0];push@res,$_[0]} + +threads->new( \&sleeper, $_ ) for @list; + +$_->join for threads->list; + +say for @res; +``` + +## Notes + + * We create a test set of 20 values between `0` and `3`. + * We fire off all the threads (`threads->new`) + * Wait for them to finish `$_->join for threads->list` + * Return the results. + * As well as `use threads`, we also `use threads::shared`. This lets us declare the results array `@res` shareable across all processes, which we need to collect the values. + +## Caveat + +Not all threads start at the same time so sometimes results don't quite come back in the same order - especially if values are close together. + diff --git a/challenge-142/james-smith/blog.txt b/challenge-142/james-smith/blog.txt new file mode 100644 index 0000000000..d15115df91 --- /dev/null +++ b/challenge-142/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-142/james-smith diff --git a/challenge-142/james-smith/perl/ch-1.pl b/challenge-142/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..9964c36011 --- /dev/null +++ b/challenge-142/james-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 24, 2, 2 ], + [ 30, 5, 2 ], + [ 121, 1, 2 ], + [ 231, 1, 3 ], + [ 242, 1 , 3 ], +); + +is( divisor_last_digit($_->[0],$_->[1]), $_->[2] ) foreach @TESTS; + +done_testing(); + +sub divisor_last_digit { + my($m,$n)=@_; + ($n==1?1:0)+grep{$_%10==$n} + map{$m%$_?():$m==$_*$_?($_):($_,$m/$_)} + 2..sqrt$m; +} diff --git a/challenge-142/james-smith/perl/ch-2.pl b/challenge-142/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..5b7b33a428 --- /dev/null +++ b/challenge-142/james-smith/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); + +use threads; +use threads::shared; +use Time::HiRes qw(sleep); + +my @res :shared; +my @list=map{0.001*int rand 3000}1..20; + +say "@list"; + +sub sleeper {sleep$_[0];push@res,$_[0]} + +threads->new( \&sleeper, $_ ) for @list; + +$_->join for threads->list; + +say for @res; diff --git a/challenge-142/luca-ferrari/blog-1.txt b/challenge-142/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..1571806a3c --- /dev/null +++ b/challenge-142/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/06/PerlWeeklyChallenge142.html#task1 diff --git a/challenge-142/luca-ferrari/blog-2.txt b/challenge-142/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..0fc1e8004c --- /dev/null +++ b/challenge-142/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/06/PerlWeeklyChallenge142.html#task2 diff --git a/challenge-142/luca-ferrari/blog-3.txt b/challenge-142/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..dc1017883e --- /dev/null +++ b/challenge-142/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/06/PerlWeeklyChallenge142.html#task1pg diff --git a/challenge-142/luca-ferrari/blog-4.txt b/challenge-142/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..d5913735fe --- /dev/null +++ b/challenge-142/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/12/06/PerlWeeklyChallenge142.html#task2pg diff --git a/challenge-142/luca-ferrari/postgresql/ch-1.sql b/challenge-142/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..57de950651 --- /dev/null +++ b/challenge-142/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,20 @@ +CREATE OR REPLACE FUNCTION + f_divisors_last_digit( m int, n int ) + RETURNS SETOF int +AS $CODE$ + WITH RECURSIVE numbers AS ( + SELECT 1 as num + UNION + SELECT num + 1 + FROM numbers + WHERE num + 1 <= m ) + , divisors AS ( + SELECT num as div + FROM numbers + WHERE m % num = 0 ) + SELECT count(*) + FROM divisors + WHERE div::text LIKE ( '%' || n::text ); + + $CODE$ + LANGUAGE sql; diff --git a/challenge-142/luca-ferrari/postgresql/ch-2.sh b/challenge-142/luca-ferrari/postgresql/ch-2.sh new file mode 100644 index 0000000000..75ad30e1c8 --- /dev/null +++ b/challenge-142/luca-ferrari/postgresql/ch-2.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +for i in $*; do + psql -At -h miguel -U luca -c "SELECT pg_sleep( $i ); SELECT $i;" testdb & +done + +echo diff --git a/challenge-142/luca-ferrari/raku/ch-1.p6 b/challenge-142/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..a2315aba80 --- /dev/null +++ b/challenge-142/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!raku + +sub MAIN( Int $m where { $m > 1 } + , Int $n where { $n > 0 && $m > $n } ) { + ( 1 .. $m ).grep( $m %% * ).grep( * ~~ / ^ \d* $n $ / ).elems.say; +} diff --git a/challenge-142/luca-ferrari/raku/ch-2.p6 b/challenge-142/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..1989f64917 --- /dev/null +++ b/challenge-142/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,10 @@ +#!raku + +sub MAIN( *@n where { @n.grep( * ~~ Int ).elems == @n.elems } ) { + my @threads; + for @n -> $sleep { + @threads.push: Promise.in( $sleep.Int ).then( { $sleep.say } ); + } + + await @threads; +} diff --git a/challenge-142/paulo-custodio/perl/ch-1.pl b/challenge-142/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..76a8e19638 --- /dev/null +++ b/challenge-142/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +# TASK #1 > Divisor Last Digit +# Submitted by: Mohammad S Anwar +# You are given positive integers, $m and $n. +# +# Write a script to find total count of divisors of $m having last digit $n. +# +# +# Example 1: +# Input: $m = 24, $n = 2 +# Output: 2 +# +# The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12. +# There are only 2 divisors having last digit 2 are 2 and 12. +# +# Example 2: +# Input: $m = 30, $n = 5 +# Output: 2 +# +# The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15. +# There are only 2 divisors having last digit 5 are 5 and 15. + +use Modern::Perl; + +sub divisors { + my($n) = @_; + my(@div_low, @div_high); + for (my $i = 1; $i <= sqrt($n); $i++) { + if ($n%$i == 0) { + push @div_low, $i; + unshift @div_high, $n/$i if $n/$i != $i; + } + } + return (@div_low, @div_high); +} + +my($m, $n) = @ARGV; +my @divisors = divisors($m); +my $count = scalar grep {/$n$/} @divisors; +say $count; diff --git a/challenge-142/paulo-custodio/perl/ch-2.pl b/challenge-142/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..83160b0a28 --- /dev/null +++ b/challenge-142/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +# TASK #2 > Sleep Sort +# Submitted by: Adam Russell +# Another joke sort similar to JortSort suggested by champion Adam Russell. +# +# You are given a list of numbers. +# +# Write a script to implement Sleep Sort. For more information, please checkout +# this post. + +use Modern::Perl; +use Config; +use threads; + +sub sleeper { + my($n) = @_; + sleep $n; + say $n; +} + +my @thrs; +push @thrs, threads->create(\&sleeper, $_) for @ARGV; +$_->join() for @thrs; diff --git a/challenge-142/paulo-custodio/python/ch-1.py b/challenge-142/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d142b7d24f --- /dev/null +++ b/challenge-142/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +# TASK #1 > Divisor Last Digit +# Submitted by: Mohammad S Anwar +# You are given positive integers, $m and $n. +# +# Write a script to find total count of divisors of $m having last digit $n. +# +# +# Example 1: +# Input: $m = 24, $n = 2 +# Output: 2 +# +# The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12. +# There are only 2 divisors having last digit 2 are 2 and 12. +# +# Example 2: +# Input: $m = 30, $n = 5 +# Output: 2 +# +# The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15. +# There are only 2 divisors having last digit 5 are 5 and 15. + +import sys +import math +import re + +def divisors(n): + div_low = [] + div_high = [] + for i in range(1, int(math.sqrt(n)+1)): + if n%i==0: + div_low.append(i) + if n/i!=i: + div_high.append(int(n/i)) + div_high = div_high[::-1] + return [*div_low, *div_high] + +m = int(sys.argv[1]) +n = int(sys.argv[2]) +count = len(list(filter(lambda x: re.search(str(n)+"$", str(x)), divisors(m)))) +print(count) diff --git a/challenge-142/paulo-custodio/python/ch-2.py b/challenge-142/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..31f6f67abe --- /dev/null +++ b/challenge-142/paulo-custodio/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +# TASK #2 > Sleep Sort +# Submitted by: Adam Russell +# Another joke sort similar to JortSort suggested by champion Adam Russell. +# +# You are given a list of numbers. +# +# Write a script to implement Sleep Sort. For more information, please checkout +# this post. + +import sys +import threading +from time import sleep + +def sleeper(n): + sleep(n) + print(n) + +thrs = [] +for n in [int(x) for x in sys.argv[1:]]: + thr = threading.Thread(target=sleeper, args=[n]) + thrs.append(thr) + thr.start() +for thr in thrs: + thr.join() diff --git a/challenge-142/paulo-custodio/t/test-1.yaml b/challenge-142/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..73afb6c3bd --- /dev/null +++ b/challenge-142/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 24 2 + input: + output: 2 +- setup: + cleanup: + args: 30 5 + input: + output: 2 diff --git a/challenge-142/paulo-custodio/t/test-2.yaml b/challenge-142/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5880ec1526 --- /dev/null +++ b/challenge-142/paulo-custodio/t/test-2.yaml @@ -0,0 +1,14 @@ +- setup: + cleanup: + args: 1 9 3 2 8 6 7 5 4 + input: + output: | + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 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(@out |
