aboutsummaryrefslogtreecommitdiff
path: root/challenge-220
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2023-06-11 23:54:17 +1000
committerSimon Green <mail@simon.green>2023-06-11 23:54:17 +1000
commite06fd8b2b4a0130a13c20f075843f751c1396a20 (patch)
treef32dc079d649ae647705d63f1dae133ab2eee00f /challenge-220
parent401be1861472af6d62bbdeb0fe65f6ced1ca8f31 (diff)
downloadperlweeklychallenge-club-e06fd8b2b4a0130a13c20f075843f751c1396a20.tar.gz
perlweeklychallenge-club-e06fd8b2b4a0130a13c20f075843f751c1396a20.tar.bz2
perlweeklychallenge-club-e06fd8b2b4a0130a13c20f075843f751c1396a20.zip
Simon's solution to challenge 220
Diffstat (limited to 'challenge-220')
-rw-r--r--challenge-220/sgreen/README.md2
-rw-r--r--challenge-220/sgreen/blog.txt1
-rwxr-xr-xchallenge-220/sgreen/perl/ch-1.pl40
-rwxr-xr-xchallenge-220/sgreen/perl/ch-2.pl36
-rwxr-xr-xchallenge-220/sgreen/python/ch-1.py24
-rwxr-xr-xchallenge-220/sgreen/python/ch-2.py29
6 files changed, 131 insertions, 1 deletions
diff --git a/challenge-220/sgreen/README.md b/challenge-220/sgreen/README.md
index b0c3afab92..4166d8271b 100644
--- a/challenge-220/sgreen/README.md
+++ b/challenge-220/sgreen/README.md
@@ -1,3 +1,3 @@
# The Weekly Challenge 219
-Blog: [The cheapest way to the square](https://dev.to/simongreennet/the-cheapest-way-to-the-square-1mhb)
+Blog: [Common squares](https://dev.to/simongreennet/common-squares-31do)
diff --git a/challenge-220/sgreen/blog.txt b/challenge-220/sgreen/blog.txt
new file mode 100644
index 0000000000..573b53471f
--- /dev/null
+++ b/challenge-220/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/common-squares-31do \ No newline at end of file
diff --git a/challenge-220/sgreen/perl/ch-1.pl b/challenge-220/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..eb4113d626
--- /dev/null
+++ b/challenge-220/sgreen/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw(all);
+
+sub word_to_hash ($word) {
+ my %hash = ();
+ foreach my $letter ( split //, lc $word ) {
+ $hash{$letter} = 1;
+ }
+ return \%hash;
+}
+
+sub main (@words) {
+ # Turns the words into a list of hashes, and take off the first word
+ my @set_list = ( map { word_to_hash($_) } @words );
+ my $first_word = shift(@set_list);
+ my @letters = ();
+
+ # Go through each letter in the first word
+ foreach my $letter ( sort keys(%$first_word) ) {
+ # And checks it is used in all other words
+ if ( 'a' le $letter
+ and $letter le 'z'
+ and all { exists $_->{$letter} } @set_list )
+ {
+ # It is! Add it to the letters list
+ push @letters, $letter;
+ }
+ }
+
+ say join( ', ', @letters );
+
+}
+
+main(@ARGV);
diff --git a/challenge-220/sgreen/perl/ch-2.pl b/challenge-220/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..b0af768b95
--- /dev/null
+++ b/challenge-220/sgreen/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics qw(permutations);
+use List::Util qw(any);
+
+sub main (@ints) {
+ my @solution = ();
+
+ my $iter = permutations( \@ints );
+ P: while ( my $p = $iter->next ) {
+ # Have we seen this solution before
+ my $string = '(' . join( ', ', @$p ) . ')';
+ next if any { $_ eq $string } @solution;
+
+ # Is each pair a square of an integer
+ foreach my $i ( 1 .. $#ints ) {
+ my $sqrt = sqrt( $p->[ $i - 1 ] + $p->[$i] );
+ if ( $sqrt != int($sqrt) ) {
+ next P;
+ }
+ }
+
+ # It is. Add a formatted version of these items to the solution array.
+ push @solution, $string;
+ }
+
+ say join( ', ', @solution );
+
+}
+
+main(@ARGV);
diff --git a/challenge-220/sgreen/python/ch-1.py b/challenge-220/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..60df7c4cff
--- /dev/null
+++ b/challenge-220/sgreen/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+import string
+import sys
+
+
+def main(words):
+ # Turns the words into a set list, and take off the first word
+ set_list = [set(w.lower()) for w in words]
+ first_word = set_list.pop(0)
+ letters = []
+
+ # Go through each letter in the first word
+ for letter in sorted(first_word):
+ # And checks it is used in all other words
+ if 'a' < letter < 'z' and all(letter in x for x in set_list):
+ # It is! Add it to the letters list
+ letters.append(letter)
+
+ print(*letters, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-220/sgreen/python/ch-2.py b/challenge-220/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d483abe7cf
--- /dev/null
+++ b/challenge-220/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import math
+import sys
+from itertools import permutations
+
+
+def main(ints):
+ solution = []
+ for p in permutations(ints):
+ # Have we seen this solution before
+ if p in solution:
+ continue
+
+ # Is each pair a square of an integer
+ for i in range(1, len(ints)):
+ sqrt = math.sqrt(p[i-1] + p[i])
+ if sqrt != int(sqrt):
+ break
+ else:
+ # It is. Add it the solution if we haven't seen it before
+ solution.append(p)
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)