aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-14 22:41:34 +0000
committerGitHub <noreply@github.com>2023-01-14 22:41:34 +0000
commit9872fead866197d3d44215ce648caa16a12ca6b0 (patch)
treeb3e3b91042146f1035b1e892739a4531a8bf9d20
parent70a1571ce4c48f53a1eebc84eddb5c035f88b987 (diff)
parentfd7d80667121d4f63bef3c312246df63c4d5fd2b (diff)
downloadperlweeklychallenge-club-9872fead866197d3d44215ce648caa16a12ca6b0.tar.gz
perlweeklychallenge-club-9872fead866197d3d44215ce648caa16a12ca6b0.tar.bz2
perlweeklychallenge-club-9872fead866197d3d44215ce648caa16a12ca6b0.zip
Merge pull request #7405 from simongreen-net/master
Simon's solution to challenge 199
-rw-r--r--challenge-199/sgreen/README.md4
-rw-r--r--challenge-199/sgreen/blog.txt1
-rwxr-xr-xchallenge-199/sgreen/perl/ch-1.pl28
-rwxr-xr-xchallenge-199/sgreen/perl/ch-2.pl35
-rwxr-xr-xchallenge-199/sgreen/python/ch-1.py24
-rwxr-xr-xchallenge-199/sgreen/python/ch-2.py30
6 files changed, 120 insertions, 2 deletions
diff --git a/challenge-199/sgreen/README.md b/challenge-199/sgreen/README.md
index 5d5a8941b6..4708657f42 100644
--- a/challenge-199/sgreen/README.md
+++ b/challenge-199/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 198
+# The Weekly Challenge 199
-[Blog](https://dev.to/simongreennet/weekly-challenge-198-2jbl)
+Blog: [It's all good](https://dev.to/simongreennet/its-all-good-foe)
diff --git a/challenge-199/sgreen/blog.txt b/challenge-199/sgreen/blog.txt
new file mode 100644
index 0000000000..35487ed63c
--- /dev/null
+++ b/challenge-199/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/its-all-good-foe \ No newline at end of file
diff --git a/challenge-199/sgreen/perl/ch-1.pl b/challenge-199/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..8e6481f644
--- /dev/null
+++ b/challenge-199/sgreen/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@list) {
+ # Calculate the frequency of each integer
+ my %freq = ();
+ foreach my $i (@list) {
+ $freq{$i}++;
+ }
+
+ my $solution = 0;
+ foreach my $f ( values(%freq) ) {
+ # If a value appears more than once, calculate the number of
+ # combinations. This is the sum of 1 + ... + f-1.
+ if ( $f > 1 ) {
+ $solution += $f * ( $f - 1 ) / 2;
+ }
+ }
+
+ # Display the output
+ say $solution;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-199/sgreen/perl/ch-2.pl b/challenge-199/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..f2d4f43c9e
--- /dev/null
+++ b/challenge-199/sgreen/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics 'combinations';
+
+sub main (@array) {
+ # Get the x, y, and z values from the input
+ my ( $x, $y, $z ) = splice( @array, -3 );
+
+ # The solution is the number of good triplets.
+ my $count = 0;
+
+ # Work through all combinations of positions
+ my $iter = combinations( [ 0 .. $#array ], 3 );
+ while ( my $x = $iter->next ) {
+ my ( $i, $j, $k ) = sort { $a <=> $b } @$x;
+
+ # If we match the criteria, add one to the count
+ if ( abs( $array[$i] - $array[$j] ) <= $x
+ and abs( $array[$j] - $array[$k] ) <= $y
+ and abs( $array[$i] - $array[$k] ) <= $z )
+ {
+ $count++;
+ }
+ }
+
+ # Display the output
+ say $count;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-199/sgreen/python/ch-1.py b/challenge-199/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..aebf116a45
--- /dev/null
+++ b/challenge-199/sgreen/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(n):
+ # Calculate the frequency of each 'integer'
+ freq = {}
+ for i in n:
+ freq[i] = freq.get(i, 0)+1
+
+ solution = 0
+ for f in freq.values():
+ # If a value appears more than once, calculate the number of
+ # combinations. This is the sum of 1 + ... + f-1.
+ if f > 1:
+ solution += f * (f-1)//2
+
+ # Display the output
+ print(solution)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-199/sgreen/python/ch-2.py b/challenge-199/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9536ef0e8b
--- /dev/null
+++ b/challenge-199/sgreen/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+from itertools import combinations
+
+
+def main(array):
+ # Get the x, y, and z values from the input
+ *array, x, y, z = array
+
+ # The solution is the number of good triplets.
+ count = 0
+
+ # Work through all combinations of positions
+ for c in combinations(range(len(array)), 3):
+ i, j, k = sorted(c)
+ # If we match the criteria, add one to the count
+ if abs(array[i] - array[j]) <= x and \
+ abs(array[j] - array[k]) <= y and \
+ abs(array[i] - array[k]) <= z:
+ count += 1
+
+ # Display the output
+ print(count)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)