aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-16 22:58:37 +0100
committerGitHub <noreply@github.com>2023-04-16 22:58:37 +0100
commit0b713469c9dcdff396d2bda9db0be6f3c4284ef8 (patch)
treedbda2220eec14514e6ba80220a4f401ebe089f5f
parent9321c4c03ec737f1116edfb4cc91aa2b96a3648e (diff)
parent2a42af32e400849b88d49d4851183ffdb503141e (diff)
downloadperlweeklychallenge-club-0b713469c9dcdff396d2bda9db0be6f3c4284ef8.tar.gz
perlweeklychallenge-club-0b713469c9dcdff396d2bda9db0be6f3c4284ef8.tar.bz2
perlweeklychallenge-club-0b713469c9dcdff396d2bda9db0be6f3c4284ef8.zip
Merge pull request #7909 from simongreen-net/master
Simon's solution to challenge 212
-rw-r--r--challenge-212/sgreen/README.md4
-rw-r--r--challenge-212/sgreen/blog.txt1
-rwxr-xr-xchallenge-212/sgreen/perl/ch-1.pl36
-rwxr-xr-xchallenge-212/sgreen/perl/ch-2.pl45
-rwxr-xr-xchallenge-212/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-212/sgreen/python/ch-2.py41
6 files changed, 152 insertions, 2 deletions
diff --git a/challenge-212/sgreen/README.md b/challenge-212/sgreen/README.md
index 1c9faf1810..4b45deae05 100644
--- a/challenge-212/sgreen/README.md
+++ b/challenge-212/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 211
+# The Weekly Challenge 212
-Blog: [Weekly Challenge 211](https://dev.to/simongreennet/weekly-challenge-211-1np1)
+Blog: [Jumping Groups](https://dev.to/simongreennet/jumping-groups-2ld2)
diff --git a/challenge-212/sgreen/blog.txt b/challenge-212/sgreen/blog.txt
new file mode 100644
index 0000000000..2924ddef44
--- /dev/null
+++ b/challenge-212/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/jumping-groups-2ld2 \ No newline at end of file
diff --git a/challenge-212/sgreen/perl/ch-1.pl b/challenge-212/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..c6f03e6ac4
--- /dev/null
+++ b/challenge-212/sgreen/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+use List::MoreUtils 'first_index';
+
+sub main (@array) {
+ my @original_word = split //, shift(@array);
+ my $lower_alphabet = [ 'a' .. 'z' ];
+ my $upper_alphabet = [ 'A' .. 'Z' ];
+ my $new_word = '';
+
+ while ( my ( $i, $original_letter ) = each(@original_word) ) {
+ # Which alphabet?
+ my $alphabet =
+ ( any { $_ eq $original_letter } @$lower_alphabet )
+ ? $lower_alphabet
+ : $upper_alphabet;
+
+ # Calculate position of new letter
+ my $pos =
+ ( ( first_index { $_ eq $original_letter } @$alphabet ) + $array[$i] )
+ % 26;
+
+ # ... and add it to the string
+ $new_word .= $alphabet->[$pos];
+ }
+
+ say $new_word;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-212/sgreen/perl/ch-2.pl b/challenge-212/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..b9e37110f1
--- /dev/null
+++ b/challenge-212/sgreen/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@array) {
+ # Take off the last value, and sort the remaining numbers
+ my $n = pop(@array);
+ @array = sort { $a <=> $b } @array;
+ my @solution = ();
+
+ while ( scalar @array ) {
+ # Get the smallest number, and create a set with expected numbers
+ my @expected = ( $array[0] .. $array[0] + $n - 1 );
+ my %not_matched = map { $_ => 1 } @expected;
+
+ # Create an new array less the first instance of each number in the hash
+ my @new_array = ();
+ foreach my $num (@array) {
+ if ( exists $not_matched{$num} ) {
+ delete $not_matched{$num};
+ }
+ else {
+ push @new_array, $num;
+ }
+ }
+
+ if ( scalar keys(%not_matched) ) {
+ # We don't have the expected numbers
+ say -1;
+ return;
+ }
+
+ # We have a solution
+ push @solution, '(' . join( ',', @expected ) . ')';
+ @array = @new_array;
+ }
+
+ # Print the solution
+ say join( ', ', @solution );
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-212/sgreen/python/ch-1.py b/challenge-212/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..aac0116d02
--- /dev/null
+++ b/challenge-212/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+import string
+
+
+def main(array):
+ original_word = array.pop(0)
+ lower_alphabet = list(string.ascii_lowercase)
+ upper_alphabet = list(string.ascii_uppercase)
+ new_word = ''
+
+ for i, original_letter in enumerate(original_word):
+ # Which alphabet?
+ alphabet = lower_alphabet if original_letter in lower_alphabet else upper_alphabet
+
+ # Calculate position of new letter
+ pos = (alphabet.index(original_letter) + int(array[i])) % 26
+
+ # ... and add it to the string
+ new_word += alphabet[pos]
+
+ print(new_word)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-212/sgreen/python/ch-2.py b/challenge-212/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9aeb60e079
--- /dev/null
+++ b/challenge-212/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ # Take off the last value, and sort the remaining numbers
+ n = array.pop()
+ array = sorted(array)
+ solution = []
+
+ while array:
+ # Get the smallest number, and create a set with expected numbers
+ expected = range(array[0], array[0]+n)
+ not_matched = set(expected)
+
+ # Create an new array less the first instance of each number in the set
+ new_array = []
+ for num in array:
+ if num in not_matched:
+ not_matched.remove(num)
+ else:
+ new_array.append(num)
+
+ if not_matched:
+ # We don't have the expected numbers
+ print('-1')
+ return
+
+ # We have a solution
+ solution.append('(' + ','.join([str(x) for x in expected]) + ')')
+ array = new_array
+
+ # Print the solution
+ print(', '.join(solution))
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)