aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-214/sgreen/README.md4
-rwxr-xr-xchallenge-214/sgreen/perl/ch-1.pl27
-rwxr-xr-xchallenge-214/sgreen/perl/ch-2.pl60
-rwxr-xr-xchallenge-214/sgreen/python/ch-1.py24
-rwxr-xr-xchallenge-214/sgreen/python/ch-2.py50
-rw-r--r--challenge-215/sgreen/README.md4
-rw-r--r--challenge-215/sgreen/blog.txt1
-rwxr-xr-xchallenge-215/sgreen/perl/ch-1.pl34
-rwxr-xr-xchallenge-215/sgreen/perl/ch-2.pl32
-rwxr-xr-xchallenge-215/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-215/sgreen/python/ch-2.py27
11 files changed, 285 insertions, 5 deletions
diff --git a/challenge-214/sgreen/README.md b/challenge-214/sgreen/README.md
index 78dadfd6ee..ca70b10319 100644
--- a/challenge-214/sgreen/README.md
+++ b/challenge-214/sgreen/README.md
@@ -1,3 +1 @@
-# The Weekly Challenge 213
-
-Blog: [Sorting Routes](https://dev.to/simongreennet/sorting-routes-595b)
+# The Weekly Challenge 214
diff --git a/challenge-214/sgreen/perl/ch-1.pl b/challenge-214/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..d68b17c47c
--- /dev/null
+++ b/challenge-214/sgreen/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main(@array) {
+ my %score_to_place = ();
+ my @sorted = sort { $b <=> $a } @array;
+ while(my ($i, $score) = each(@sorted)) {
+ if (not exists $score_to_place{$score}) {
+ $score_to_place{$score} = $i+1;
+ }
+ }
+
+ my %place_to_word = ( 1 => 'G', 2 => 'S', 3 => 'B' );
+ my @solution = ();
+ foreach my $score (@array) {
+ my $place = $score_to_place{$score};
+ push @solution, $place_to_word{$place} || $place;
+ }
+
+ say join ', ', @solution;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-214/sgreen/perl/ch-2.pl b/challenge-214/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..1049d060f7
--- /dev/null
+++ b/challenge-214/sgreen/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub shrink_list(@array) {
+ # Shrink the list if it has consecutive numbers
+ my @new_array = [];
+ foreach my $i (@array) {
+ my ($number, $count) = @$i;
+ if ($#new_array != -1 and $new_array[-1][0] == $number) {
+ $new_array[-1] = [$number, $count + $new_array[-1][1]];
+ }
+ else {
+ push @new_array, [$number, $count];
+ }
+ }
+
+ return @new_array;
+}
+
+sub score_array(@array) {
+ # Recursive function to get the highest score
+
+ # Shrink the list if it has consecutive numbers
+ @array = shrink_list(@array);
+
+ if ($#array == 1) {
+ # There is only one remaining number
+ return $array[0][1] ** 2;
+ }
+
+ my $max_score = 0;
+
+ while(my($i, $tup )= each @array) {
+ # Create a copy of the array without the chosen element
+ my @new_array = @array;
+ splice(@new_array, $i, 1);
+
+ # Calculate the maximum possible score recursively
+ my $score = $tup->[1] ** 2 + score_array(@new_array);
+ if ($score > $max_score) {
+ $max_score = $score;
+ }
+ }
+
+ # Return the maximum score
+ return $max_score;
+}
+
+sub main(@array) {
+ # Turn the list in a list of tuples of the number and the occurrences of it
+ @array = map { [$_, 1] } @array;
+ my $score = score_array(@array);
+ say $score;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-214/sgreen/python/ch-1.py b/challenge-214/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..35bd25744a
--- /dev/null
+++ b/challenge-214/sgreen/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(array):
+ score_to_place = {}
+ for i, score in enumerate(sorted(array, reverse=True)):
+ if score not in score_to_place:
+ score_to_place[score] = i+1
+
+ place_to_word = {1: 'G', 2: 'S', 3: 'B'}
+ solution = []
+ for i in array:
+ place = score_to_place[i]
+ solution.append(place_to_word.get(place, place))
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)
diff --git a/challenge-214/sgreen/python/ch-2.py b/challenge-214/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..eeeb695be2
--- /dev/null
+++ b/challenge-214/sgreen/python/ch-2.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+import sys
+
+def shrink_list(array):
+ '''Shrink the list if it has consecutive numbers'''
+ new_array = []
+ for i in array:
+ number, count = i
+ if len(new_array) and new_array[-1][0] == number:
+ new_array[-1] = (number, count + new_array[-1][1])
+ else:
+ new_array.append((number, count))
+
+ return new_array
+
+def score_array(array):
+ '''Recursive function to get the highest score'''
+ # Shrink the list if it has consecutive numbers
+ array = shrink_list(array)
+
+ if len(array) == 1:
+ # There is only one remaining number
+ return array[0][1] ** 2
+
+ max_score = 0
+
+ for i, tup in enumerate(array):
+ # Create a copy of the array without the chosen element
+ new_array = array.copy()
+ del new_array[i]
+
+ # Calculate the maximum possible score recursively
+ score = tup[1] ** 2 + score_array(new_array)
+ if score > max_score:
+ max_score = score
+
+ # Return the maximum score
+ return max_score
+
+def main(array):
+ # Turn the list in a list of tuples of the number and the occurrences of it
+ array = [ (x, 1) for x in array]
+ score = score_array(array)
+ print(score)
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)
diff --git a/challenge-215/sgreen/README.md b/challenge-215/sgreen/README.md
index 78dadfd6ee..fe4eee3265 100644
--- a/challenge-215/sgreen/README.md
+++ b/challenge-215/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 213
+# The Weekly Challenge 215
-Blog: [Sorting Routes](https://dev.to/simongreennet/sorting-routes-595b)
+Blog: [Weekly Challenge 215](https://dev.to/simongreennet/weekly-challenge-215-k0b)
diff --git a/challenge-215/sgreen/blog.txt b/challenge-215/sgreen/blog.txt
new file mode 100644
index 0000000000..f60644dc60
--- /dev/null
+++ b/challenge-215/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-215-k0b \ No newline at end of file
diff --git a/challenge-215/sgreen/perl/ch-1.pl b/challenge-215/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..83e082ace9
--- /dev/null
+++ b/challenge-215/sgreen/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub main (@words) {
+ my $current_word = shift @words;
+ my $unsorted_words = 0;
+
+ if ( any { $_ lt $current_word } @words ) {
+ # If the first word is not the smallest, the whole list is unsorted
+ say $#words + 2;
+ return;
+ }
+
+ foreach my $word (@words) {
+ if ( $word ge $current_word ) {
+ # The next word is in order
+ $current_word = $word;
+ }
+ else {
+ # The word is less than the current word
+ $unsorted_words++;
+ }
+ }
+
+ say $unsorted_words;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-215/sgreen/perl/ch-2.pl b/challenge-215/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..4edeb09b21
--- /dev/null
+++ b/challenge-215/sgreen/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@array) {
+ my $to_place = pop(@array);
+
+ foreach my $i ( 1 .. $#array - 1 ) {
+ # Are the number before the current one, the current one and the
+ # next one all zero?
+ if ( $array[ $i - 1 ] == 0
+ and $array[$i] == 0
+ and $array[ $i + 1 ] == 0 )
+ {
+ # If so, we can put a one here
+ $array[$i] = 1;
+ if ( --$to_place == 0 ) {
+ # We have placed all the ones
+ say 1;
+ return;
+ }
+ }
+ }
+
+ # There is no possible solution
+ say 0;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-215/sgreen/python/ch-1.py b/challenge-215/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..b10b6e1547
--- /dev/null
+++ b/challenge-215/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(words):
+ current_word = words.pop(0)
+ unsorted_words = 0
+
+ if any(word < current_word for word in words):
+ # If the first word is not the smallest, the whole list is unsorted
+ print(len(words)+1)
+ return
+
+ for word in words:
+ if word >= current_word:
+ # The next word is in order
+ current_word = word
+ else:
+ # The word is less than the current word
+ unsorted_words += 1
+
+ print(unsorted_words)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-215/sgreen/python/ch-2.py b/challenge-215/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9fc8a9a7e9
--- /dev/null
+++ b/challenge-215/sgreen/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(array):
+ to_place = array.pop()
+
+ for i in range(1, len(array)-1):
+ # Are the number before the current one, the current one and the next one all zero?
+ if array[i-1] == 0 and array[i] == 0 and array[i+1] == 0:
+ # If so, we can put a one here
+ array[i] = 1
+ to_place -= 1
+ if to_place == 0:
+ # We have placed all the ones
+ print(1)
+ return
+
+ # There is no possible solution
+ print(0)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)