From 6d9f9d89ba086af0198b7d335c046e4096d38fde Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 10 Apr 2023 20:17:58 +1000 Subject: Simon's solution to task 1, challenge 212 --- challenge-212/sgreen/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++++++++ challenge-212/sgreen/python/ch-1.py | 27 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 challenge-212/sgreen/perl/ch-1.pl create mode 100755 challenge-212/sgreen/python/ch-1.py 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/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:]) -- cgit From 2a42af32e400849b88d49d4851183ffdb503141e Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 16 Apr 2023 23:03:51 +1000 Subject: Simon's solution to challenge 212 --- challenge-212/sgreen/README.md | 4 ++-- challenge-212/sgreen/blog.txt | 1 + challenge-212/sgreen/perl/ch-2.pl | 45 +++++++++++++++++++++++++++++++++++++ challenge-212/sgreen/python/ch-2.py | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 challenge-212/sgreen/blog.txt create mode 100755 challenge-212/sgreen/perl/ch-2.pl create mode 100755 challenge-212/sgreen/python/ch-2.py 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-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-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) -- cgit