diff options
| -rw-r--r-- | challenge-304/sgreen/README.md | 3 | ||||
| -rw-r--r-- | challenge-305/sgreen/README.md | 3 | ||||
| -rw-r--r-- | challenge-306/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-306/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-306/sgreen/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-306/sgreen/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-306/sgreen/python/ch-1.py | 28 | ||||
| -rwxr-xr-x | challenge-306/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-306/sgreen/python/test.py | 19 |
9 files changed, 130 insertions, 8 deletions
diff --git a/challenge-304/sgreen/README.md b/challenge-304/sgreen/README.md deleted file mode 100644 index 4bb689be34..0000000000 --- a/challenge-304/sgreen/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# The Weekly Challenge 303 - -Blog: [Earn with 3 digits](https://dev.to/simongreennet/earn-with-3-digits-45b6) diff --git a/challenge-305/sgreen/README.md b/challenge-305/sgreen/README.md deleted file mode 100644 index 4bb689be34..0000000000 --- a/challenge-305/sgreen/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# The Weekly Challenge 303 - -Blog: [Earn with 3 digits](https://dev.to/simongreennet/earn-with-3-digits-45b6) diff --git a/challenge-306/sgreen/README.md b/challenge-306/sgreen/README.md index 4bb689be34..c4195b4620 100644 --- a/challenge-306/sgreen/README.md +++ b/challenge-306/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 303 +# The Weekly Challenge 306 -Blog: [Earn with 3 digits](https://dev.to/simongreennet/earn-with-3-digits-45b6) +Blog: [The last odd](https://dev.to/simongreennet/the-last-odd-kib) diff --git a/challenge-306/sgreen/blog.txt b/challenge-306/sgreen/blog.txt new file mode 100644 index 0000000000..dbd3d73b2f --- /dev/null +++ b/challenge-306/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-last-odd-kib
\ No newline at end of file diff --git a/challenge-306/sgreen/perl/ch-1.pl b/challenge-306/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..b90109c70f --- /dev/null +++ b/challenge-306/sgreen/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'sum'; + +sub main (@ints) { + my $solution = 0; + + my $l = scalar(@ints); + + # The odd-length length of sub array + for ( my $length = 1 ; $length <= $l ; $length += 2 ) { + # The starting position of this sub array + foreach my $start ( 0 .. $l - $length ) { + $solution += sum( @ints[ $start .. $start + $length - 1 ] ); + } + } + + say $solution; +} + +main(@ARGV); diff --git a/challenge-306/sgreen/perl/ch-2.pl b/challenge-306/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1f09eea6b2 --- /dev/null +++ b/challenge-306/sgreen/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + while ( $#ints > 0 ) { + # Sort the list, and remove the last two elements + @ints = sort { $a <=> $b } @ints; + my $x = pop(@ints); + my $y = pop(@ints); + + if ( $x != $y ) { + # If the last two elements, append the difference to the ints + # array. We know that x is greater than y, always. + push @ints, $x - $y; + } + } + + say $#ints == 0 ? $ints[0] : 0; +} + +main(@ARGV); diff --git a/challenge-306/sgreen/python/ch-1.py b/challenge-306/sgreen/python/ch-1.py new file mode 100755 index 0000000000..d4c66cca35 --- /dev/null +++ b/challenge-306/sgreen/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys + + +def odd_sum(ints: list) -> int: + solution = 0 + + l = len(ints) + + # The odd-length length of sub array + for length in range(1, l+1, 2): + # The starting position of this sub array + for start in range(0, l-length+1): + solution += sum(ints[start:start+length]) + + return solution + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = odd_sum(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-306/sgreen/python/ch-2.py b/challenge-306/sgreen/python/ch-2.py new file mode 100755 index 0000000000..9a62b386f6 --- /dev/null +++ b/challenge-306/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def last_element(ints: list) -> int: + while len(ints) > 1: + # Sort the list, and remove the last two elements + ints.sort() + x = ints.pop() + y = ints.pop() + + if x != y: + # If the last two elements, append the difference to the ints + # array. We know that x is greater than y, always. + ints.append(x-y) + + return ints[0] if ints else 0 + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = last_element(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-306/sgreen/python/test.py b/challenge-306/sgreen/python/test.py new file mode 100755 index 0000000000..f696c34a47 --- /dev/null +++ b/challenge-306/sgreen/python/test.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.odd_sum([2, 5, 3, 6, 4]), 77) + self.assertEqual(ch_1.odd_sum([1, 3]), 4) + + def test_ch_2(self): + self.assertEqual(ch_2.last_element([3, 8, 5, 2, 9, 2]), 1) + self.assertEqual(ch_2.last_element([3, 2, 5]), 0) + + +if __name__ == '__main__': + unittest.main() |
