diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-09 17:22:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-09 17:22:32 +0000 |
| commit | a275fa9bcf05f05cfc347a7b05a3fdcfb01b8fd2 (patch) | |
| tree | 1bf30ae1737350a4f401065b2d19a506ce8d8605 | |
| parent | 929f626fbb9a25944b2753e762d26f7c652d3d84 (diff) | |
| parent | 6f1f7bb5384bb1eee4d9cc27380b39c5b13a87f1 (diff) | |
| download | perlweeklychallenge-club-a275fa9bcf05f05cfc347a7b05a3fdcfb01b8fd2.tar.gz perlweeklychallenge-club-a275fa9bcf05f05cfc347a7b05a3fdcfb01b8fd2.tar.bz2 perlweeklychallenge-club-a275fa9bcf05f05cfc347a7b05a3fdcfb01b8fd2.zip | |
Merge pull request #5187 from pauloscustodio/devel
Add Python solution to challenge 138
| -rw-r--r-- | challenge-105/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-105/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-105/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-105/paulo-custodio/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-105/paulo-custodio/python/ch-2.py | 45 | ||||
| -rw-r--r-- | challenge-105/paulo-custodio/test.pl | 4 | ||||
| -rw-r--r-- | challenge-106/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-106/paulo-custodio/python/ch-2.py | 39 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/python/ch-1.py | 31 | ||||
| -rw-r--r-- | challenge-138/paulo-custodio/python/ch-2.py | 58 |
11 files changed, 209 insertions, 9 deletions
diff --git a/challenge-105/paulo-custodio/Makefile b/challenge-105/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-105/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-105/paulo-custodio/perl/ch-1.pl b/challenge-105/paulo-custodio/perl/ch-1.pl index c6ef0fbb89..09238281d9 100644 --- a/challenge-105/paulo-custodio/perl/ch-1.pl +++ b/challenge-105/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 105 # diff --git a/challenge-105/paulo-custodio/perl/ch-2.pl b/challenge-105/paulo-custodio/perl/ch-2.pl index 683584cdbd..be883d4e89 100644 --- a/challenge-105/paulo-custodio/perl/ch-2.pl +++ b/challenge-105/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 105 # diff --git a/challenge-105/paulo-custodio/python/ch-1.py b/challenge-105/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..fe52a1081e --- /dev/null +++ b/challenge-105/paulo-custodio/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +# Challenge 105 +# +# TASK #1 > Nth root +# Submitted by: Mohammad S Anwar +# You are given positive numbers $N and $k. +# +# Write a script to find out the $Nth root of $k. For more information, please +# take a look at the wiki page. +# +# Example +# Input: $N = 5, $k = 248832 +# Output: 12 +# +# Input: $N = 5, $k = 34 +# Output: 2.02 + +import sys + +def round(n): + ROUND_FACTOR = 10000 + result = int(n*ROUND_FACTOR+0.5)/ROUND_FACTOR + if int(result)==result: + result = int(result) + return result + +n, k = int(sys.argv[1]), int(sys.argv[2]) +print(round(k ** (1/n))) diff --git a/challenge-105/paulo-custodio/python/ch-2.py b/challenge-105/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e074ad3185 --- /dev/null +++ b/challenge-105/paulo-custodio/python/ch-2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Challenge 105 +# +# TASK #2 > The Name Game +# Submitted by: Mohammad S Anwar +# You are given a $name. +# +# Write a script to display the lyrics to the Shirley Ellis song The Name Game. +# Please checkout the wiki page for more information. +# +# Example +# Input: $name = "Katie" +# Output: +# +# Katie, Katie, bo-batie, +# Bonana-fanna fo-fatie +# Fee fi mo-matie +# Katie! + +import sys +import re + +name = sys.argv[1] +end = re.sub(r"^[bcdfghjklmnpqrstvwxyz]", "", name, flags=re.IGNORECASE).lower() + +if re.search(r"^b", name, flags=re.IGNORECASE): + b = "" +else: + b = "b" + +if re.search(r"^f", name, flags=re.IGNORECASE): + f = "" +else: + f = "f" + +if re.search(r"^m", name, flags=re.IGNORECASE): + m = "" +else: + m = "m" + +print(f"{name}, {name}, bo-{b}{end},") +print(f"Bonana-fanna fo-{f}{end}") +print(f"Fee fi mo-{m}{end}") +print(f"{name}!") diff --git a/challenge-105/paulo-custodio/test.pl b/challenge-105/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-105/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-106/paulo-custodio/perl/ch-2.pl b/challenge-106/paulo-custodio/perl/ch-2.pl index 390fa68cb1..aa2f756590 100644 --- a/challenge-106/paulo-custodio/perl/ch-2.pl +++ b/challenge-106/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # TASK #2 › Decimal String # Submitted by: Mohammad S Anwar diff --git a/challenge-106/paulo-custodio/python/ch-2.py b/challenge-106/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a019e26d6 --- /dev/null +++ b/challenge-106/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# TASK #2 > Decimal String +# Submitted by: Mohammad S Anwar +# You are given numerator and denominator i.e. $N and $D. +# +# Write a script to convert the fraction into decimal string. If the fractional +# part is recurring then put it in parenthesis. +# +# Example +# Input: $N = 1, $D = 3 +# Output: "0.(3)" +# +# Input: $N = 1, $D = 2 +# Output: "0.5" +# +# Input: $N = 5, $D = 66 +# Output: "0.0(75)" + +import sys +import re +from decimal import * + +def frac2str(n, d): + getcontext().prec = 1000 + getcontext().rounding = ROUND_DOWN + + n = Decimal(n) + d = Decimal(d) + q = str(n/d) + + for rept in range(1,101): + match = re.search(r"((\d{"+str(rept)+r"})\2+\d*)", q) + if match: + q = re.sub(match.group(1), "("+match.group(2)+")", q) + return q + return q + +print(frac2str(int(sys.argv[1]), int(sys.argv[2]))) diff --git a/challenge-138/paulo-custodio/perl/ch-2.pl b/challenge-138/paulo-custodio/perl/ch-2.pl index c64a445618..27eceb6508 100644 --- a/challenge-138/paulo-custodio/perl/ch-2.pl +++ b/challenge-138/paulo-custodio/perl/ch-2.pl @@ -29,7 +29,7 @@ use List::Util 'sum'; my $n = shift||1; say sqroot_is_sum_splits($n); -sub num_splits { +sub get_splits { my($n) = @_; my @splits; add_splits(\@splits, [], $n); @@ -54,7 +54,7 @@ sub sqroot_is_sum_splits { my($n) = @_; my $sq = sqrt($n); return 0 if int($sq) != $sq; # not pefect square - for (num_splits($n)) { + for (get_splits($n)) { my @split = @$_; return 1 if sum(@split) == $sq; } diff --git a/challenge-138/paulo-custodio/python/ch-1.py b/challenge-138/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..b5595a79a0 --- /dev/null +++ b/challenge-138/paulo-custodio/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# TASK #1 > Workdays +# Submitted by: Mohammad S Anwar +# You are given a year, $year in 4-digits form. +# +# Write a script to calculate the total number of workdays in the given year. +# +# For the task, we consider, Monday - Friday as workdays. +# +# Example 1 +# Input: $year = 2021 +# Output: 261 +# Example 2 +# Input: $year = 2020 +# Output: 262 + +import sys +import datetime + +def count_work_days(year): + count = 0 + dt = datetime.date(year, 1, 1) + while dt.year == year: + dow = dt.isoweekday() + if dow < 6: + count += 1 + dt += datetime.timedelta(days=1) + return count + +print(count_work_days(int(sys.argv[1]))) diff --git a/challenge-138/paulo-custodio/python/ch-2.py b/challenge-138/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..7c6aa8862e --- /dev/null +++ b/challenge-138/paulo-custodio/python/ch-2.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +# TASK #2 > Split Number +# Submitted by: Mohammad S Anwar +# You are given a perfect square. +# +# Write a script to figure out if the square root the given number is same as +# sum of 2 or more splits of the given number. +# +# Example 1 +# Input: $n = 81 +# Output: 1 +# +# Since, sqrt(81) = 8 + 1 +# Example 2 +# Input: $n = 9801 +# Output: 1 +# +# Since, sqrt(9801) = 98 + 0 + 1 +# Example 3 +# Input: $n = 36 +# Output: 0 +# +# Since, sqrt(36) != 3 + 6 + +import sys +import math + +def get_splits(n): + splits = [] + + def add_splits(path, rest): + nonlocal splits + + if rest=="": + splits.append(path) + else: + for i in range(1, len(rest)+1): + a = rest[0:i] + b = rest[i:] + add_splits([*path, int(a)], b) + + add_splits([], str(n)) + return splits + +def sqroot_is_sum_splits(n): + sq = math.sqrt(n) + if int(sq)!=sq: + return False # not perfect square + for split in get_splits(n): + if sum(split)==int(sq): + return True + return False + +if sqroot_is_sum_splits(int(sys.argv[1])): + print(1) +else: + print(0) |
