aboutsummaryrefslogtreecommitdiff
path: root/challenge-198
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-01-05 20:32:18 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-01-05 20:32:18 +0000
commita3955b58c74d643c9cbff8fe2b43e4befb1f32ba (patch)
tree464a1f34687eda78818caad752e555570a1914c1 /challenge-198
parent744d48aaf56cf5749fee4b79970567b191370a11 (diff)
downloadperlweeklychallenge-club-a3955b58c74d643c9cbff8fe2b43e4befb1f32ba.tar.gz
perlweeklychallenge-club-a3955b58c74d643c9cbff8fe2b43e4befb1f32ba.tar.bz2
perlweeklychallenge-club-a3955b58c74d643c9cbff8fe2b43e4befb1f32ba.zip
- Added solutions by Arne Sommer.
- Added blog post by James Smith. - Added solutions by Bob Lied. - Added solutions by Dave Jacoby. - Added solutions by E. Choroba. - Added solutions by Ali Moradi. - Added solutions by Marton Polgar. - Added solutions by Luca Ferrari. - Added solutions by Pip Stuart. - Added solutions by Peter Campbell Smith. - Added solutions by Robbie Hatley. - Added solutions by Stephen G. Lynn. - Added solutions by Rawley Fowler.
Diffstat (limited to 'challenge-198')
-rwxr-xr-xchallenge-198/eric-cheung/python/ch-1.py12
-rwxr-xr-xchallenge-198/eric-cheung/python/ch-2.py11
-rw-r--r--challenge-198/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-198/laurent-rosenfeld/perl/ch-1.pl20
-rw-r--r--challenge-198/laurent-rosenfeld/perl/ch-2.pl18
-rw-r--r--challenge-198/laurent-rosenfeld/raku/ch-1.raku13
-rw-r--r--challenge-198/laurent-rosenfeld/raku/ch-2.raku6
-rw-r--r--challenge-198/rawleyfowler/README.md4
-rw-r--r--challenge-198/rawleyfowler/raku/ch-1.raku (renamed from challenge-198/rawleyfowler/task1.raku)0
-rw-r--r--challenge-198/rawleyfowler/raku/ch-2.raku (renamed from challenge-198/rawleyfowler/task2.raku)0
10 files changed, 83 insertions, 2 deletions
diff --git a/challenge-198/eric-cheung/python/ch-1.py b/challenge-198/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..34d0347180
--- /dev/null
+++ b/challenge-198/eric-cheung/python/ch-1.py
@@ -0,0 +1,12 @@
+
+arrInputList = [2, 5, 8, 1] ## Example 1
+## arrInputList = [3] ## Example 2
+
+arrInputList.sort()
+arrDiff = [arrInputList[nIndx + 1] - arrInputList[nIndx] for nIndx in range(0, len(arrInputList) - 1)]
+
+if len(arrDiff) > 0:
+ nMaxArrDiff = max(arrDiff)
+ print (arrDiff.count(nMaxArrDiff))
+else:
+ print (0)
diff --git a/challenge-198/eric-cheung/python/ch-2.py b/challenge-198/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..315da4e457
--- /dev/null
+++ b/challenge-198/eric-cheung/python/ch-2.py
@@ -0,0 +1,11 @@
+
+from sympy import isprime
+
+## nInput = 10 ## Example 1
+## nInput = 15 ## Example 2
+## nInput = 1 ## Example 3
+nInput = 25 ## Example 4
+
+arrPrime = [nLoop for nLoop in range(2, nInput) if isprime(nLoop)]
+
+print (len(arrPrime))
diff --git a/challenge-198/laurent-rosenfeld/blog.txt b/challenge-198/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..792185b39e
--- /dev/null
+++ b/challenge-198/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2023/01/perl-weekly-challenge-198-max-gap-and-prime-count.html
diff --git a/challenge-198/laurent-rosenfeld/perl/ch-1.pl b/challenge-198/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..b99e96ef36
--- /dev/null
+++ b/challenge-198/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub max_gap {
+ return 0 if scalar @_ < 2;
+ my @sorted = sort { $a <=> $b } @_;
+ my %gaps;
+ for my $i (1..$#sorted) {
+ push @{$gaps{$sorted[$i] - $sorted[$i-1]}}, $i;
+ }
+ my $max_gap = 0;
+ for my $k (keys %gaps) {
+ $max_gap = $k if $k > $max_gap;
+ }
+ return scalar @{$gaps{$max_gap}};
+}
+for my $test ([<2 5 8 1>], [<2 7>], [3,], [<12 2 6 5 15 9>]) {
+ printf "%-20s => %d\n", "@$test", max_gap @$test;
+}
diff --git a/challenge-198/laurent-rosenfeld/perl/ch-2.pl b/challenge-198/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..d0903cf706
--- /dev/null
+++ b/challenge-198/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub is_prime {
+ my $num = shift;
+ for my $i (2 .. $num ** .5) {
+ return 0 if $num % $i == 0;
+ }
+ return 1;
+}
+sub count_primes {
+ my $n = shift;
+ return scalar grep is_prime($_), 2..$n;
+}
+for my $i (<10 15 1 25>) {
+ say "$i \t => ", count_primes $i;
+}
diff --git a/challenge-198/laurent-rosenfeld/raku/ch-1.raku b/challenge-198/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..c7a6fef8e5
--- /dev/null
+++ b/challenge-198/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,13 @@
+sub max-gap (@in) {
+ return 0 if @in.elems < 2;
+ my @sorted = sort @in;
+ my %gaps;
+ for 1..@sorted.end -> $i {
+ push %gaps, ( @sorted[$i] - @sorted[$i-1] => $i );
+ }
+ my $max-gap = %gaps.keys.max;
+ return %gaps{$max-gap}.elems;
+}
+for <2 5 8 1>, <2 7>, (3,), <12 2 6 5 15 9> -> @test {
+ say (~@test).fmt("%-20s => "), max-gap @test;
+}
diff --git a/challenge-198/laurent-rosenfeld/raku/ch-2.raku b/challenge-198/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..91128040f7
--- /dev/null
+++ b/challenge-198/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,6 @@
+sub count-primes (Int $n) {
+ return (grep ({.is-prime}), 1..$n).elems;
+}
+for <10 15 1 25> -> $i {
+ say "$i \t => ", count-primes $i;
+}
diff --git a/challenge-198/rawleyfowler/README.md b/challenge-198/rawleyfowler/README.md
index f55ad6c829..4d4b3f88f8 100644
--- a/challenge-198/rawleyfowler/README.md
+++ b/challenge-198/rawleyfowler/README.md
@@ -1,5 +1,5 @@
## How to run
```bash
-raku task1.raku '2 5 8 1' # Find gap pairs
-raku task2.raku '100' # Find primes
+raku raku/ch-1.raku '2 5 8 1' # Find gap pairs
+raku raku/ch-2.raku '100' # Find primes
```
diff --git a/challenge-198/rawleyfowler/task1.raku b/challenge-198/rawleyfowler/raku/ch-1.raku
index e3f2468cec..e3f2468cec 100644
--- a/challenge-198/rawleyfowler/task1.raku
+++ b/challenge-198/rawleyfowler/raku/ch-1.raku
diff --git a/challenge-198/rawleyfowler/task2.raku b/challenge-198/rawleyfowler/raku/ch-2.raku
index b7116610e4..b7116610e4 100644
--- a/challenge-198/rawleyfowler/task2.raku
+++ b/challenge-198/rawleyfowler/raku/ch-2.raku