diff options
| author | Simon Green <mail@simon.green> | 2023-04-16 23:03:51 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2023-04-16 23:03:51 +1000 |
| commit | 2a42af32e400849b88d49d4851183ffdb503141e (patch) | |
| tree | e04a19f6a4b9149c9ab24ea0efcf1ac7bee14549 /challenge-212 | |
| parent | 6d9f9d89ba086af0198b7d335c046e4096d38fde (diff) | |
| download | perlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.tar.gz perlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.tar.bz2 perlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.zip | |
Simon's solution to challenge 212
Diffstat (limited to 'challenge-212')
| -rw-r--r-- | challenge-212/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-212/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-212/sgreen/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-212/sgreen/python/ch-2.py | 41 |
4 files changed, 89 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-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) |
