diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-12-13 21:59:48 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-12-13 21:59:48 +0000 |
| commit | 83bbf539df77c2f4bb734d248fa13ae89e47dcf9 (patch) | |
| tree | 2f1911c69a45260055271d8bf8022a4251eaaebc | |
| parent | 526c322c070f34841e5a75a25a1d86bd370d88e8 (diff) | |
| parent | ecafe0e840b90c1462ec4ac924f3255dd3beb2c0 (diff) | |
| download | perlweeklychallenge-club-83bbf539df77c2f4bb734d248fa13ae89e47dcf9.tar.gz perlweeklychallenge-club-83bbf539df77c2f4bb734d248fa13ae89e47dcf9.tar.bz2 perlweeklychallenge-club-83bbf539df77c2f4bb734d248fa13ae89e47dcf9.zip | |
Merge remote-tracking branch 'upstream/master'
46 files changed, 3925 insertions, 2482 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:]) diff --git a/challenge-143/eric-cheung/excel-vba/Challenge_143.xlsm b/challenge-143/eric-cheung/excel-vba/Challenge_143.xlsm Binary files differnew file mode 100755 index 0000000000..895a479b7d --- /dev/null +++ b/challenge-143/eric-cheung/excel-vba/Challenge_143.xlsm diff --git a/challenge-143/eric-cheung/excel-vba/ch-2.bas b/challenge-143/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..101b9bea72 --- /dev/null +++ b/challenge-143/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,55 @@ +Attribute VB_Name = "ModTask_02"
+Option Explicit
+
+Public Const strMyTitle As String = "Eric Cheung"
+
+Sub Task_02() '' Stealthy Number
+
+ '' Const nInpuNum As Integer = 36 '' Example 1
+ '' Const nInpuNum As Integer = 12 '' Example 2
+ Const nInpuNum As Integer = 6 '' Example 3
+
+ Dim nLoop As Integer, nSubLoop As Integer, nCnt As Integer
+ Dim bIsPrime As Boolean
+
+ Dim nFactorArr_01() As Integer, nFactorArr_02()
+
+ ReDim nFactorArr_01(1 To 1)
+ ReDim nFactorArr_02(1 To 1)
+
+ nCnt = 1
+ nFactorArr_01(1) = 1: nFactorArr_02(1) = nInpuNum
+
+ bIsPrime = True
+ For nLoop = 2 To Sqr(nInpuNum)
+ If nInpuNum Mod nLoop = 0 Then
+ bIsPrime = False
+
+ nCnt = nCnt + 1
+ ReDim Preserve nFactorArr_01(1 To nCnt)
+ ReDim Preserve nFactorArr_02(1 To nCnt)
+
+ nFactorArr_01(nCnt) = nLoop
+ nFactorArr_02(nCnt) = nInpuNum / nLoop
+ End If
+ Next nLoop
+
+ If bIsPrime Then
+ MsgBox 0, vbOKOnly, strMyTitle
+ Exit Sub
+ End If
+
+ For nLoop = LBound(nFactorArr_01) To UBound(nFactorArr_01) - 1
+ For nSubLoop = nLoop + 1 To UBound(nFactorArr_01)
+ If Abs(nFactorArr_01(nLoop) + nFactorArr_02(nLoop) - nFactorArr_01(nSubLoop) - nFactorArr_02(nSubLoop)) = 1 Then
+ MsgBox 1, vbOKOnly, strMyTitle
+ Exit Sub
+ End If
+ Next nSubLoop
+ Next nLoop
+
+ MsgBox 0, vbOKOnly, strMyTitle
+
+End Sub
+
+
diff --git a/challenge-143/eric-cheung/python/ch-1.py b/challenge-143/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..4820b6c769 --- /dev/null +++ b/challenge-143/eric-cheung/python/ch-1.py @@ -0,0 +1,9 @@ +## Calculator
+import os
+
+strMathExpr = "10 + 20 - 5" ## Example 1
+## strMathExpr = "(10 + 20 - 5) * 2" ## Example 2
+
+print (strMathExpr + " = " + str(eval(strMathExpr)))
+
+os.system("pause")
diff --git a/challenge-143/mohammad-anwar/perl/ch-1.pl b/challenge-143/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..74a9fa647c --- /dev/null +++ b/challenge-143/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +=head1 + +Week 143: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-143 + +Task #1: Calculator + + You are given a string, `$s`, containing mathematical expression. + + Write a script to print the result of the mathematical expression. To keep it simple, please only accept `+ - * ()`. + +=cut + +use strict; +use warnings; +use Test::More; + +is(eval qq|10 + 20 - 5|, 25, 'Example 1'); +is(eval qq|(10 + 20 - 5) * 2|, 50, 'Example 2'); + +done_testing; + diff --git a/challenge-143/mohammad-anwar/python/ch-1.py b/challenge-143/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..fb116d96a1 --- /dev/null +++ b/challenge-143/mohammad-anwar/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +''' + +Week 143: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-143 + +Task #1: Calculator + + You are given a string, `$s`, containing mathematical expression. + + Write a script to print the result of the mathematical expression. To keep it simple, please only accept `+ - * ()`. + +''' + +import unittest + +# +# +# Unit test class + +class TestJortSort(unittest.TestCase): + + def test_example_1(self): + self.assertEqual(eval('10 + 20 - 5'), 25, 'Example 1') + + def test_example_2(self): + self.assertEqual(eval('(10 + 20 - 5) * 2'), 50, 'Example 2') + +unittest.main() diff --git a/challenge-143/olivier-delouya/perl/ch-1.sh b/challenge-143/olivier-delouya/perl/ch-1.sh new file mode 100644 index 0000000000..fa86b0e685 --- /dev/null +++ b/challenge-143/olivier-delouya/perl/ch-1.sh @@ -0,0 +1,5 @@ +calculator='print(eval($e));' + +perl -se $calculator -- -e="10 + 20 - 5" + +perl -se $calculator -- -e="(10 + 20 - 5) * 2" diff --git a/challenge-143/paulo-custodio/perl/ch-1.pl b/challenge-143/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..90b6fe6506 --- /dev/null +++ b/challenge-143/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# TASK #1 > Calculator +# Submitted by: Mohammad S Anwar +# You are given a string, $s, containing mathematical expression. +# +# Write a script to print the result of the mathematical expression. To keep +# it simple, please only accept + - * (). +# +# Example 1: +# Input: $s = "10 + 20 - 5" +# Output: 25 +# Example 2: +# Input: $s = "(10 + 20 - 5) * 2" +# Output: 50 + +# Note: alternative one-liner solution: say eval("@ARGV") + +use Modern::Perl; + +# ($input, $value) = expr($input) +sub expr { + my($input) = @_; + ($input, my $value) = factor($input); + while (1) { + if ($input =~ s/^\s*\*//) { + ($input, my $b) = factor($input); + $value *= $b; + } + elsif ($input =~ s/^\s*\///) { + ($input, my $b) = factor($input); + $value /= $b; + } + elsif ($input =~ /^\s*(?:\)|$)/) { + return ($input, $value); + } + else { + die "expected / or * at: $input\n"; + } + } +} + +# ($input, $value) = factor($input) +sub factor { + my($input) = @_; + ($input, my $value) = term($input); + while (1) { + if ($input =~ s/^\s*\+//) { + ($input, my $b) = term($input); + $value += $b; + } + elsif ($input =~ s/^\s*\-//) { + ($input, my $b) = term($input); + $value -= $b; + } + else { + return ($input, $value); + } + } +} + +# ($input, $value) = term($input) +sub term { + my($input) = @_; + while (1) { + if ($input =~ s/^\s*([-+]?\d+)//) { + return ($input, $1); + } + elsif ($input =~ s/^\s*\(//) { + ($input, my $value) = expr($input); + $input =~ s/^\s*\)// or die "expected ) at: $input\n"; + return ($input, $value); + } + else { + die "expected ( or number at: $input\n"; + } + } +} + +my $input = "@ARGV"; +($input, my $value) = expr($input); +say $value; diff --git a/challenge-143/paulo-custodio/perl/ch-2.pl b/challenge-143/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a805a8180b --- /dev/null +++ b/challenge-143/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# TASK #2 > Stealthy Number +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n. +# +# Write a script to find out if the given number is Stealthy Number. +# +# A positive integer N is stealthy, if there exist positive integers a, b, c, d +# such that a * b = c * d = N and a + b = c + d + 1. +# +# Example 1 +# Input: $n = 36 +# Output: 1 +# +# Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. +# Example 2 +# Input: $n = 12 +# Output: 1 +# +# Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 +# Example 3 +# Input: $n = 6 +# Output: 0 +# +# Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 + +use Modern::Perl; + +sub is_stealthy { + my($n) = @_; + for my $a (1..$n) { + if ($n % $a == 0) { + my $b = $n / $a; # a*b=n + for my $c (1..$n) { + if ($n % $c == 0) { + my $d = $n / $c; # c*d=n + if ($a+$b==$c+$d+1) { + return 1; + } + } + } + } + } + return 0; +} + +my $n = shift//1; +say is_stealthy($n); diff --git a/challenge-143/paulo-custodio/python/ch-1.py b/challenge-143/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..a81c52fd0e --- /dev/null +++ b/challenge-143/paulo-custodio/python/ch-1.py @@ -0,0 +1,72 @@ +#!/usr/bin/python3 + +# TASK #1 > Calculator +# Submitted by: Mohammad S Anwar +# You are given a string, $s, containing mathematical expression. +# +# Write a script to print the result of the mathematical expression. To keep +# it simple, please only accept + - * (). +# +# Example 1: +# Input: $s = "10 + 20 - 5" +# Output: 25 +# Example 2: +# Input: $s = "(10 + 20 - 5) * 2" +# Output: 50 + +import sys +import re + +def expr(input): + input, value = factor(input) + while True: + input = input.strip()+" " + if input[0]=='*': + input = input[1:] + input, b = factor(input) + value *= b + elif input[0]=='/': + input = input[1:] + input, b = factor(input) + value /= b + else: + return input, value + +def factor(input): + input, value = term(input) + while True: + input = input.strip()+" " + if input[0]=='+': + input = input[1:] + input, b = term(input) + value += b + elif input[0]=='-': + input = input[1:] + input, b = term(input) + value -= b + else: + return input, value + +def term(input): + input = input.strip()+" " + match = re.match(r"[-+]?\d+", input) + if match: + value = int(match.group(0)) + input = input[match.end(0):] + return input, value + elif input[0]=='(': + input = input[1:] + input, value = expr(input) + input = input.strip()+" " + if input[0]!=')': + print("expected )") + sys.exit(1) + input = input[1:] + return input, value + else: + print("expected ( or number") + sys.exit(1) + +input = " ".join(sys.argv[1:]) +input, value = expr(input) +print(value) diff --git a/challenge-143/paulo-custodio/python/ch-2.py b/challenge-143/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..3d1976d3f0 --- /dev/null +++ b/challenge-143/paulo-custodio/python/ch-2.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +# TASK #2 > Stealthy Number +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n. +# +# Write a script to find out if the given number is Stealthy Number. +# +# A positive integer N is stealthy, if there exist positive integers a, b, c, d +# such that a * b = c * d = N and a + b = c + d + 1. +# +# Example 1 +# Input: $n = 36 +# Output: 1 +# +# Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1. +# Example 2 +# Input: $n = 12 +# Output: 1 +# +# Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1 +# Example 3 +# Input: $n = 6 +# Output: 0 +# +# Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1 + +import sys + +def is_stealthy(n): + for a in range(1, n+1): + if n%a==0: + b = n/a # a*b=n + for c in range(1, n+1): + if n%c==0: + d = n/c # c*d=n + if a+b==c+d+1: + return 1 + return 0 + +n = int(sys.argv[1]) +print(is_stealthy(n)) diff --git a/challenge-143/paulo-custodio/t/test-1.yaml b/challenge-143/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f0a1e9d550 --- /dev/null +++ b/challenge-143/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 10 + 20 - 5 + input: + output: 25 +- setup: + cleanup: + args: 10 + 20 - -5 + input: + output: 35 +- setup: + cleanup: + args: "'(10 + 20 - 5) * 2'" + input: + output: 50 +- setup: + cleanup: + args: "'(10 + 20 - 5) / 2'" + input: + output: 12.5 +- setup: + cleanup: + args: "'(10 + 20 - 5) * -2'" + input: + output: -50 diff --git a/challenge-143/paulo-custodio/t/test-2.yaml b/challenge-143/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e301a2d362 --- /dev/null +++ b/challenge-143/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 36 + input: + output: 1 +- setup: + cleanup: + args: 12 + input: + output: 1 +- setup: + cleanup: + args: 6 + input: + output: 0 diff --git a/challenge-143/robert-dicicco/perl/ch-2.pl b/challenge-143/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..3bd0ea1527 --- /dev/null +++ b/challenge-143/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!perl.exe + +use strict; +use warnings; +use ntheory qw/divisors/; +use IO::Prompter; + +### Author: Robert DiCicco +### Date: 13-DEC-2021 +### Challenge #143 Stealthy Number + +# initialize vars +# array to hold list of divisors +my @final = (); + +# sum of initial set of divisors +my $target = 0; + +# limit to single digits +my $limit = 10; + +# Get number from user +my $inp = prompt 'Input the number : ', -integer => [ 1 .. 999 ]; +chomp($inp); +$inp = int($inp); + +# calculate list of divisors +my @d = divisors($inp); + +# get divisors and their sums +foreach(sort @d){ + #skip if divisor is greater than $limit + next if($_ >= $limit); + + my $val = $inp / $_; + + # is single digit + if($val < $limit) { + my $digit_sum = $val + $_; + if($target == 0){ + $target = $digit_sum; + push(@final,$val,$_); + } + + if($digit_sum == ($target - 1)){ + push(@final,$val,$_); + last; + } + } +} + +# if we have 4 divisors that meet the criteria, print +if(scalar(@final) == 4){ + print("$final[0] x $final[1] = $final[2] x $final[3] and $final[0] + $final[1] = $final[2] + $final[3] + 1\n"); +} else { + print("Not Stealthy!\n"); +} diff --git a/challenge-143/roger-bell-west/kotlin/ch-2.kt b/challenge-143/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..8a57e98251 --- /dev/null +++ b/challenge-143/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,53 @@ +fun factorpairs(n: Int): ArrayList<Int> { + if (n == 1) { + return ArrayList(2) + } + var ff=ArrayList<Int>() + var s=Math.sqrt(n.toDouble()).toInt() + if (s*s == n) { + ff.add(s*2) + s-- + } + for (pf in 2..s) { + if (n % pf == 0) { + ff.add(pf+n/pf) + } + } + ff.add(n+1) + return ff +} + +fun is_stealthy(n: Int): Boolean { + val p=factorpairs(n) + if (p.count() == 1) { + return false + } + for (ix in 0..p.count()-2) { + for (iy in ix+1..p.count()-1) { + if (Math.abs(p[ix]-p[iy])==1) { + return true + } + } + } + return false +} + +fun main() { + if (is_stealthy(36)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (is_stealthy(12)) { + print("Pass") |
