diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-12 11:21:00 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-12 11:21:00 +0000 |
| commit | 7eedbc877a5e2e6750bd0c302f5880d05a3505a5 (patch) | |
| tree | 208229fd2efdf398c91e7c0ee7ac189f88978fcf | |
| parent | d00e3b49c7580928b6e32080bd3ce9f4f7d7bbac (diff) | |
| download | perlweeklychallenge-club-7eedbc877a5e2e6750bd0c302f5880d05a3505a5.tar.gz perlweeklychallenge-club-7eedbc877a5e2e6750bd0c302f5880d05a3505a5.tar.bz2 perlweeklychallenge-club-7eedbc877a5e2e6750bd0c302f5880d05a3505a5.zip | |
Add Python solution to challenge 82
| -rw-r--r-- | challenge-082/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/perl/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/python/ch-1.py | 46 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/python/ch-2.py | 47 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/t/test-2.yaml | 20 | ||||
| -rw-r--r-- | challenge-082/paulo-custodio/test.pl | 21 |
8 files changed, 135 insertions, 26 deletions
diff --git a/challenge-082/paulo-custodio/Makefile b/challenge-082/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-082/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-082/paulo-custodio/perl/ch-1.pl b/challenge-082/paulo-custodio/perl/ch-1.pl index 5c83822a26..36f2083ee1 100644 --- a/challenge-082/paulo-custodio/perl/ch-1.pl +++ b/challenge-082/paulo-custodio/perl/ch-1.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 082 # -# TASK #1 › Common Factors +# TASK #1 > Common Factors # Submitted by: Niels van Dijke # You are given 2 positive numbers $M and $N. # @@ -40,7 +40,7 @@ say "(", join(", ", @common), ")"; sub common_factors { my($a, $b) = @_; my @common; - for (my $i = 1; 2*$i <= $a || 2*$i <= $b; $i++) { + for (my $i = 1; $i <= $a || $i <= $b; $i++) { if (($a % $i)==0 && ($b % $i)==0) { push @common, $i; } diff --git a/challenge-082/paulo-custodio/perl/ch-2.pl b/challenge-082/paulo-custodio/perl/ch-2.pl index 40d7fa92b8..0ff83b3ce0 100644 --- a/challenge-082/paulo-custodio/perl/ch-2.pl +++ b/challenge-082/paulo-custodio/perl/ch-2.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 082 # -# TASK #2 › Interleave String +# TASK #2 > Interleave String # Submitted by: Mohammad S Anwar # You are given 3 strings; $A, $B and $C. # diff --git a/challenge-082/paulo-custodio/python/ch-1.py b/challenge-082/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c1eb31d194 --- /dev/null +++ b/challenge-082/paulo-custodio/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 082 +# +# TASK #1 > Common Factors +# Submitted by: Niels van Dijke +# You are given 2 positive numbers $M and $N. +# +# Write a script to list all common factors of the given numbers. +# +# Example 1: +# Input: +# $M = 12 +# $N = 18 +# +# Output: +# (1, 2, 3, 6) +# +# Explanation: +# Factors of 12: 1, 2, 3, 4, 6 +# Factors of 18: 1, 2, 3, 6, 9 +# Example 2: +# Input: +# $M = 18 +# $N = 23 +# +# Output: +# (1) +# +# Explanation: +# Factors of 18: 1, 2, 3, 6, 9 +# Factors of 23: 1 + +import sys + +def get_common_factors(a, b): + factors = [] + i = 1 + while i <= a or i <= b: + if a%i==0 and b%i==0: + factors.append(i) + i += 1 + return factors + +factors = get_common_factors(int(sys.argv[1]), int(sys.argv[2])) +print("("+ ", ".join([str(x) for x in factors]) +" )") diff --git a/challenge-082/paulo-custodio/python/ch-2.py b/challenge-082/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..743f578782 --- /dev/null +++ b/challenge-082/paulo-custodio/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +# Challenge 082 +# +# TASK #2 > Interleave String +# Submitted by: Mohammad S Anwar +# You are given 3 strings; $A, $B and $C. +# +# Write a script to check if $C is created by interleave $A and $B. +# +# Print 1 if check is success otherwise 0. +# +# Example 1: +# Input: +# $A = "XY" +# $B = "X" +# $C = "XXY" +# +# Output: 1 +# EXPLANATION +# "X" (from $B) + "XY" (from $A) = $C +# Example 2: +# Input: +# $A = "XXY" +# $B = "XXZ" +# $C = "XXXXZY" +# +# Output: 1 +# EXPLANATION +# "XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C +# Example 3: +# Input: +# $A = "YX" +# $B = "X" +# $C = "XXY" +# +# Output: 0 + +import sys + +def interleaved(a, b, c): + for i in range(len(a)+1): + if a[:i]+b+a[i:] == c: + return True + return False + +print(1 if interleaved(sys.argv[1], sys.argv[2], sys.argv[3]) else 0) diff --git a/challenge-082/paulo-custodio/t/test-1.yaml b/challenge-082/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c4ddea94be --- /dev/null +++ b/challenge-082/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 12 18 + input: + output: (1, 2, 3, 6) +- setup: + cleanup: + args: 18 23 + input: + output: (1) +- setup: + cleanup: + args: 5 5 + input: + output: (1, 5) diff --git a/challenge-082/paulo-custodio/t/test-2.yaml b/challenge-082/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3115516fe0 --- /dev/null +++ b/challenge-082/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: XY X XXY + input: + output: 1 +- setup: + cleanup: + args: XY Z XYZ + input: + output: 1 +- setup: + cleanup: + args: XXY XXZ XXXXZY + input: + output: 1 +- setup: + cleanup: + args: YX X XXY + input: + output: 0 diff --git a/challenge-082/paulo-custodio/test.pl b/challenge-082/paulo-custodio/test.pl deleted file mode 100644 index a14c8eda63..0000000000 --- a/challenge-082/paulo-custodio/test.pl +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl/ch-1.pl 12 18"), "(1, 2, 3, 6)\n"; -is capture("perl/ch-1.pl 18 23"), "(1)\n"; - -is capture("perl/ch-2.pl XY X XXY"), "1\n"; -is capture("perl/ch-2.pl XY Z XYZ"), "1\n"; -is capture("perl/ch-2.pl XXY XXZ XXXXZY"), "1\n"; -is capture("perl/ch-2.pl YX X XXY"), "0\n"; - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
