aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-05 10:21:46 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-05 10:21:46 +0000
commit7d9930c228b5efce194374deaf9ddb87cafc1603 (patch)
tree569fbb2c616d7ae8f361ef0c2f2fb946d48c6bc1
parent54d32bbce1df813f3131b651d3017f81b83a5b6c (diff)
downloadperlweeklychallenge-club-7d9930c228b5efce194374deaf9ddb87cafc1603.tar.gz
perlweeklychallenge-club-7d9930c228b5efce194374deaf9ddb87cafc1603.tar.bz2
perlweeklychallenge-club-7d9930c228b5efce194374deaf9ddb87cafc1603.zip
Add Python solution to challenge 110
-rw-r--r--challenge-110/paulo-custodio/Makefile2
-rw-r--r--challenge-110/paulo-custodio/python/ch-1.py39
-rw-r--r--challenge-110/paulo-custodio/python/ch-2.py47
-rwxr-xr-xchallenge-110/paulo-custodio/test.pl4
4 files changed, 88 insertions, 4 deletions
diff --git a/challenge-110/paulo-custodio/Makefile b/challenge-110/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-110/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-110/paulo-custodio/python/ch-1.py b/challenge-110/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d0ae52b786
--- /dev/null
+++ b/challenge-110/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 110
+#
+# TASK #1 - Valid Phone Numbers
+# Submitted by: Mohammad S Anwar
+# You are given a text file.
+#
+# Write a script to display all valid phone numbers in the given text file.
+#
+# Acceptable Phone Number Formats
+# +nn nnnnnnnnnn
+# (nn) nnnnnnnnnn
+# nnnn nnnnnnnnnn
+# Input File
+# 0044 1148820341
+# +44 1148820341
+# 44-11-4882-0341
+# (44) 1148820341
+# 00 1148820341
+# Output
+# 0044 1148820341
+# +44 1148820341
+# (44) 1148820341
+
+import fileinput
+import sys
+import re
+
+def is_valid_phone(phone):
+ return re.match(r"^\s*(?:\+\d{2}|\(\d{2}\)|\d{4})\s+\d{10}\s*$", phone)
+
+def filter_input():
+ for phone in fileinput.input():
+ phone = phone.strip()
+ if is_valid_phone(phone):
+ print(phone)
+
+filter_input()
diff --git a/challenge-110/paulo-custodio/python/ch-2.py b/challenge-110/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..f73f24c8e9
--- /dev/null
+++ b/challenge-110/paulo-custodio/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+# Challenge 110
+#
+# TASK #2 - Transpose File
+# Submitted by: Mohammad S Anwar
+# You are given a text file.
+#
+# Write a script to transpose the contents of the given file.
+#
+# Input File
+# name,age,sex
+# Mohammad,45,m
+# Joe,20,m
+# Julie,35,f
+# Cristina,10,f
+# Output:
+# name,Mohammad,Joe,Julie,Cristina
+# age,45,20,35,10
+# sex,m,m,f,f
+
+import fileinput
+import sys
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ lines.append(line)
+ return lines
+
+def read_data(lines):
+ m = []
+ for line in lines:
+ line = line.strip()
+ cols = line.split(',')
+ m.append(cols)
+ return m
+
+def transpose(m):
+ t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
+ return t
+
+def print_data(m):
+ for row in m:
+ print(",".join(row))
+
+print_data(transpose(read_data(read_input())))
diff --git a/challenge-110/paulo-custodio/test.pl b/challenge-110/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-110/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';