aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-09 12:30:36 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-09 12:30:36 +0000
commit5304a23d00a022c5e3ed05ec4eecb3b2fa2a0828 (patch)
treeb3a103fa708454b58838881c5dc27b0c9635c018
parent75d7dc93b1629888da13f19e1b58657d407f1c13 (diff)
downloadperlweeklychallenge-club-5304a23d00a022c5e3ed05ec4eecb3b2fa2a0828.tar.gz
perlweeklychallenge-club-5304a23d00a022c5e3ed05ec4eecb3b2fa2a0828.tar.bz2
perlweeklychallenge-club-5304a23d00a022c5e3ed05ec4eecb3b2fa2a0828.zip
Add Python solution to challenge 138
-rw-r--r--challenge-138/paulo-custodio/perl/ch-2.pl4
-rw-r--r--challenge-138/paulo-custodio/python/ch-1.py31
-rw-r--r--challenge-138/paulo-custodio/python/ch-2.py58
3 files changed, 91 insertions, 2 deletions
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)