aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-19 11:12:01 +0000
committerGitHub <noreply@github.com>2023-03-19 11:12:01 +0000
commit44756ff6c2b4e10b9799f482ee31fda95a4c081f (patch)
tree984d3d97bb09839378ee198e462908874b3194d6
parentfaa5a8079d332d30df9d7ce16d1461704dcea6ef (diff)
parent58100399bb455f719a2f13c3394aafb2932c0932 (diff)
downloadperlweeklychallenge-club-44756ff6c2b4e10b9799f482ee31fda95a4c081f.tar.gz
perlweeklychallenge-club-44756ff6c2b4e10b9799f482ee31fda95a4c081f.tar.bz2
perlweeklychallenge-club-44756ff6c2b4e10b9799f482ee31fda95a4c081f.zip
Merge pull request #7751 from simongreen-net/master
Simon's solution to challenge 208
-rw-r--r--challenge-208/sgreen/README.md4
-rw-r--r--challenge-208/sgreen/blog.txt1
-rwxr-xr-xchallenge-208/sgreen/perl/ch-1.pl47
-rwxr-xr-xchallenge-208/sgreen/perl/ch-2.pl31
-rwxr-xr-xchallenge-208/sgreen/python/ch-1.py49
-rwxr-xr-xchallenge-208/sgreen/python/ch-2.py27
6 files changed, 157 insertions, 2 deletions
diff --git a/challenge-208/sgreen/README.md b/challenge-208/sgreen/README.md
index 9a014752a8..dc39259a5d 100644
--- a/challenge-208/sgreen/README.md
+++ b/challenge-208/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 207
+# The Weekly Challenge 208
-Blog: [The H Word](https://dev.to/simongreennet/the-h-word-3neh)
+Blog: [Weekly Challenge 208](https://dev.to/simongreennet/weekly-challenge-208-3i23)
diff --git a/challenge-208/sgreen/blog.txt b/challenge-208/sgreen/blog.txt
new file mode 100644
index 0000000000..f0dccfbb27
--- /dev/null
+++ b/challenge-208/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-208-3i23 \ No newline at end of file
diff --git a/challenge-208/sgreen/perl/ch-1.pl b/challenge-208/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..b40072b9e2
--- /dev/null
+++ b/challenge-208/sgreen/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::MoreUtils 'first_index';
+
+sub main ( $first_list, $second_list ) {
+ # Turn the words into a list
+ my @first_list = ( $first_list =~ /(\w+)/g );
+ my @second_list = ( $second_list =~ /(\w+)/g );
+
+ # We set the index_sum to one more than the greatest possible solution
+ my $index_sum = $#first_list + $#second_list + 3;
+ my @solution = ();
+
+ # Loop through the first_list
+ while ( my ( $i1, $w ) = each @first_list ) {
+ # See if it appears in the second list
+ my $i2 = first_index { $_ eq $w } @second_list;
+ if ( $i2 != -1 ) {
+ # The the index sum of this word
+ my $i = $i1 + $i2;
+ if ( $i < $index_sum ) {
+ # It is better than the previous solution
+ @solution = ($w);
+ $index_sum = $i;
+ }
+ elsif ( $i == $index_sum ) {
+ # It is the same as the previous solution
+ push @solution, $w;
+ }
+ }
+ }
+
+ # Print the results
+ if ( $#solution != -1 ) {
+ say '("', join( '", "', @solution ), '")';
+ }
+ else {
+ say '()';
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-208/sgreen/perl/ch-2.pl b/challenge-208/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..d210f2da9c
--- /dev/null
+++ b/challenge-208/sgreen/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@array) {
+ my $missing = my $duplicate = undef;
+
+ foreach my $i ( 1 .. $#array + 1 ) {
+ # Find out how many times this number appears in the list
+ my $count = scalar( grep { $_ == $i } @array );
+ if ( $count == 0 ) {
+ $missing = $i;
+ }
+ elsif ( $count > 1 ) {
+ $duplicate = $i;
+ }
+ }
+
+ if ( not defined $missing ) {
+ say -1;
+ }
+ else {
+ $duplicate //= '';
+ say "($duplicate, $missing)";
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-208/sgreen/python/ch-1.py b/challenge-208/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..9ab9f318d4
--- /dev/null
+++ b/challenge-208/sgreen/python/ch-1.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def find_index_in_list(array, word):
+ '''Find the index of a word in a list'''
+ for i, w in enumerate(array):
+ if w == word:
+ return i
+
+ # This word does not appear in the list
+ return None
+
+
+def main(first_list, second_list):
+ # Turn the words into a list
+ first_list = re.findall(r'\w+', first_list)
+ second_list = re.findall(r'\w+', second_list)
+
+ # We set the index_sum to one more than the greatest possible solution
+ index_sum = len(first_list) + len(second_list) + 1
+ solution = []
+
+ # Loop through the first_list
+ for i1, w in enumerate(first_list):
+ # See if it appears in the second list
+ i2 = find_index_in_list(second_list, w)
+ if i2 is not None:
+ # The the index sum of this word
+ i = i1 + i2
+ if i < index_sum:
+ # It is better than the previous solution
+ solution = [w]
+ index_sum = i
+ elif i == index_sum:
+ # It is the same as the previous solution
+ solution.append(w)
+
+ # Print the results
+ if solution:
+ print('("' + '", "'.join(solution) + '")')
+ else:
+ print('()')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1], sys.argv[2])
diff --git a/challenge-208/sgreen/python/ch-2.py b/challenge-208/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..84952f60f1
--- /dev/null
+++ b/challenge-208/sgreen/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ missing = None
+ duplicate = None
+
+ for i in range(1, len(array)+1):
+ # Find out how many times this number appears in the list
+ count = sum(1 for n in array if n == i)
+ if count == 0:
+ missing = i
+ elif count > 1:
+ duplicate = i
+
+ if missing is None:
+ print(-1)
+ else:
+ print(f'({duplicate or ""}, {missing})')
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)