diff options
| -rw-r--r-- | challenge-202/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-202/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-202/sgreen/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-202/sgreen/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-202/sgreen/python/ch-1.py | 28 | ||||
| -rwxr-xr-x | challenge-202/sgreen/python/ch-2.py | 32 |
6 files changed, 131 insertions, 2 deletions
diff --git a/challenge-202/sgreen/README.md b/challenge-202/sgreen/README.md index e7306a3042..642af7b377 100644 --- a/challenge-202/sgreen/README.md +++ b/challenge-202/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 201 +# The Weekly Challenge 202 -Blog: [Missing pennies](https://dev.to/simongreennet/missing-pennies-3791) +Blog: [Weekly Challenge 202](https://dev.to/simongreennet/weekly-challenge-202-4dcm) diff --git a/challenge-202/sgreen/blog.txt b/challenge-202/sgreen/blog.txt new file mode 100644 index 0000000000..9a8b83d656 --- /dev/null +++ b/challenge-202/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-202-4dcm
\ No newline at end of file diff --git a/challenge-202/sgreen/perl/ch-1.pl b/challenge-202/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..9bd2e4d6cd --- /dev/null +++ b/challenge-202/sgreen/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main(@n) { + # Count the number of sequential odd numbers + my $odds = 0; + + foreach my $i (@n) { + if ($i % 2 == 1) { + $odds++; + + if ($odds == 3) { + # We have found three sequential odd numbers + say '1'; + return; + } + } + else { + # Restart the count + $odds = 0; + } + } + + # There is no solution + say '0'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-202/sgreen/perl/ch-2.pl b/challenge-202/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..3ddb31460e --- /dev/null +++ b/challenge-202/sgreen/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@n) { + # This stores the current widest (leftmost) valley + my @solution = (); + + foreach my $valley ( 0 .. $#n ) { + my $start = my $end = $valley; + + # Get the values to the left that are >= the previous value + while ( $start > 0 and $n[$start] <= $n[ $start - 1 ] ) { + $start--; + } + + # Get the values to the right that are >= the previous value + while ( $end < $#n and $n[$end] <= $n[ $end + 1 ] ) { + $end++; + } + + # If the len of this solution is larger than the previous, it's a + # better solution + if ( scalar(@solution) < $end - $start ) { + @solution = @n[ $start .. $end ]; + } + + } + + say join ', ', @solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-202/sgreen/python/ch-1.py b/challenge-202/sgreen/python/ch-1.py new file mode 100755 index 0000000000..974ab31d78 --- /dev/null +++ b/challenge-202/sgreen/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # Count the number of sequential odd numbers + odds = 0 + + for i in n: + if i % 2 == 1: + odds += 1 + if odds >= 3: + # We have found three sequential odd numbers + print('1') + return + else: + # Restart the count + odds = 0 + + # There is no solution + print('0') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-202/sgreen/python/ch-2.py b/challenge-202/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a17bd47631 --- /dev/null +++ b/challenge-202/sgreen/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # This stores the current widest (leftmost) valley + solution = [] + + for valley in range(len(n)): + start = end = valley + + # Get the values to the left that are >= the previous value + while start > 0 and n[start] <= n[start-1]: + start -= 1 + + # Get the values to the right that are >= the previous value + while end < len(n)-1 and n[end] <= n[end+1]: + end += 1 + + # If the len of this solution is larger than the previous, it's a + # better solution + if len(solution) < end-start: + solution = n[start:end+1] + + print(*solution, sep=', ') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) |
