diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-10 18:16:51 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-10 18:16:51 +0000 |
| commit | e6984ea881fc8dbe31017c60ba4f8af9ddab8329 (patch) | |
| tree | a183f8386bf250512d63808f2010395a7491eec8 | |
| parent | d83f91b866be692bee5eb46cf0252eeebb4634ef (diff) | |
| download | perlweeklychallenge-club-e6984ea881fc8dbe31017c60ba4f8af9ddab8329.tar.gz perlweeklychallenge-club-e6984ea881fc8dbe31017c60ba4f8af9ddab8329.tar.bz2 perlweeklychallenge-club-e6984ea881fc8dbe31017c60ba4f8af9ddab8329.zip | |
Add Python solution to challenge 88
| -rw-r--r-- | challenge-088/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/python/ch-1.py | 45 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/python/ch-2.py | 70 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/t/test-1.yaml | 20 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/t/test-2.yaml | 50 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/test.pl | 66 |
8 files changed, 189 insertions, 68 deletions
diff --git a/challenge-088/paulo-custodio/Makefile b/challenge-088/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-088/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-088/paulo-custodio/perl/ch-1.pl b/challenge-088/paulo-custodio/perl/ch-1.pl index d6dfe94e73..bcbfc0a151 100644 --- a/challenge-088/paulo-custodio/perl/ch-1.pl +++ b/challenge-088/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 088 # diff --git a/challenge-088/paulo-custodio/perl/ch-2.pl b/challenge-088/paulo-custodio/perl/ch-2.pl index af4b0c2c28..deb0502b22 100644 --- a/challenge-088/paulo-custodio/perl/ch-2.pl +++ b/challenge-088/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 088 # diff --git a/challenge-088/paulo-custodio/python/ch-1.py b/challenge-088/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c63ae8f4be --- /dev/null +++ b/challenge-088/paulo-custodio/python/ch-1.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Challenge 088 +# +# TASK #1 > Array of Product +# Submitted by: Mohammad S Anwar +# You are given an array of positive integers @N. +# +# Write a script to return an array @M where $M[i] is the product of all elements +# of @N except the index $N[i]. +# +# Example 1: +# Input: +# @N = (5, 2, 1, 4, 3) +# Output: +# @M = (24, 60, 120, 30, 40) +# +# $M[0] = 2 x 1 x 4 x 3 = 24 +# $M[1] = 5 x 1 x 4 x 3 = 60 +# $M[2] = 5 x 2 x 4 x 3 = 120 +# $M[3] = 5 x 2 x 1 x 3 = 30 +# $M[4] = 5 x 2 x 1 x 4 = 40 +# Example 2: +# Input: +# @N = (2, 1, 4, 3) +# Output: +# @M = (12, 24, 6, 8) +# +# $M[0] = 1 x 4 x 3 = 12 +# $M[1] = 2 x 4 x 3 = 24 +# $M[2] = 2 x 1 x 3 = 6 +# $M[3] = 2 x 1 x 4 = 8 + +import sys + +def array_product(n): + m = [1 for x in n] # initialize the products to 1 + for i in range(len(n)): + for j in range(len(m)): + if i!=j: + m[j] *= n[i] + return m + +m = array_product([int(x) for x in sys.argv[1:]]) +print("("+ ", ".join([str(x) for x in m]) +")") diff --git a/challenge-088/paulo-custodio/python/ch-2.py b/challenge-088/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..c2a06ba585 --- /dev/null +++ b/challenge-088/paulo-custodio/python/ch-2.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +# Challenge 088 +# +# TASK #2 > Spiral Matrix +# Submitted by: Mohammad S Anwar +# You are given m x n matrix of positive integers. +# +# Write a script to print spiral matrix as list. +# +# Example 1: +# Input: +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# Ouput: +# [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ] +# Example 2: +# Input: +# [ 1, 2, 3, 4 ] +# [ 5, 6, 7, 8 ] +# [ 9, 10, 11, 12 ] +# [ 13, 14, 15, 16 ] +# Output: +# [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ] + +import fileinput +import re + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def read_matrix(lines): + m = [] + for line in lines: + line = re.sub(r"\D+", " ", line) + cols = [int(x) for x in line.split()] + m.append(cols) + return m + +def spiral(m): + s = [] + while len(m) > 0: + # put top row left-right + for n in m[0]: + s.append(n) + m.pop(0) + + # put right column top-bottom + if len(m) > 0 and len(m[0]) > 0: + for r in range(len(m)): + s.append(m[r].pop(-1)) + + # put bottom row right-left + if len(m) > 0: + for n in m[-1][::-1]: + s.append(n) + m.pop(-1) + + # put left column top-bottom + if len(m) > 0 and len(m[-1]) > 0: + for r in range(len(m))[::-1]: + s.append(m[r].pop(0)) + return s + +s = spiral(read_matrix(read_input())) +print("[ "+ ", ".join([str(x) for x in s]) +" ]") diff --git a/challenge-088/paulo-custodio/t/test-1.yaml b/challenge-088/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c22655bdeb --- /dev/null +++ b/challenge-088/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 5 2 1 4 3 + input: + output: (24, 60, 120, 30, 40) +- setup: + cleanup: + args: 2 1 4 3 + input: + output: (12, 24, 6, 8) +- setup: + cleanup: + args: 0 1 4 3 + input: + output: (12, 0, 0, 0) +- setup: + cleanup: + args: 1 1 1 1 + input: + output: (1, 1, 1, 1) diff --git a/challenge-088/paulo-custodio/t/test-2.yaml b/challenge-088/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f9083e28a7 --- /dev/null +++ b/challenge-088/paulo-custodio/t/test-2.yaml @@ -0,0 +1,50 @@ +- setup: + cleanup: + args: + input: | + [ 5 ] + output: | + [ 5 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + output: | + [ 1, 2, 3 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + [ 7, 8, 9 ] + output: | + [ 1, 2, 3, 9, 8, 7 ] +- setup: + cleanup: + args: + input: | + [ 1 ] + [ 4 ] + [ 7 ] + output: | + [ 1, 4, 7 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + [ 4, 5, 6 ] + [ 7, 8, 9 ] + output: | + [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3, 4 ] + [ 5, 6, 7, 8 ] + [ 9, 10, 11, 12 ] + [ 13, 14, 15, 16 ] + output: | + [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ] diff --git a/challenge-088/paulo-custodio/test.pl b/challenge-088/paulo-custodio/test.pl deleted file mode 100644 index e2a07e0555..0000000000 --- a/challenge-088/paulo-custodio/test.pl +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -my $in = "in.txt"; - -is capture("perl perl/ch-1.pl 5 2 1 4 3"), "(24, 60, 120, 30, 40)\n"; -is capture("perl perl/ch-1.pl 2 1 4 3 "), "(12, 24, 6, 8)\n"; -is capture("perl perl/ch-1.pl 0 1 4 3 "), "(12, 0, 0, 0)\n"; -is capture("perl perl/ch-1.pl 1 1 1 1 "), "(1, 1, 1, 1)\n"; - -spew($in, <<END); -[ 5 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 5 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -[ 7, 8, 9 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3, 9, 8, 7 ]\n"; - -spew($in, <<END); -[ 1 ] -[ 4 ] -[ 7 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 4, 7 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -[ 4, 5, 6 ] -[ 7, 8, 9 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3, 6, 9, 8, 7, 4, 5 ]\n"; - -spew($in, <<END); -[ 1, 2, 3, 4 ] -[ 5, 6, 7, 8 ] -[ 9, 10, 11, 12 ] -[ 13, 14, 15, 16 ] -END -is capture("perl perl/ch-2.pl <$in"), - "[ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]\n"; - -unlink($in); -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} - -sub spew { - my($file, $text) = @_; - open(my $fh, ">", $file) or die "write $file: $!\n"; - print $fh $text; -} |
