From 8bdcc1eac06889402d8b9beb3f1e7c467df9a4ff Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 2 Jan 2022 20:58:06 +1100 Subject: sgreen solutions to challenge 145 --- challenge-145/sgreen/README.md | 5 ++--- challenge-145/sgreen/blog.txt | 1 + challenge-145/sgreen/perl/ch-1.pl | 27 +++++++++++++++++++++++++++ challenge-145/sgreen/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ challenge-145/sgreen/python/ch-1.py | 28 ++++++++++++++++++++++++++++ challenge-145/sgreen/python/ch-2.py | 27 +++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 challenge-145/sgreen/blog.txt create mode 100755 challenge-145/sgreen/perl/ch-1.pl create mode 100755 challenge-145/sgreen/perl/ch-2.pl create mode 100755 challenge-145/sgreen/python/ch-1.py create mode 100755 challenge-145/sgreen/python/ch-2.py diff --git a/challenge-145/sgreen/README.md b/challenge-145/sgreen/README.md index 85b71b378f..cf23ccf502 100644 --- a/challenge-145/sgreen/README.md +++ b/challenge-145/sgreen/README.md @@ -1,4 +1,3 @@ -# The Weekly Challenge 144 +# The Weekly Challenge 145 -Solution by Simon Green. Sorry, no blog this week. Hopefully the code -is easy enough to understand. +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-145-4kho) diff --git a/challenge-145/sgreen/blog.txt b/challenge-145/sgreen/blog.txt new file mode 100644 index 0000000000..50ca8d8ff9 --- /dev/null +++ b/challenge-145/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-145-4kho diff --git a/challenge-145/sgreen/perl/ch-1.pl b/challenge-145/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..4b6a431082 --- /dev/null +++ b/challenge-145/sgreen/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use List::Util 'sum'; + +sub main { + my @first_nums = ( shift =~ /(-?\d+(?:\.\d+)?)/gm ); + my @second_nums = ( shift =~ /(-?\d+(?:\.\d+)?)/gm ); + + if ( @first_nums != @second_nums ) { + die "Arrays are of different lengths\n"; + } + + my @dots = (); + my @sums = (); + + foreach my $i ( 0 .. $#first_nums ) { + push @dots, "($first_nums[$i] × $second_nums[$i])"; + push @sums, $first_nums[$i] * $second_nums[$i]; + } + + say join( ' + ', @dots ), ' => ', join( ' + ', @sums ), ' => ', sum(@sums); +} + +main(@ARGV); diff --git a/challenge-145/sgreen/perl/ch-2.pl b/challenge-145/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..eee8054ad5 --- /dev/null +++ b/challenge-145/sgreen/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use List::Util 'none'; + +sub main { + my $word = shift; + my @strings = (); + + # Make the starting point from each character + foreach my $i ( 0 .. length($word) - 1 ) { + + # And the end point + foreach my $j ( $i .. length($word) ) { + + # This is string we will examine + my $s = substr( $word, $i, $j - $i ); + + # If we don't already know about it, and it's palindromic, add + # it to the string array + if ( none { $s eq $_ } @strings and $s eq reverse($s) ) { + push @strings, $s; + } + } + } + + say join ' ', @strings; +} + +main(@ARGV); diff --git a/challenge-145/sgreen/python/ch-1.py b/challenge-145/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7c18890304 --- /dev/null +++ b/challenge-145/sgreen/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import sys +import re + + +def main(inputs): + first_nums = list(map(int, re.findall(r'-?\d+', inputs[0]))) + second_nums = list(map(int, re.findall(r'-?\d+', inputs[1]))) + + if len(first_nums) != len(second_nums): + raise ValueError('Arrays are of different lengths') + + dots = [] + sums = [] + for i in range(len(first_nums)): + dots.append(f'({first_nums[i]} × {second_nums[i]})') + sums.append(first_nums[i] * second_nums[i]) + + print(' + '.join(dots) + + ' => ' + + ' + '.join(map(str, sums)) + + ' => ' + + str(sum(sums))) + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/challenge-145/sgreen/python/ch-2.py b/challenge-145/sgreen/python/ch-2.py new file mode 100755 index 0000000000..65ed569178 --- /dev/null +++ b/challenge-145/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import sys + + +def main(word): + strings = [] + + # Make the starting point from each character + for i in range(len(word)): + + # And the end point + for j in range(i, len(word)): + + # This is string we will examine + s = word[i:j + 1] + + # If we don't already know about it, and it's palindromic, add + # it to the string array + if s not in strings and s == s[::-1]: + strings.append(s) + + print(*strings, sep=' ') + + +if __name__ == '__main__': + main(sys.argv[1]) -- cgit