aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-17 23:21:07 +0100
committerGitHub <noreply@github.com>2023-09-17 23:21:07 +0100
commit50aeb058ffa98a3291c10c50bd27a3bcaf58e1fe (patch)
treebc0ee0b21b80a020ce797c1203324adb7fdbf70a
parent619c89254f40d5bd0ffe680ddfcd86841460eda6 (diff)
parent63ccde0b593cd04a0eed9642df3e9dd3c1061686 (diff)
downloadperlweeklychallenge-club-50aeb058ffa98a3291c10c50bd27a3bcaf58e1fe.tar.gz
perlweeklychallenge-club-50aeb058ffa98a3291c10c50bd27a3bcaf58e1fe.tar.bz2
perlweeklychallenge-club-50aeb058ffa98a3291c10c50bd27a3bcaf58e1fe.zip
Merge pull request #8714 from simongreen-net/master
Simon's solution to challenge 234
-rw-r--r--challenge-234/sgreen/README.md4
-rwxr-xr-xchallenge-234/sgreen/perl/ch-1.pl26
-rwxr-xr-xchallenge-234/sgreen/perl/ch-2.pl34
-rwxr-xr-xchallenge-234/sgreen/python/ch-1.py17
-rwxr-xr-xchallenge-234/sgreen/python/ch-2.py29
5 files changed, 107 insertions, 3 deletions
diff --git a/challenge-234/sgreen/README.md b/challenge-234/sgreen/README.md
index 4d94e036b1..2444f94ca5 100644
--- a/challenge-234/sgreen/README.md
+++ b/challenge-234/sgreen/README.md
@@ -1,3 +1 @@
-# The Weekly Challenge 233
-
-Blog: [Similar frequency](https://dev.to/simongreennet/similar-frequency-3i6b)
+# The Weekly Challenge 234
diff --git a/challenge-234/sgreen/perl/ch-1.pl b/challenge-234/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..71bb803cb0
--- /dev/null
+++ b/challenge-234/sgreen/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@words) {
+ # Turn the letter of the first word into a hash (key is each letter)
+ my $first_word = shift @words;
+ my %solution = map { $_ => 1 } split //, $first_word;
+
+ foreach my $word (@words) {
+ foreach my $letter ( keys %solution ) {
+ # If the letter is not in the current word, delete it
+ if ( index( $word, $letter ) == -1 ) {
+ delete $solution{$letter};
+ }
+ }
+ }
+
+ print join ', ', sort keys %solution;
+
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-234/sgreen/perl/ch-2.pl b/challenge-234/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..6ad4dfc0d0
--- /dev/null
+++ b/challenge-234/sgreen/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics 'combinations';
+
+sub main (@ints) {
+ # Calculate the frequency of all integers
+ my %freq = ();
+ foreach my $i (@ints) {
+ $freq{$i}++;
+ }
+
+ if ( scalar( keys %freq ) < 3 ) {
+ say 0;
+ return;
+ }
+
+ my $solutions = 0;
+ my $iter = combinations( [ keys %freq ], 3 );
+ while ( my $c = $iter->next ) {
+ my ( $i, $j, $k ) = @$c;
+ # The number of solutions of this combination the product of the frequencies
+ $solutions += $freq{$i} * $freq{$j} * $freq{$k};
+
+ }
+
+ say $solutions;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-234/sgreen/python/ch-1.py b/challenge-234/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..36d5ca9c78
--- /dev/null
+++ b/challenge-234/sgreen/python/ch-1.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(words):
+ # Turn each word into a set
+ sets = [set(w) for w in words]
+ first_set = sets.pop(0)
+
+ # The solution is the intersection of all sets
+ solution = first_set.intersection(*sets)
+ print(*sorted(solution), sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-234/sgreen/python/ch-2.py b/challenge-234/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..2efa21b40e
--- /dev/null
+++ b/challenge-234/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+from itertools import combinations
+
+
+def main(ints):
+ # Calculate the frequency of all integers
+ freq = {}
+ for i in ints:
+ freq[i] = freq.get(i, 0) + 1
+
+ if len(freq) < 3:
+ print('0')
+ return
+
+ solutions = 0
+ for c in combinations(freq, 3):
+ i, j, k = c
+ # The number of solutions of this combination the product of the frequencies
+ solutions += freq[i] * freq[j] * freq[k]
+
+ print(solutions)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)