diff options
| author | Simon Green <mail@simon.green> | 2021-12-12 14:45:17 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2021-12-12 14:45:17 +1100 |
| commit | 277a6b908be328067cc11fb8353d5a88f91db2d8 (patch) | |
| tree | 6e4e5c0cb588b005bccb5ad0b99bb41e7967fa04 | |
| parent | a64c96703f1714a1598fe7fb90450a2763122643 (diff) | |
| download | perlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.tar.gz perlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.tar.bz2 perlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.zip | |
sgreen solutions to challenge 142
| -rw-r--r-- | challenge-142/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-142/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-142/sgreen/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-142/sgreen/perl/ch-2.pl | 34 | ||||
| -rwxr-xr-x | challenge-142/sgreen/python/ch-1.py | 40 | ||||
| -rwxr-xr-x | challenge-142/sgreen/python/ch-2.py | 29 |
6 files changed, 148 insertions, 2 deletions
diff --git a/challenge-142/sgreen/README.md b/challenge-142/sgreen/README.md index 559ca2a5da..afcad14008 100644 --- a/challenge-142/sgreen/README.md +++ b/challenge-142/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 141 +# The Weekly Challenge 142 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-141-133e) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-142-1gb3) diff --git a/challenge-142/sgreen/blog.txt b/challenge-142/sgreen/blog.txt new file mode 100644 index 0000000000..3649f584e5 --- /dev/null +++ b/challenge-142/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-142-1gb3 diff --git a/challenge-142/sgreen/perl/ch-1.pl b/challenge-142/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..aba9adb143 --- /dev/null +++ b/challenge-142/sgreen/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _get_divisors { + my $number = shift; + + # Every number is divisible by 1 + my @divisors = (1); + + # One only has one divisor + return @divisors if $number == 1; + + # Find other divisors + foreach my $i ( 2 .. int( $number / 2 ) ) { + if ( $number % $i == 0 ) { + push @divisors, $i; + } + } + + # ... including the number itself + push @divisors, $number; + + return @divisors; +} + +sub main { + my ( $m, $n ) = @_; + + die "The first must be a positive integer\n" + if $m < 1; + die "The second number must be between 1 and 9\n" + if $n < 1 or $n > 9; + + # Get a list of divisors and find those that end with n. + my @solution = grep { $_ % 10 == $n } _get_divisors($m); + say scalar(@solution); +} + +main(@ARGV); diff --git a/challenge-142/sgreen/perl/ch-2.pl b/challenge-142/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..a3f733ae4d --- /dev/null +++ b/challenge-142/sgreen/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use threads; +use feature 'say'; +use List::Util 'any'; +use Time::HiRes 'sleep'; + +sub _sleep_sort { + my $seconds = shift; + sleep $seconds; + say $seconds; +} + +sub main { + my @sleep_seconds = @_; + + if ( any { $_ < 0 } @sleep_seconds ) { + die "You can sort negative numbers\n"; + } + + my @threads = (); + foreach my $seconds (@sleep_seconds) { + my $new_thread = threads->create( '_sleep_sort', $seconds ); + push @threads, $new_thread; + } + + foreach my $t (@threads) { + $t->join(); + } +} + +main(@ARGV); diff --git a/challenge-142/sgreen/python/ch-1.py b/challenge-142/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7f45ee1156 --- /dev/null +++ b/challenge-142/sgreen/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import sys + + +def _get_divisors(number): + # Every number is divisible by 1 + divisors = [1] + + # One only has one divisor + if number == 1: + return divisors + + # Find other divisors + for i in range(2, int(number / 2) + 1): + if number % i == 0: + divisors.append(i) + + # ... including the number itself + divisors.append(number) + + return divisors + + +def main(inputs): + m = int(inputs[0]) + n = int(inputs[1]) + + if m < 1: + raise ValueError('The first number must be a positive integer') + if n < 1 or n > 9: + raise ValueError('The second number must be between 1 and 9') + + # Get a list of divisors and find those that end with n. + solution = [x for x in _get_divisors(m) if x % 10 == n] + print(len(solution)) + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/challenge-142/sgreen/python/ch-2.py b/challenge-142/sgreen/python/ch-2.py new file mode 100755 index 0000000000..1aebb131da --- /dev/null +++ b/challenge-142/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import sys +from time import sleep +from threading import Thread + + +def _sleep_sort(seconds): + sleep(float(seconds)) + print(seconds) + + +def main(inputs): + threads = [] + + if any(float(x) < 0 for x in inputs): + raise ValueError('You can sort negative numbers') + + for seconds in inputs: + new_thread = Thread(target=_sleep_sort, args=(seconds,)) + threads.append(new_thread) + new_thread.start() + + for t in threads: + t.join() + + +if __name__ == '__main__': + main(sys.argv[1:]) |
