diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-12-23 06:52:16 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-12-23 06:52:16 +0000 |
| commit | 77c3dd4216f84bafa208dd4c67018b3f714a932f (patch) | |
| tree | 9bc87c9f3462785737d165d41f5d3939854dfd26 | |
| parent | 4a21974fd493b6f19d141c278badf706470682d1 (diff) | |
| parent | 86ddf7b64e4859fc0b00eda0cb7cb0bdb62d12ad (diff) | |
| download | perlweeklychallenge-club-77c3dd4216f84bafa208dd4c67018b3f714a932f.tar.gz perlweeklychallenge-club-77c3dd4216f84bafa208dd4c67018b3f714a932f.tar.bz2 perlweeklychallenge-club-77c3dd4216f84bafa208dd4c67018b3f714a932f.zip | |
Merge remote-tracking branch 'upstream/master'
48 files changed, 2326 insertions, 1371 deletions
diff --git a/challenge-029/paulo-custodio/Makefile b/challenge-029/paulo-custodio/Makefile index c3c762d746..2b745f7bea 100644 --- a/challenge-029/paulo-custodio/Makefile +++ b/challenge-029/paulo-custodio/Makefile @@ -1,2 +1,14 @@ -all: +all: test + +test: libcmult.so perl ../../challenge-001/paulo-custodio/test.pl + +cmult.o: cmult.c + gcc -c -fpic cmult.c + +libcmult.so: cmult.o + gcc -shared -o $@ cmult.o + +clean: + $(RM) cmult.o libcmult.so *~ + $(RM) -rf _Inline diff --git a/challenge-029/paulo-custodio/cmult.c b/challenge-029/paulo-custodio/cmult.c new file mode 100644 index 0000000000..8306d9597c --- /dev/null +++ b/challenge-029/paulo-custodio/cmult.c @@ -0,0 +1,3 @@ +float cmult(int a, float b) { + return (float)a * b; +} diff --git a/challenge-029/paulo-custodio/perl/ch-2.pl b/challenge-029/paulo-custodio/perl/ch-2.pl index c33fa95943..e444dcfaf6 100644 --- a/challenge-029/paulo-custodio/perl/ch-2.pl +++ b/challenge-029/paulo-custodio/perl/ch-2.pl @@ -7,10 +7,8 @@ # defined or standard C function. use Modern::Perl; -use Inline C => <<'END'; - int sum(int a, int b) { - return a+b; - } -END +use Path::Tiny; -say sum(@ARGV); +use Inline C => path("cmult.c")->slurp(); + +say sprintf("%.2f", cmult(@ARGV)); diff --git a/challenge-029/paulo-custodio/python/ch-1.py b/challenge-029/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..11e598d80c --- /dev/null +++ b/challenge-029/paulo-custodio/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +# Challenge 029 + +# Task #1 +# Write a script to demonstrate brace expansion. For example, script would take +# command line argument Perl {Daily,Weekly,Monthly,Yearly} Challenge and should +# expand it and print like below: +# +# Perl Daily Challenge +# Perl Weekly Challenge +# Perl Monthly Challenge +# Perl Yearly Challenge + +import sys +import re + +def print_expanded(text): + mo = re.search(r"[{]([^{}]*?)[}]", text) + if mo: + before = text[:mo.start(0)] + expand = mo.group(1) + after = text[mo.end(0):] + + for arg in expand.split(","): + print_expanded(before+arg+after) + else: + print(text) + +print_expanded(" ".join(sys.argv[1:])) diff --git a/challenge-029/paulo-custodio/python/ch-2.py b/challenge-029/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e758df9016 --- /dev/null +++ b/challenge-029/paulo-custodio/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# Challenge 029 + +# Task #2 +# Write a script to demonstrate calling a C function. It could be any user +# defined or standard C function. + +import sys +import ctypes +import pathlib + +# Load the shared library into ctypes +libname = pathlib.Path().absolute() / "libcmult.so" +c_lib = ctypes.CDLL(libname) +c_lib.cmult.restype = ctypes.c_float + +x = int(sys.argv[1]) +y = float(sys.argv[2]) +result = c_lib.cmult(x, ctypes.c_float(y)) +print(f"{result:.2f}") diff --git a/challenge-029/paulo-custodio/t/test-2.yaml b/challenge-029/paulo-custodio/t/test-2.yaml index 45d285dccc..db5057e526 100644 --- a/challenge-029/paulo-custodio/t/test-2.yaml +++ b/challenge-029/paulo-custodio/t/test-2.yaml @@ -1,5 +1,5 @@ - setup: cleanup: - args: 2 3 + args: 3 3.14 input: - output: 5 + output: 9.42 diff --git a/challenge-030/paulo-custodio/Makefile b/challenge-030/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-030/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-030/paulo-custodio/README b/challenge-030/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-030/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-030/paulo-custodio/perl/ch-1.pl b/challenge-030/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..7160d8840b --- /dev/null +++ b/challenge-030/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# Challenge 030 +# +# Task #1 +# Write a script to list dates for Sunday Christmas between 2019 and 2100. For +# example, 25 Dec 2022 is Sunday. + +use Modern::Perl; +use DateTime; + +my @sunday_xmas; +for my $year (2019..2100) { + my $date = DateTime->new(year=>$year, month=>12, day=>25); + push @sunday_xmas, $year if $date->day_of_week == 7; +} + +say join(", ", @sunday_xmas); diff --git a/challenge-030/paulo-custodio/perl/ch-2.pl b/challenge-030/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..974dca199a --- /dev/null +++ b/challenge-030/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# Challenge 030 +# +# Task #2 +# Write a script to print all possible series of 3 positive numbers, where in +# each series at least one of the number is even and sum of the three numbers +# is always 12. For example, 3,4,5. + +use Modern::Perl; +use List::Util 'sum'; +use List::MoreUtils 'any'; + +my $sum = shift||12; + +for my $i (1..$sum) { + for my $j ($i+1..$sum) { + for my $k ($j+1..$sum) { + if (sum($i, $j, $k) == $sum) { + if (any {$_%2==0} $i, $j, $k) { + say "$i,$j,$k" + } + } + } + } +} diff --git a/challenge-030/paulo-custodio/python/ch-1.py b/challenge-030/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..4f15e8d7b5 --- /dev/null +++ b/challenge-030/paulo-custodio/python/ch-1.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 + +# Challenge 030 +# +# Task #1 +# Write a script to list dates for Sunday Christmas between 2019 and 2100. For +# example, 25 Dec 2022 is Sunday. + +import datetime + +sunday_xmas = [] +for year in range(2019, 2101): + dt = datetime.date(year, 12, 25) + if dt.isoweekday()==7: + sunday_xmas.append(year) +print(*sunday_xmas, sep=", ") diff --git a/challenge-030/paulo-custodio/python/ch-2.py b/challenge-030/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..29da0ffd41 --- /dev/null +++ b/challenge-030/paulo-custodio/python/ch-2.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 + +# Challenge 030 +# +# Task #2 +# Write a script to print all possible series of 3 positive numbers, where in +# each series at least one of the number is even and sum of the three numbers +# is always 12. For example, 3,4,5. + +import sys + +S = int(sys.argv[1]) + +for i in range(1, S+1): + for j in range(i+1, S+1): + for k in range(j+1, S+1): + if sum([i, j, k]) == S: + if any([x%2==0 for x in [i, j, k]]): + print(f"{i},{j},{k}") diff --git a/challenge-030/paulo-custodio/t/test-1.yaml b/challenge-030/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..679021db33 --- /dev/null +++ b/challenge-030/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095 diff --git a/challenge-030/paulo-custodio/t/test-2.yaml b/challenge-030/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b0782d35ac --- /dev/null +++ b/challenge-030/paulo-custodio/t/test-2.yaml @@ -0,0 +1,12 @@ +- setup: + cleanup: + args: 12 + input: + output: | + 1,2,9 + 1,3,8 + 1,4,7 + 1,5,6 + 2,3,7 + 2,4,6 + 3,4,5 diff --git a/challenge-144/dave-jacoby/blog.txt b/challenge-144/dave-jacoby/blog.txt new file mode 100644 index 0000000000..c2963f5c71 --- /dev/null +++ b/challenge-144/dave-jacoby/blog.txt @@ -0,0 +1,2 @@ +https://jacoby.github.io/2021/12/20/almost-prime-and-in-sequence-the-weekly-challenge-144.html + diff --git a/challenge-144/dave-jacoby/perl/ch-1.pl b/challenge-144/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..24672bb570 --- /dev/null +++ b/challenge-144/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +say join ', ', grep { is_semiprime($_) } 1 .. 100; + +sub is_semiprime ($n ) { + my $done; + return 0 if is_prime($n); + my @factors = + grep { !$done->{ $_->[0] }{ $_->[1] }++ } # avoid replication + grep { is_prime( $_->[0] ) } # factor 1 is prime + grep { is_prime( $_->[1] ) } # factor 2 is prime + map { [ sort $_, $n / $_ ] } # both applicable factors + grep { 0 == $n % $_ } # is a factor + 2 .. sqrt $n; + return scalar @factors == 1 ? 1 : 0; +} + +sub is_prime ($n) { + for ( 2 .. sqrt $n ) { return unless $n % $_ } + return 1; +} diff --git a/challenge-144/dave-jacoby/perl/ch-2.pl b/challenge-144/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..93e98d9a72 --- /dev/null +++ b/challenge-144/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use JSON; +my $json = JSON->new; + +my @examples; +push @examples, [ 1, 2 ]; +push @examples, [ 2, 3 ]; +push @examples, [ 2, 5 ]; +push @examples, [ 5, 7 ]; + +for my $x (@examples) { + say '-' x 20; + my ( $u, $v ) = $x->@*; + my @sequence = ulam( $u, $v ); + my $sequence = join ', ', sort { $a <=> $b } @sequence; + say <<"END"; + Input: \$u = $u, \$v = $v + Output: $sequence +END +} + +sub ulam ( $u = 1, $v = 2 ) { + my %output; + my @output; + + # cover the base cases + $output{$u} = 1; + $output{$v} = 1; + + my ($c) = sort { $b <=> $a } $u, $v; + while (1) { + $c++; + + # ensure that non-Ulam numbers ("exactly one way") + # get weeded out + map { delete $output{$_} } grep { $output{$_} > 1 } + keys %output; + @output = sort { $a <=> $b } keys %output; + + # testing early, because of the filter + return @output if scalar @output == 10; + + for my $i ( 0 .. -2 + scalar @output ) { + my $x = $output[$i]; + for my $j ( $i + 1 .. -1 + scalar @output ) { + my $y = $output[$j]; + my $d = $x + $y; + if ( $c == $d ) { + $output{$c}++; + } + } + } + } + + # "Remember the impossible scenario we never planned for?" + return []; +} diff --git a/challenge-144/david-santiago/raku/ch-1.raku b/challenge-144/david-santiago/raku/ch-1.raku new file mode 100755 index 0000000000..422fb4955f --- /dev/null +++ b/challenge-144/david-santiago/raku/ch-1.raku @@ -0,0 +1,64 @@ +#!/usr/bin/env raku + +sub sieve-of-atkin(Int $limit --> List){ + my @sieve; + my @prime-list = (2,3); + + my $x = 1; + while $x ** 2 < $limit { + my $y = 1; + while $y ** 2 < $limit { + + my $n = 4 * $x ** 2 + $y ** 2; + + if $n <= $limit && ($n % 12 == 1 || $n % 12 == 5) { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + + $n = 3 * $x ** 2 + $y ** 2; + if $n <= $limit && $n % 12 == 7 { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + + $n = (3 * $x ** 2 ) - $y ** 2; + if $n <= $limit && $x > $y && $n % 12 == 11 { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + $y++; + } + $x++; + } + # Mark all multiples of squares as non-prime + my $r = 5; + while $r ** 2 < $limit { + if @sieve[$r] { + loop ( my $i = $r**2; $i < $limit; $i += $r**2 ) { + @sieve[$i] = False; + } + } + $r++; + } + + for @sieve.kv -> $index, $is-prime { + @prime-list.append($index) if $is-prime; + } + + return @prime-list; +} + + +sub MAIN() { + my @prime-list := sieve-of-atkin(100); + my @combinations = (|@prime-list xx 2 ).flat.combinations(2); + + my %semi-primes; + for @combinations -> @comb { + my $candidate = @comb[0]*@comb[1]; + %semi-primes{$candidate} = (@comb[0],@comb[1]) if $candidate < 100; + } + + for %semi-primes.keys.sort { + say "$_ is Semiprime as $_ = {%semi-primes{$_}.join(' x ')}"; + } + +} diff --git a/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm b/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm Binary files differnew file mode 100755 index 0000000000..9afa059c40 --- /dev/null +++ b/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm diff --git a/challenge-144/eric-cheung/excel-vba/ch-1.bas b/challenge-144/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..68ed30b1ef --- /dev/null +++ b/challenge-144/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,76 @@ +Attribute VB_Name = "ModTask_01"
+Option Explicit
+
+Public Const strMyTitle As String = "Eric Cheung"
+
+Function IsPrime(nInput As Integer) As Boolean
+
+ Dim nLoop As Integer
+
+ If nInput = 1 Then
+ IsPrime = False
+ Exit Function
+ End If
+
+ For nLoop = 2 To Int(Sqr(nInput))
+ If nInput Mod nLoop = 0 Then
+ IsPrime = False
+ Exit Function
+ End If
+ Next nLoop
+
+ IsPrime = True
+
+End Function
+
+Function IsSemiPrime(nInput As Integer) As Boolean
+
+ Dim nPrimeLoop As Integer
+ Dim bByPassLoop As Boolean
+
+ For nPrimeLoop = 2 To nInput - 2
+
+ bByPassLoop = False
+
+ If nInput Mod nPrimeLoop > 0 Then
+ bByPassLoop = True
+ End If
+
+ If Not bByPassLoop And Not IsPrime(nPrimeLoop) Then
+ bByPassLoop = True
+ End If
+
+ If Not bByPassLoop And Not IsPrime(nInput / nPrimeLoop) Then
+ bByPassLoop = True
+ End If
+
+ If Not bByPassLoop Then
+ IsSemiPrime = True
+ Exit Function
+ End If
+
+ Next nPrimeLoop
+
+ IsSemiPrime = False
+
+End Function
+
+Sub Task_01()
+
+ Dim strMsg As String
+ Dim nMainLoop As Integer
+
+ For nMainLoop = 2 To 100
+ If IsSemiPrime(nMainLoop) Then
+ If strMsg <> "" Then
+ strMsg = strMsg & ", "
+ End If
+ strMsg = strMsg & nMainLoop
+ End If
+ Next nMainLoop
+
+ MsgBox strMsg, vbOKOnly, strMyTitle
+
+End Sub
+
+
diff --git a/challenge-144/eric-cheung/excel-vba/ch-2.bas b/challenge-144/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..08e1f84f0e --- /dev/null +++ b/challenge-144/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,87 @@ +Attribute VB_Name = "ModTask_02"
+Option Explicit
+Public nNumArr() As Integer
+
+Function IsUniqComb(nInput As Integer) As Boolean
+
+ Dim nSubLoop_01 As Integer, nSubLoop_02 As Integer
+ Dim bOccur As Boolean
+
+ bOccur = False
+ IsUniqComb = True
+
+ For nSubLoop_01 = LBound(nNumArr) To UBound(nNumArr) - 1
+ For nSubLoop_02 = nSubLoop_01 + 1 To UBound(nNumArr)
+ If nNumArr(nSubLoop_01) + nNumArr(nSubLoop_02) = nInput Then
+ If Not bOccur Then
+ bOccur = True
+ Else
+ IsUniqComb = False
+ Exit Function
+ End If
+ End If
+ Next nSubLoop_02
+ Next nSubLoop_01
+
+End Function
+
+Sub Task_02()
+
+ '' Example 1
+ '' Const nNum_01 As Integer = 1
+ '' Const nNum_02 As Integer = 2
+
+ '' Example 2
+ '' Const nNum_01 As Integer = 2
+ '' Const nNum_02 As Integer = 3
+
+ '' Example 3
+ Const nNum_01 As Integer = 2
+ Const nNum_02 As Integer = 5
+
+ Const nNumMax As Integer = 10
+
+ Dim strMsg As String
+
+ Dim nLoop_01 As Integer, nLoop_02 As Integer, nLoopCnt As Integer
+ Dim nLastNum As Integer, nTempNum As Integer
+
+ ReDim nNumArr(1 To 3)
+
+ nNumArr(1) = nNum_01: nNumArr(2) = nNum_02: nNumArr(3) = nNum_01 + nNum_02: nLastNum = nNumArr(3)
+
+ nLoopCnt = 3
+
+ Do While (nLoopCnt < nNumMax)
+
+ nTempNum = 9999
+ For nLoop_01 = LBound(nNumArr) To UBound(nNumArr) - 1
+ For nLoop_02 = nLoop_01 + 1 To UBound(nNumArr)
+ If _
+ nNumArr(nLoop_01) + nNumArr(nLoop_02) > nLastNum _
+ And nNumArr(nLoop_01) + nNumArr(nLoop_02) < nTempNum _
+ Then
+ nTempNum = nNumArr(nLoop_01) + nNumArr(nLoop_02)
+ End If
+ Next nLoop_02
+ Next nLoop_01
+
+ If IsUniqComb(nTempNum) Then
+ nLoopCnt = nLoopCnt + 1
+ ReDim Preserve nNumArr(1 To nLoopCnt)
+ nNumArr(nLoopCnt) = nTempNum
+ End If
+
+ nLastNum = nTempNum
+ Loop
+
+ For nLoop_01 = LBound(nNumArr) To UBound(nNumArr)
+ If strMsg <> "" Then
+ strMsg = strMsg & ", "
+ End If
+ strMsg = strMsg & nNumArr(nLoop_01)
+ Next nLoop_01
+
+ MsgBox strMsg, vbOKOnly, strMyTitle
+
+End Sub
diff --git a/challenge-144/mark-anderson/raku/ch-1.raku b/challenge-144/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..e5e68b46d6 --- /dev/null +++ b/challenge-144/mark-anderson/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku + +use Prime::Factor; +use Test; + +is-deeply (^100).grep(&semi-prime), ( 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, + 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, + 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, + 91, 93, 94, 95); + +is-deeply (^Inf).hyper.grep(&semi-prime)[1000, 2000, 5000, 9999], + (3599, 7454, 19645, 40882); + +sub semi-prime($n) +{ + my @div = proper-divisors($n, :s).skip; + return False if @div ~~ Empty; + return @div.grep(*.is-prime) == @div; +} diff --git a/challenge-144/mohammad-anwar/perl/ch-1.pl b/challenge-144/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..7a77887d1b --- /dev/null +++ b/challenge-144/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +=head1 + +Week 144: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-144 + +Task #1: Semiprime + + Write a script to generate all Semiprime number <= 100. + +=cut + +use strict; +use warnings; +use Test::More; +use Test::Deep; +use ntheory qw(semi_primes); + +# Sample copied from https://oeis.org/A001358 +my $exp = [ + 4, 6, 9, 10, 14, + 15, 21, 22, 25, 26, + 33, 34, 35, 38, 39, + 46, 49, 51, 55, 57, + 58, 62, 65, 69, 74, + 77, 82, 85, 86, 87, + 91, 93, 94, 95, +]; + +is_deeply(semi_primes(100), $exp, 'Example'); + +done_testing; diff --git a/challenge-144/peter-campbell-smith/blog.txt b/challenge-144/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..41721af4c7 --- /dev/null +++ b/challenge-144/peter-campbell-smith/blog.txt< |
