From 81f50c4150062cbd787a33583a53f7e32a4a2835 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 6 Dec 2021 09:14:28 +0000 Subject: Solutions for challenge #142 --- challenge-142/roger-bell-west/perl/ch-1.pl | 36 +++++++++++++++++++ challenge-142/roger-bell-west/perl/ch-2.pl | 35 +++++++++++++++++++ challenge-142/roger-bell-west/postscript/ch-1.ps | 44 ++++++++++++++++++++++++ challenge-142/roger-bell-west/python/ch-1.py | 35 +++++++++++++++++++ challenge-142/roger-bell-west/python/ch-2.py | 35 +++++++++++++++++++ challenge-142/roger-bell-west/raku/ch-1.p6 | 31 +++++++++++++++++ challenge-142/roger-bell-west/raku/ch-2.p6 | 27 +++++++++++++++ challenge-142/roger-bell-west/ruby/ch-1.rb | 40 +++++++++++++++++++++ challenge-142/roger-bell-west/ruby/ch-2.rb | 26 ++++++++++++++ challenge-142/roger-bell-west/rust/ch-1.rs | 44 ++++++++++++++++++++++++ challenge-142/roger-bell-west/rust/ch-2.rs | 41 ++++++++++++++++++++++ 11 files changed, 394 insertions(+) create mode 100755 challenge-142/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-142/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-142/roger-bell-west/postscript/ch-1.ps create mode 100755 challenge-142/roger-bell-west/python/ch-1.py create mode 100755 challenge-142/roger-bell-west/python/ch-2.py create mode 100755 challenge-142/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-142/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-142/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-142/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-142/roger-bell-west/rust/ch-1.rs create mode 100644 challenge-142/roger-bell-west/rust/ch-2.rs (limited to 'challenge-142') diff --git a/challenge-142/roger-bell-west/perl/ch-1.pl b/challenge-142/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..04ccf574dc --- /dev/null +++ b/challenge-142/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(dld(24,2),2,'example 1'); +is(dld(30,5),2,'example 2'); + +sub factor { + my $n=shift; + if ($n==1) { + return [1]; + } + my @ff; + my $s=int(sqrt($n)); + if ($s*$s == $n) { + push @ff,$s; + $s--; + } + foreach my $pf (2..$s) { + if ($n % $pf == 0) { + unshift @ff,$pf; + push @ff,$n/$pf; + } + } + unshift @ff,1; + push @ff,$n; + return \@ff; +} + +sub dld { + my ($m,$n)=@_; + return scalar grep {$_ % 10 == $n} @{factor($m)}; +} diff --git a/challenge-142/roger-bell-west/perl/ch-2.pl b/challenge-142/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..a3f7178bee --- /dev/null +++ b/challenge-142/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +my @a; +foreach (1..10) { + push @a,int(rand()*100+10); +} + +is_deeply(sleepsort(\@a),[sort {$a <=> $b} @a],'example 1'); + +use Parallel::ForkManager; +use Time::HiRes qw(usleep); + +sub sleepsort { + my $n=shift; + my @r; + my $pm=Parallel::ForkManager->new(scalar @{$n}); + $pm->set_waitpid_blocking_sleep(0); + $pm->run_on_finish(sub{ + if (defined $_[5]) { + push @r,${$_[5]}; + } + }); + foreach my $e (@{$n}) { + $pm->start and next; + usleep 1000*$e; + $pm->finish(0,\$e); + } + $pm->wait_all_children; + return \@r; +} diff --git a/challenge-142/roger-bell-west/postscript/ch-1.ps b/challenge-142/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..b12b135d7f --- /dev/null +++ b/challenge-142/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,44 @@ +%!PS + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/factor { + /nn exch def + nn 1 eq { + [ 1 ] + } { + /ff 0 array def + /s nn sqrt cvi def + s s mul nn eq { + /ff ff s apush def + /s s 1 sub def + } if + 2 1 s { + /pf exch def + nn pf mod 0 eq { + /ff ff pf apush def + /ff ff nn pf idiv apush def + } if + } for + /ff ff 1 apush nn apush def + ff + } ifelse +} bind def + +/dld { + /n exch def + /m exch def + /tx 0 def + m factor { + 10 mod n eq { + /tx tx 1 add def + } if + } forall + tx +} bind def + +24 2 dld 2 eq { (Pass) } { (FAIL) } ifelse print ( ) print +30 5 dld 2 eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-142/roger-bell-west/python/ch-1.py b/challenge-142/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..21808d0111 --- /dev/null +++ b/challenge-142/roger-bell-west/python/ch-1.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt +from collections import deque + +def factor(n): + if n==1: + return [1] + ff=deque() + s=int(sqrt(n)) + if s*s==n: + ff.append(s) + s-=1 + for pf in range(2,s+1): + if n % pf == 0: + ff.appendleft(pf) + ff.append(int(n/pf)) + ff.appendleft(1) + ff.append(n) + return ff + +def dld(m,n): + return len([i for i in factor(m) if i % 10 == n]) + +class TestDld(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(dld(24,2),2,'example 1') + + def test_ex2(self): + self.assertEqual(dld(30,5),2,'example 2') + +unittest.main() diff --git a/challenge-142/roger-bell-west/python/ch-2.py b/challenge-142/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..f377b7678a --- /dev/null +++ b/challenge-142/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +import unittest + +from random import randint +import multiprocessing as mp +from time import sleep + +def sleeper(x,q): + sleep(float(x)/500.0) + q.put(x) + +def sleepsort(n): + q=mp.SimpleQueue() + pq=[] + for i in n: + p=mp.Process(target=sleeper,args=(i,q)) + p.start() + pq.append(p) + for p in pq: + p.join() + r=[] + while not q.empty(): + r.append(q.get()) + return r + +class TestSleepsort(unittest.TestCase): + + def test_ex1(self): + a=[] + for i in range(10): + a.append(randint(1,100)) + self.assertEqual(sleepsort(a),sorted(a),'example 1') + +unittest.main() diff --git a/challenge-142/roger-bell-west/raku/ch-1.p6 b/challenge-142/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..d505ec7eac --- /dev/null +++ b/challenge-142/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(dld(24,2),2,'example 1'); +is(dld(30,5),2,'example 2'); + +sub factor($n) { + if ($n==1) { + return [1]; + } + my @ff; + my $s=floor(sqrt($n)); + if ($s*$s == $n) { + push @ff,$s; + $s--; + } + for (2..$s) -> $pf { + if ($n % $pf == 0) { + unshift @ff,$pf; + push @ff,$n div $pf; + } + } + return @ff; +} + +sub dld($m,$n) { + return elems(grep {$_ % 10 == $n}, factor($m)); +} diff --git a/challenge-142/roger-bell-west/raku/ch-2.p6 b/challenge-142/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..1af0ce46b8 --- /dev/null +++ b/challenge-142/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#! /usr/bin/perl6 + +use Test; +plan 1; + +my @a; +for (1..10) { + push @a,(^100).pick+10; +} + +is-deeply(sleepsort(@a),[sort @a],'example 1'); + +sub sleepsort(@a) { + my @r; + my $channel=Channel.new; + await (@a).map: -> $e { + start { + sleep $e/100; + $channel.send($e); + } + } + $channel.close; + for $channel.list -> $r { + push @r,$r; + } + return @r; +} diff --git a/challenge-142/roger-bell-west/ruby/ch-1.rb b/challenge-142/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..4e0aefc4a3 --- /dev/null +++ b/challenge-142/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,40 @@ +#! /usr/bin/ruby + +def factor(n) + if n==1 then + return [1] + end + ff=[] + s=Math.sqrt(n).floor + if s*s==n then + ff.push(s) + s-=1 + end + 2.upto(s) do |pf| + if n % pf == 0 then + ff.push(n/pf) + ff.unshift(pf) + end + end + ff.push(n) + ff.unshift(1) + return ff +end + +def dld(m,n) + return factor(m).find_all{|i| i % 10 == n}.length() +end + +require 'test/unit' + +class TestDld < Test::Unit::TestCase + + def test_ex1 + assert_equal(2,dld(24,2)) + end + + def test_ex2 + assert_equal(2,dld(30,5)) + end + +end diff --git a/challenge-142/roger-bell-west/ruby/ch-2.rb b/challenge-142/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1226a5f10d --- /dev/null +++ b/challenge-142/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,26 @@ +#! /usr/bin/ruby + +def sleepsort(n) + mutex=Mutex.new + r=[] + threads=n.collect do |x| + Thread.new do + sleep(0.001 * x) + mutex.synchronize {r.push(x)} + end + end + threads.each { |t| t.join } + return r +end + +require 'test/unit' + +class TestSleepsort < Test::Unit::TestCase + + def test_ex1 + r=Random.new + a=1.upto(10).map{rand(100)} + assert_equal(a.sort(),sleepsort(a)) + end + +end diff --git a/challenge-142/roger-bell-west/rust/ch-1.rs b/challenge-142/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..5cf0653c47 --- /dev/null +++ b/challenge-142/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,44 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::VecDeque; + +#[test] +fn test_ex1() { + assert_eq!(dld(24, 2), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(dld(30, 5), 2); +} + +fn factor(n: u32) -> VecDeque { + let mut ff = VecDeque::new(); + if n == 1 { + ff.push_front(1); + return ff; + } + let mut s = (n as f64).sqrt() as u32; + if s * s == n { + ff.push_back(s); + s -= 1; + } + for pf in 2..=s { + if n % pf == 0 { + ff.push_front(pf); + ff.push_back(n / pf); + } + } + ff.push_front(1); + ff.push_back(n); + ff +} + +fn dld(m: u32, n: u32) -> u32 { + factor(m) + .into_iter() + .filter(|i| i % 10 == n) + .collect::>() + .len() as u32 +} diff --git a/challenge-142/roger-bell-west/rust/ch-2.rs b/challenge-142/roger-bell-west/rust/ch-2.rs new file mode 100644 index 0000000000..cca2fe113c --- /dev/null +++ b/challenge-142/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,41 @@ +// Cargo.toml: +// [dependencies] +// rand = "0.8.4" + +use rand::Rng; +use std::sync::mpsc::channel; +use std::thread::{sleep, spawn}; +use std::time; + +#[test] +fn text_ex1() { + let mut a = vec![]; + let mut rng = rand::thread_rng(); + for _i in 1..=10 { + a.push(rng.gen_range(1..=100) as u32); + } + let mut b = a.clone(); + b.sort(); + assert_eq!(sleepsort(a), b); +} + +fn sleepsort(n: Vec) -> Vec { + let (sender, receiver) = channel(); + let mut handles = vec![]; + for &i in &n { + let ss = sender.clone(); + handles.push(spawn(move || { + sleep(time::Duration::from_millis(i as u64)); + ss.send(i).unwrap(); + })); + } + for handle in handles { + handle.join().unwrap(); + } + let mut ri = receiver.iter(); + let mut r = vec![]; + for _i in n { + r.push(ri.next().unwrap()); + } + r +} -- cgit From 33aeeee274e119a2b282714acdb2a3d08e794805 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Dec 2021 10:54:46 +0100 Subject: Task 1 done --- challenge-142/luca-ferrari/raku/ch-1.p6 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 challenge-142/luca-ferrari/raku/ch-1.p6 (limited to 'challenge-142') 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; +} -- cgit From 793bbdfbc76b468219d950485487c2b561a3dff0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Dec 2021 11:16:01 +0100 Subject: Task 2 done --- challenge-142/luca-ferrari/raku/ch-2.p6 | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-142/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-142') 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; +} -- cgit From 8b5022cf04fb7839cb1c25ec8463e738135fcb41 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Dec 2021 11:24:04 +0100 Subject: Task 1 PostgreSQL done --- challenge-142/luca-ferrari/postgresql/ch-1.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-142/luca-ferrari/postgresql/ch-1.sql (limited to 'challenge-142') 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; -- cgit From 7c96bc97153320c86ab1c3796002d11f8ba2f76b Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 6 Dec 2021 11:41:42 +0100 Subject: Add solutions to 142: Divisor Last Digit & Sleep Sort by E. Choroba --- challenge-142/e-choroba/perl/ch-1.pl | 30 ++++++++++++++++++++++++++++++ challenge-142/e-choroba/perl/ch-2.pl | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 challenge-142/e-choroba/perl/ch-1.pl create mode 100755 challenge-142/e-choroba/perl/ch-2.pl (limited to 'challenge-142') 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'; -- cgit From ceac7deb6f7bccbf587d2616fa7bcc72a27d1c2c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Dec 2021 12:00:31 +0100 Subject: Task 2 done in PostgreSQL. Kind of, since there is no support for threading. --- challenge-142/luca-ferrari/postgresql/ch-2.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 challenge-142/luca-ferrari/postgresql/ch-2.sh (limited to 'challenge-142') 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 -- cgit From 0a9d29d1e48bee36b8011eaf05f89cf5793201f0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Dec 2021 12:17:05 +0100 Subject: Blog references --- challenge-142/luca-ferrari/blog-1.txt | 1 + challenge-142/luca-ferrari/blog-2.txt | 1 + challenge-142/luca-ferrari/blog-3.txt | 1 + challenge-142/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-142/luca-ferrari/blog-1.txt create mode 100644 challenge-142/luca-ferrari/blog-2.txt create mode 100644 challenge-142/luca-ferrari/blog-3.txt create mode 100644 challenge-142/luca-ferrari/blog-4.txt (limited to 'challenge-142') 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 -- cgit From 576b2414b5710fb36761e040116b0f2b6233e359 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 6 Dec 2021 12:27:19 +0100 Subject: Solution for week 142. We lack the resources to implement task 2 (this requires a real time operating system, and a perl with threads compiled in). Task 1 is just a repeat of last week, and the week before. I can't be assed to copy-and-paste most of last weeks code, so, we just have a Perl solution. --- challenge-142/abigail/README.md | 31 ------------------------------- challenge-142/abigail/perl/ch-1.pl | 10 ++++++++++ challenge-142/abigail/perl/ch-2.pl | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 31 deletions(-) create mode 100644 challenge-142/abigail/perl/ch-1.pl create mode 100644 challenge-142/abigail/perl/ch-2.pl (limited to 'challenge-142') 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/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"; -- cgit From 0738a0e639d7b7e8f60cf0ecd60d2f5e6f8124b7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 6 Dec 2021 16:07:55 +0000 Subject: Add Perl solution to challenge 142 --- challenge-142/paulo-custodio/perl/ch-1.pl | 41 ++++++++++++++++++++++++++++++ challenge-142/paulo-custodio/perl/ch-2.pl | 24 +++++++++++++++++ challenge-142/paulo-custodio/t/test-1.yaml | 10 ++++++++ challenge-142/paulo-custodio/t/test-2.yaml | 14 ++++++++++ 4 files changed, 89 insertions(+) create mode 100644 challenge-142/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-142/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-142/paulo-custodio/t/test-1.yaml create mode 100644 challenge-142/paulo-custodio/t/test-2.yaml (limited to 'challenge-142') 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/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 -- cgit From 519d44c0a7356bc03840549e421446656f7f2813 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 6 Dec 2021 16:22:35 +0000 Subject: Add Python solution to challenge 142 --- challenge-142/paulo-custodio/python/ch-1.py | 42 +++++++++++++++++++++++++++++ challenge-142/paulo-custodio/python/ch-2.py | 26 ++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-142/paulo-custodio/python/ch-1.py create mode 100644 challenge-142/paulo-custodio/python/ch-2.py (limited to 'challenge-142') 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() -- cgit From 896c415abc215829ef87d7fde92ab5c8d4ae536f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 6 Dec 2021 20:58:06 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_142.xlsm | Bin 0 -> 25970 bytes challenge-142/eric-cheung/excel-vba/ch-1.bas | 42 +++++++++++++++++++++ challenge-142/eric-cheung/python/ch-2.py | 19 ++++++++++ 3 files changed, 61 insertions(+) create mode 100755 challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm create mode 100755 challenge-142/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-142/eric-cheung/python/ch-2.py (limited to 'challenge-142') diff --git a/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm b/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm new file mode 100755 index 0000000000..865d40e9de Binary files /dev/null and b/challenge-142/eric-cheung/excel-vba/Challenge_142.xlsm differ 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) + -- cgit From 54620721cb174feca58d62f153c204d26b0297f7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 6 Dec 2021 21:10:03 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-142/ulrich-rieke/cpp/ch-1.cpp | 21 +++++++++++++++++++++ challenge-142/ulrich-rieke/haskell/ch-1.hs | 8 ++++++++ challenge-142/ulrich-rieke/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-142/ulrich-rieke/raku/ch-1.raku | 12 ++++++++++++ challenge-142/ulrich-rieke/raku/ch-2.raku | 15 +++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 challenge-142/ulrich-rieke/cpp/ch-1.cpp create mode 100644 challenge-142/ulrich-rieke/haskell/ch-1.hs create mode 100644 challenge-142/ulrich-rieke/perl/ch-1.pl create mode 100644 challenge-142/ulrich-rieke/raku/ch-1.raku create mode 100644 challenge-142/ulrich-rieke/raku/ch-2.raku (limited to 'challenge-142') diff --git a/challenge-142/ulrich-rieke/cpp/ch-1.cpp b/challenge-142/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..59df3c1d2c --- /dev/null +++ b/challenge-142/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +int main( int argc, char * argv[ ] ) { + int m = std::atoi( argv[ 1 ] ) ; + int n = std::atoi( argv[ 2 ] ) ; + std::vector divisors ; + for ( int i = 1 ; i < m + 1 ; i++ ) { + if ( m % i == 0 ) { + divisors.push_back( i ) ; + } + } + int output = 0 ; + for ( int num : divisors ) { + if ( num % 10 == n ) + output++ ; + } + std::cout << output << std::endl ; + return 0 ; +} diff --git a/challenge-142/ulrich-rieke/haskell/ch-1.hs b/challenge-142/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..4276e04062 --- /dev/null +++ b/challenge-142/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,8 @@ +module Challenge142 + where + +solution :: Int -> Int -> Int +solution m n = length $ filter (\i -> mod i 10 == n) divisors + where + divisors :: [Int] + divisors = filter (\i -> mod m i == 0 ) [1 .. m] diff --git a/challenge-142/ulrich-rieke/perl/ch-1.pl b/challenge-142/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..0ba282f9f3 --- /dev/null +++ b/challenge-142/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +my $m = $ARGV[ 0 ] ; +my $n = $ARGV[ 1 ] ; +my @divisors ; +for my $i (1 .. $m ) { + if ( $m % $i == 0 ) { + push @divisors , $i ; + } +} +my $output = 0 ; +for my $num ( @divisors ) { + if ( $num % 10 == $n ) { + $output++ ; + } +} +say $output ; diff --git a/challenge-142/ulrich-rieke/raku/ch-1.raku b/challenge-142/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..d280806fbb --- /dev/null +++ b/challenge-142/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,12 @@ +use v6 ; + +subset Positive of Int where * > 0 ; +sub MAIN( Positive $m , Positive $n ) { + my @divisors ; + for (1 .. $m) -> $i { + if ( $m %% $i ) { + @divisors.push( $i ) ; + } + } + say @divisors.grep( { $_ % 10 == $n } ).elems ; +} diff --git a/challenge-142/ulrich-rieke/raku/ch-2.raku b/challenge-142/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..1f02a2aefa --- /dev/null +++ b/challenge-142/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,15 @@ +use v6 ; + +say "Enter a number of positive integers!" ; +my $line = $*IN.get ; +my @numbers = $line.split( /\s+/ ).map( { .Int } ) ; +my @threads = @numbers.map: { + Thread.start( + sub { + my $num = @numbers.pick ; + sleep $num ; + say $num ; + }, + ); +} +.finish for @threads ; -- cgit From 70b4063d3698c95d7370680844f4c378d1fcf548 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 7 Dec 2021 00:22:10 +0000 Subject: adding in readme and fixing ch-1.pl for edge case where `$m` is a square --- challenge-142/james-smith/README.md | 81 +++++++++++++++++++++++++++++++++- challenge-142/james-smith/blog.txt | 1 + challenge-142/james-smith/perl/ch-1.pl | 3 +- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 challenge-142/james-smith/blog.txt (limited to 'challenge-142') 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 index 78892f3f26..9964c36011 100644 --- a/challenge-142/james-smith/perl/ch-1.pl +++ b/challenge-142/james-smith/perl/ch-1.pl @@ -11,6 +11,7 @@ use Data::Dumper qw(Dumper); my @TESTS = ( [ 24, 2, 2 ], [ 30, 5, 2 ], + [ 121, 1, 2 ], [ 231, 1, 3 ], [ 242, 1 , 3 ], ); @@ -22,6 +23,6 @@ done_testing(); sub divisor_last_digit { my($m,$n)=@_; ($n==1?1:0)+grep{$_%10==$n} - map{$m%$_?():($_,$m/$_)} + map{$m%$_?():$m==$_*$_?($_):($_,$m/$_)} 2..sqrt$m; } -- cgit From 933d1d9072cace4431091c213cf58a0904f97926 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 7 Dec 2021 14:21:24 +0000 Subject: Blog post for challenge #142 --- challenge-142/roger-bell-west/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-142/roger-bell-west/blog.txt (limited to 'challenge-142') diff --git a/challenge-142/roger-bell-west/blog.txt b/challenge-142/roger-bell-west/blog.txt new file mode 100644 index 0000000000..26485ea16b --- /dev/null +++ b/challenge-142/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2021/12/The_Weekly_Challenge_142__The_Last_Digit_of_Sleep.html -- cgit From ee821caa5d4ce419bac9f9c200243dd1643fb325 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Dec 2021 16:40:48 +0000 Subject: - Added solutions by Robert DiCicco. --- challenge-142/robert-dicicco/perl/ch-1.pl | 73 +++++++++++++++++++++++++++++++ challenge-142/robert-dicicco/perl/ch-2.pl | 55 +++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 challenge-142/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-142/robert-dicicco/perl/ch-2.pl (limited to 'challenge-142') 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"); +} -- cgit From 798865a3f84e1f41d7285b3c0affec455010e2a2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 7 Dec 2021 20:43:10 +0100 Subject: Links to blogs --- challenge-142/abigail/blog.txt | 1 + challenge-142/abigail/blog1.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-142/abigail/blog.txt create mode 100644 challenge-142/abigail/blog1.txt (limited to 'challenge-142') 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 -- cgit