diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-09 15:45:06 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-09 15:45:06 +0000 |
| commit | ca352d8cd7cca2853df072d214e0802aa1fd9fd9 (patch) | |
| tree | 2a492c7f5d63b499cd653b37b90e7f886c998205 | |
| parent | 4174c1724112a2200f3c5d001e9419fdffddafc8 (diff) | |
| download | perlweeklychallenge-club-ca352d8cd7cca2853df072d214e0802aa1fd9fd9.tar.gz perlweeklychallenge-club-ca352d8cd7cca2853df072d214e0802aa1fd9fd9.tar.bz2 perlweeklychallenge-club-ca352d8cd7cca2853df072d214e0802aa1fd9fd9.zip | |
Add Python solution to challenge 77
| -rw-r--r-- | challenge-077/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/perl/ch-1.pl | 8 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/perl/ch-2.pl | 10 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/python/ch-2.py | 58 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/t/test-1.yaml | 28 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/t/test-2.yaml | 17 | ||||
| -rw-r--r-- | challenge-077/paulo-custodio/test.pl | 45 |
8 files changed, 167 insertions, 52 deletions
diff --git a/challenge-077/paulo-custodio/Makefile b/challenge-077/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-077/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-077/paulo-custodio/perl/ch-1.pl b/challenge-077/paulo-custodio/perl/ch-1.pl index 9d7c9f7fc4..aaf650c6dd 100644 --- a/challenge-077/paulo-custodio/perl/ch-1.pl +++ b/challenge-077/paulo-custodio/perl/ch-1.pl @@ -2,12 +2,13 @@ # Challenge 077 # -# TASK #1 › Fibonacci Sum +# TASK #1 > Fibonacci Sum # Submitted by: Mohammad S Anwar # You are given a positive integer $N. # # UPDATE: 2020-09-07 09:00:00 -# Write a script to find out all possible combination of Fibonacci Numbers required to get $N on addition. +# Write a script to find out all possible combination of Fibonacci Numbers +# required to get $N on addition. # # You are NOT allowed to repeat a number. Print 0 if none found. # @@ -41,7 +42,8 @@ for my $k (1 .. scalar(@terms)) { my $combinat = Math::Combinatorics->new(count => $k, data => \@terms); while(my @set = $combinat->next_combination) { if (sum(@set) == $N) { - push @out, join(" + ", sort @set)." = $N\n"; + @set = sort {$a<=>$b} @set; + push @out, join(" + ", @set)." = $N\n"; } } } diff --git a/challenge-077/paulo-custodio/perl/ch-2.pl b/challenge-077/paulo-custodio/perl/ch-2.pl index 12a0eb3f36..8929127696 100644 --- a/challenge-077/paulo-custodio/perl/ch-2.pl +++ b/challenge-077/paulo-custodio/perl/ch-2.pl @@ -2,18 +2,20 @@ # Challenge 077 # -# TASK #2 › Lonely X +# TASK #2 > Lonely X # Submitted by: Mohammad S Anwar # You are given m x n character matrix consists of O and X only. # -# Write a script to count the total number of X surrounded by O only. Print 0 if none found. +# Write a script to count the total number of X surrounded by O only. +# Print 0 if none found. # # Example 1: # Input: [ O O X ] # [ X O O ] # [ X O O ] # -# Output: 1 as there is only one X at the first row last column surrounded by only O. +# Output: 1 as there is only one X at the first row last column surrounded by +# only O. # Example 2: # Input: [ O O X O ] # [ X O O O ] @@ -28,7 +30,7 @@ use Modern::Perl; -my @m = @ARGV; +my @m = <>; for (@m) { $_ = [split //, $_]; } my $lonely = 0; diff --git a/challenge-077/paulo-custodio/python/ch-1.py b/challenge-077/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..92fcacda04 --- /dev/null +++ b/challenge-077/paulo-custodio/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +# Challenge 077 +# +# TASK #1 > Fibonacci Sum +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# UPDATE: 2020-09-07 09:00:00 +# Write a script to find out all possible combination of Fibonacci Numbers +# required to get $N on addition. +# +# You are NOT allowed to repeat a number. Print 0 if none found. +# +# Example 1: +# Input: $N = 6 +# +# Output: +# 1 + 2 + 3 = 6 +# 1 + 5 = 6 +# Example 2: +# Input: $N = 9 +# +# Output: +# 1 + 8 = 9 +# 1 + 3 + 5 = 9 + +import sys +from itertools import combinations + +# compute list of Fibonacci numbers up to input +fib = [0, 1] + +def compute_fib(target): + global fib + while fib[-1] < target: + fib.append(fib[-1]+fib[-2]) + +N = int(sys.argv[1]) +compute_fib(N) + +# terms for addition are the Fibonacci numbers except the first two terms (0,1) +terms = fib[2:] +output = [] +for k in range(1, len(terms)+1): + for combin in combinations(terms, k): + if sum(combin)==N: + combin = sorted(list(combin)) + output.append(" + ".join([str(x) for x in combin])+" = "+str(N)) +output.sort() +print(*output,sep="\n") diff --git a/challenge-077/paulo-custodio/python/ch-2.py b/challenge-077/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..eb485e1e70 --- /dev/null +++ b/challenge-077/paulo-custodio/python/ch-2.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +# Challenge 077 +# +# TASK #2 > Lonely X +# Submitted by: Mohammad S Anwar +# You are given m x n character matrix consists of O and X only. +# +# Write a script to count the total number of X surrounded by O only. +# Print 0 if none found. +# +# Example 1: +# Input: [ O O X ] +# [ X O O ] +# [ X O O ] +# +# Output: 1 as there is only one X at the first row last column surrounded by +# only O. +# Example 2: +# Input: [ O O X O ] +# [ X O O O ] +# [ X O O X ] +# [ O X O O ] +# +# Output: 2 +# +# a) First X found at Row 1 Col 3. +# +# b) Second X found at Row 3 Col 4. + +import sys +import fileinput + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +m = read_input() +for i in range(len(m)): + m[i] = [x for x in m[i].rstrip("\n")] + +lonely = 0 +for row in range(len(m)): + for col in range(len(m[row])): + if m[row][col]=='X': + neigh = 0 + for dr in range(-1,2): + for dc in range(-1,2): + if dr!=0 or dc!=0: + if row+dr>=0 and row+dr<len(m): + if col+dc>=0 and col+dc<len(m[row]): + if m[row+dr][col+dc]=='X': + neigh += 1 + if neigh==0: + lonely += 1 +print(lonely) diff --git a/challenge-077/paulo-custodio/t/test-1.yaml b/challenge-077/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..06d13f5ff6 --- /dev/null +++ b/challenge-077/paulo-custodio/t/test-1.yaml @@ -0,0 +1,28 @@ +- setup: + cleanup: + args: 6 + input: + output: | + 1 + 2 + 3 = 6 + 1 + 5 = 6 +- setup: + cleanup: + args: 9 + input: + output: | + 1 + 3 + 5 = 9 + 1 + 8 = 9 +- setup: + cleanup: + args: 100 + input: + output: | + 1 + 2 + 3 + 5 + 13 + 21 + 55 = 100 + 1 + 2 + 3 + 5 + 34 + 55 = 100 + 1 + 2 + 3 + 5 + 89 = 100 + 1 + 2 + 8 + 13 + 21 + 55 = 100 + 1 + 2 + 8 + 34 + 55 = 100 + 1 + 2 + 8 + 89 = 100 + 3 + 8 + 13 + 21 + 55 = 100 + 3 + 8 + 34 + 55 = 100 + 3 + 8 + 89 = 100 diff --git a/challenge-077/paulo-custodio/t/test-2.yaml b/challenge-077/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5169a8dadc --- /dev/null +++ b/challenge-077/paulo-custodio/t/test-2.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: + input: | + OOX + XOO + XOO + output: 1 +- setup: + cleanup: + args: + input: | + OOXO + XOOO + XOOX + OXOO + output: 2 diff --git a/challenge-077/paulo-custodio/test.pl b/challenge-077/paulo-custodio/test.pl deleted file mode 100644 index 5ca5b6bdfb..0000000000 --- a/challenge-077/paulo-custodio/test.pl +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl 6"), <<END; -1 + 2 + 3 = 6 -1 + 5 = 6 -END - -is capture("perl perl/ch-1.pl 9"), <<END; -1 + 3 + 5 = 9 -1 + 8 = 9 -END - -is capture("perl perl/ch-1.pl 100"), <<END; -1 + 13 + 2 + 21 + 3 + 5 + 55 = 100 -1 + 13 + 2 + 21 + 55 + 8 = 100 -1 + 2 + 3 + 34 + 5 + 55 = 100 -1 + 2 + 3 + 5 + 89 = 100 -1 + 2 + 34 + 55 + 8 = 100 -1 + 2 + 8 + 89 = 100 -13 + 21 + 3 + 55 + 8 = 100 -3 + 34 + 55 + 8 = 100 -3 + 8 + 89 = 100 -END - -is capture("perl perl/ch-2.pl ". - "OOX ". - "XOO ". - "XOO "), "1\n"; -is capture("perl perl/ch-2.pl ". - "OOXO ". - "XOOO ". - "XOOX ". - "OXOO "), "2\n"; - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
