diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-24 17:31:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-24 17:31:39 +0100 |
| commit | 5d25c6e2b681143209bb64c08fb04a822e962133 (patch) | |
| tree | 2a4775a308f3762ca6103bf25f3b5efef404ce84 | |
| parent | 247e5a8ccbf2ca49929f435a6efc5e7f9c6e4e36 (diff) | |
| parent | cdd967fb112b2fe41e9b9909b43b35c2fce76dc7 (diff) | |
| download | perlweeklychallenge-club-5d25c6e2b681143209bb64c08fb04a822e962133.tar.gz perlweeklychallenge-club-5d25c6e2b681143209bb64c08fb04a822e962133.tar.bz2 perlweeklychallenge-club-5d25c6e2b681143209bb64c08fb04a822e962133.zip | |
Merge pull request #6328 from simongreen-net/master
sgreen solutions to challenge 170
| -rw-r--r-- | challenge-170/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-170/sgreen/input1.yml | 5 | ||||
| -rw-r--r-- | challenge-170/sgreen/input2.yml | 6 | ||||
| -rwxr-xr-x | challenge-170/sgreen/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-170/sgreen/perl/ch-2.pl | 62 | ||||
| -rwxr-xr-x | challenge-170/sgreen/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-170/sgreen/python/ch-2.py | 41 |
7 files changed, 193 insertions, 3 deletions
diff --git a/challenge-170/sgreen/README.md b/challenge-170/sgreen/README.md index 9956720a38..512411c9b5 100644 --- a/challenge-170/sgreen/README.md +++ b/challenge-170/sgreen/README.md @@ -1,3 +1 @@ -# The Weekly Challenge 169 - -Blog: [It's all about the numbers](https://dev.to/simongreennet/its-all-about-the-numbers-4fh5)
\ No newline at end of file +# The Weekly Challenge 170 diff --git a/challenge-170/sgreen/input1.yml b/challenge-170/sgreen/input1.yml new file mode 100644 index 0000000000..a5d16eed2d --- /dev/null +++ b/challenge-170/sgreen/input1.yml @@ -0,0 +1,5 @@ +- - [ 1, 2 ] + - [ 3, 4 ] + +- - [ 5, 6 ] + - [ 7, 8 ]
\ No newline at end of file diff --git a/challenge-170/sgreen/input2.yml b/challenge-170/sgreen/input2.yml new file mode 100644 index 0000000000..a323a95c6d --- /dev/null +++ b/challenge-170/sgreen/input2.yml @@ -0,0 +1,6 @@ +- - [ 1, -4 ,7 ] + - [ -2, 3, 3 ] +- - [ 8, -9, -6, 5 ] + - [ 1, -3, -4, 7 ] + - [ 2, 8, -8, -3 ] + - [ 1, 2, -5, -1 ]
\ No newline at end of file diff --git a/challenge-170/sgreen/perl/ch-1.pl b/challenge-170/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..8e9b3328ea --- /dev/null +++ b/challenge-170/sgreen/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental 'signatures'; +use feature 'say'; + +sub is_prime ($number) { + # Return true or false if the number is a prime + if ( $number < 2 ) { + return; + } + + foreach my $i ( 2 .. sqrt($number) ) { + if ( $number % $i == 0 ) { + return; + } + } + + # It's a prime + return 1; +} + +sub main { + my @solutions = (1); + my $value = 1; + my $counter = 0; + + # We need 10 solutions + while ( @solutions < 10 ) { + $counter++; + if ( is_prime($counter) ) { + # Multiple the solution by this number + $value *= $counter; + push @solutions, $value; + } + } + + say join ', ', @solutions; +} + +main();
\ No newline at end of file diff --git a/challenge-170/sgreen/perl/ch-2.pl b/challenge-170/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..792a7f15ac --- /dev/null +++ b/challenge-170/sgreen/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental 'signatures'; +use feature 'say'; + +use List::Util 'max'; +use YAML 'LoadFile'; + +sub check_array ($a) { + # Check that all rows in the array are the same length + foreach my $i ( 1 .. $#$a ) { + if ( scalar( @{ $a->[0] } ) != scalar( @{ $a->[$i] } ) ) { + die "mismatched array width"; + } + } + + return $a; +} + +sub make_row ( $values1, $values2 ) { + # Given two arrays, multiple the first value by each of the second values + my @row = (); + foreach my $value (@$values1) { + push @row, map { $value * $_ } @$values2; + } + return \@row; +} + +sub main ($file_name) { + # Read the supplied file in YAML format + my $arrays = LoadFile($file_name); + + if ( scalar(@$arrays) != 2 ) { + die "Expecting two arrays\n"; + } + + my $array1 = check_array( $arrays->[0] ); + my $array2 = check_array( $arrays->[1] ); + + my @solution = (); + foreach my $row (@$array1) { + foreach my $second_row (@$array2) { + push @solution, make_row( $row, $second_row ); + } + } + + # Format each row uniformly. + my $max_length = max( + map { + max( map { length($_) } @$_ ) + } @solution + ); + my $fmt = '[' . ( " %${max_length}s" x scalar( @{ $solution[0] } ) ) . " ]\n"; + + foreach my $row (@solution) { + printf $fmt, @$row; + } +} + +main(@ARGV); diff --git a/challenge-170/sgreen/python/ch-1.py b/challenge-170/sgreen/python/ch-1.py new file mode 100755 index 0000000000..356393c2b4 --- /dev/null +++ b/challenge-170/sgreen/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import math + + +def is_prime(number): + '''Return true or false if the number is a prime''' + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def main(): + solutions = [1] + value = 1 + counter = 0 + + # We need 10 solutions + while len(solutions) < 10: + counter += 1 + if is_prime(counter): + # Multiple the solution by this number + value *= counter + solutions.append(value) + + print(*solutions, sep=', ') + + +if __name__ == '__main__': + main() diff --git a/challenge-170/sgreen/python/ch-2.py b/challenge-170/sgreen/python/ch-2.py new file mode 100755 index 0000000000..fc0700a967 --- /dev/null +++ b/challenge-170/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import sys +import yaml + + +def check_array(a): + ''' Check that all rows in the array are the same length''' + for i in range(1, len(a)): + if (len(a[0]) != len(a[i])): + raise ValueError('mismatched array width') + + return a + + +def main(file_name): + # Read the supplied file in YAML format + with open(file_name, 'r') as file: + arrays = yaml.safe_load(file) + + if len(arrays) != 2: + raise ValueError('Expecting two arrays') + + array1 = check_array(arrays[0]) + array2 = check_array(arrays[1]) + + solution = [] + for row in array1: + for second_row in array2: + solution.append(list( str(c1 * c2) for c1 in row for c2 in second_row)) + + # Format each row uniformly. + max_length = max(max(len(col) for col in row) for row in solution) + fmt = '[' + (' {:>' + str(max_length) + 's}') * len(solution[0]) + ' ]' + + for row in solution: + print(fmt.format(*row)) + + +if __name__ == '__main__': + main(sys.argv[1]) |
