diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-18 21:40:14 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-18 21:40:14 +0100 |
| commit | a12aefacd745c4abc7fdced7912e8e15f3739d29 (patch) | |
| tree | 9d16bea1243249bcc1766cdc68cdfbb72c7a8406 | |
| parent | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff) | |
| download | perlweeklychallenge-club-a12aefacd745c4abc7fdced7912e8e15f3739d29.tar.gz perlweeklychallenge-club-a12aefacd745c4abc7fdced7912e8e15f3739d29.tar.bz2 perlweeklychallenge-club-a12aefacd745c4abc7fdced7912e8e15f3739d29.zip | |
Add Python solution to challenge 065
| -rw-r--r-- | challenge-064/paulo-custodio/python/ch-2.py | 2 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/python/ch-2.py | 56 |
5 files changed, 94 insertions, 4 deletions
diff --git a/challenge-064/paulo-custodio/python/ch-2.py b/challenge-064/paulo-custodio/python/ch-2.py index 12034bea57..deff42f193 100644 --- a/challenge-064/paulo-custodio/python/ch-2.py +++ b/challenge-064/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 064 # diff --git a/challenge-065/paulo-custodio/perl/ch-1.pl b/challenge-065/paulo-custodio/perl/ch-1.pl index 67d2eb12e6..a4d26d30c4 100644 --- a/challenge-065/paulo-custodio/perl/ch-1.pl +++ b/challenge-065/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 065 # -# TASK #1 › Digits Sum +# TASK #1 > Digits Sum # Submitted by: Mohammad S Anwar # Reviewed by: Ryan Thompson # diff --git a/challenge-065/paulo-custodio/perl/ch-2.pl b/challenge-065/paulo-custodio/perl/ch-2.pl index 1adaf4eb34..085f793168 100644 --- a/challenge-065/paulo-custodio/perl/ch-2.pl +++ b/challenge-065/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 065 # -# TASK #2 › Palindrome Partition +# TASK #2 > Palindrome Partition # Submitted by: Mohammad S Anwar # Reviewed by: Ryan Thompson # @@ -10,7 +10,7 @@ # gives Palindrome. Return -1 if none found. # # Please make sure, partition should not overlap. For example, for given string -# “abaab”, the partition “aba” and “baab” would not be valid, since they overlap. +# "abaab", the partition "aba" and "baab" would not be valid, since they overlap. # # Example 1 # Input: $S = 'aabaab' diff --git a/challenge-065/paulo-custodio/python/ch-1.py b/challenge-065/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..344143f905 --- /dev/null +++ b/challenge-065/paulo-custodio/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# Challenge 065 +# +# TASK #1 > Digits Sum +# Submitted by: Mohammad S Anwar +# Reviewed by: Ryan Thompson +# +# You are given two positive numbers $N and $S. +# +# Write a script to list all positive numbers having exactly $N digits where sum +# of all digits equals to $S. +# +# Example +# Input: +# $N = 2 +# $S = 4 +# +# Output: +# 13, 22, 31, 40 + +import sys +from itertools import product + +def digits_sum(N, S): + out = [] + for n in range(10**(N-1), 10**N): + if sum(int(d) for d in str(n)) == S: + out.append(n) + return out + +N, S = map(int, sys.argv[1:]) +out = digits_sum(N, S) +print(", ".join(map(str, out))) diff --git a/challenge-065/paulo-custodio/python/ch-2.py b/challenge-065/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..702ed4d413 --- /dev/null +++ b/challenge-065/paulo-custodio/python/ch-2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Challenge 065 +# +# TASK #2 > Palindrome Partition +# Submitted by: Mohammad S Anwar +# Reviewed by: Ryan Thompson +# +# You are given a string $S. Write a script print all possible partitions that +# gives Palindrome. Return -1 if none found. +# +# Please make sure, partition should not overlap. For example, for given string +# "abaab", the partition "aba" and "baab" would not be valid, since they overlap. +# +# Example 1 +# Input: $S = 'aabaab' +# Ouput: +# There are 3 possible solutions. +# a) 'aabaa' +# b) 'aa', 'baab' +# c) 'aba' +# Example 2 +# Input: $S = 'abbaba' +# Output: +# There are 3 possible solutions. +# a) 'abba' +# b) 'bb', 'aba' +# c) 'bab' + +import sys + +def is_palindrome(s): + return len(s) >= 2 and s == s[::-1] + +def partitions(s): + seen = set() + length = len(s) + for lead in range(length + 1): + for p1 in range(2, length + 1): + for p2 in [0] + list(range(2, length + 1)): + for tail in range(length + 1): + if lead + p1 + p2 + tail != length: + continue + s1 = s[lead:lead+p1] + s2 = s[lead+p1:lead+p1+p2] + if is_palindrome(s1) and is_palindrome(s2): + key = f"{s1}, {s2}" + if key not in seen: + print(key) + seen.add(key) + elif is_palindrome(s1): + if s1 not in seen: + print(s1) + seen.add(s1) + +partitions(sys.argv[1] if len(sys.argv) > 1 else "") |
