aboutsummaryrefslogtreecommitdiff
path: root/challenge-072
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-20 14:48:44 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-20 14:48:44 +0100
commit4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (patch)
treef33731b0be43f3473393004f8cbfcf9326370fd6 /challenge-072
parent592752b24e8a00f5b61a35cfc5a6f29246cb42a5 (diff)
downloadperlweeklychallenge-club-4ae5477a9bc6c8b01ee984ed6a5a90dbec170833.tar.gz
perlweeklychallenge-club-4ae5477a9bc6c8b01ee984ed6a5a90dbec170833.tar.bz2
perlweeklychallenge-club-4ae5477a9bc6c8b01ee984ed6a5a90dbec170833.zip
Add Python solution to challenge 072
Diffstat (limited to 'challenge-072')
-rw-r--r--challenge-072/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-072/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-072/paulo-custodio/python/ch-1.py40
-rw-r--r--challenge-072/paulo-custodio/python/ch-2.py40
4 files changed, 82 insertions, 2 deletions
diff --git a/challenge-072/paulo-custodio/perl/ch-1.pl b/challenge-072/paulo-custodio/perl/ch-1.pl
index 6c00f85553..d435b392c7 100644
--- a/challenge-072/paulo-custodio/perl/ch-1.pl
+++ b/challenge-072/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 072
#
-# TASK #1 › Trailing Zeroes
+# TASK #1 > Trailing Zeroes
# Submitted by: Mohammad S Anwar
# You are given a positive integer $N (<= 10).
#
diff --git a/challenge-072/paulo-custodio/perl/ch-2.pl b/challenge-072/paulo-custodio/perl/ch-2.pl
index b01009ee25..506995a434 100644
--- a/challenge-072/paulo-custodio/perl/ch-2.pl
+++ b/challenge-072/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 072
#
-# TASK #2 › Lines Range
+# TASK #2 > Lines Range
# Submitted by: Mohammad S Anwar
# You are given a text file name $file and range $A - $B where $A <= $B.
#
diff --git a/challenge-072/paulo-custodio/python/ch-1.py b/challenge-072/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..8f455f1ac8
--- /dev/null
+++ b/challenge-072/paulo-custodio/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+# Challenge 072
+#
+# TASK #1 > Trailing Zeroes
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N (<= 10).
+#
+# Write a script to print number of trailing zeroes in $N!.
+#
+# Example 1
+# Input: $N = 10
+# Output: 2 as $N! = 3628800 has 2 trailing zeroes
+#
+# Example 2
+# Input: $N = 7
+# Output: 1 as $N! = 5040 has 1 trailing zero
+#
+# Example 3
+# Input: $N = 4
+# Output: 0 as $N! = 24 has 0 trailing zero
+
+import re
+import sys
+
+def fact(n):
+ if n < 2:
+ return 1
+ else:
+ return n*fact(n-1)
+
+def trailing_zeros(n):
+ s = str(n)
+ if m := re.search(r'0+$', s):
+ return len(m.group(0))
+ else:
+ return 0
+
+N = int(sys.argv[1])
+print(trailing_zeros(fact(N)))
diff --git a/challenge-072/paulo-custodio/python/ch-2.py b/challenge-072/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6d897d5b2b
--- /dev/null
+++ b/challenge-072/paulo-custodio/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+# Challenge 072
+#
+# TASK #2 > Lines Range
+# Submitted by: Mohammad S Anwar
+# You are given a text file name $file and range $A - $B where $A <= $B.
+#
+# Write a script to display lines range $A and $B in the given file.
+#
+# Example
+# Input:
+# $ cat input.txt
+# L1
+# L2
+# L3
+# L4
+# ...
+# ...
+# ...
+# ...
+# L100
+# $A = 4 and $B = 12
+# Output:
+# L4
+# L5
+# L6
+# L7
+# L8
+# L9
+# L10
+# L11
+# L12
+
+import sys
+FILE, A, B = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
+
+f = open(sys.argv[1], "r")
+lines = [x.rstrip("\n") for x in f.readlines()]
+print("\n".join(lines[A-1:B]))