aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-02 22:11:14 +0100
committerGitHub <noreply@github.com>2021-05-02 22:11:14 +0100
commit7ef61f0b2a15fc082bbd9496fba443eecc32a742 (patch)
tree36a0f2f3b286ebb65f3d45c4912498e2f046b119
parent597cbad831165a61d1c856fae84e97ee2dc27078 (diff)
parent270a7de8aed488e9cffd080da540d734e7d98b1d (diff)
downloadperlweeklychallenge-club-7ef61f0b2a15fc082bbd9496fba443eecc32a742.tar.gz
perlweeklychallenge-club-7ef61f0b2a15fc082bbd9496fba443eecc32a742.tar.bz2
perlweeklychallenge-club-7ef61f0b2a15fc082bbd9496fba443eecc32a742.zip
Merge pull request #3993 from Cris-HD/branch-for-challenge-110
Added challenge 110 solution
-rw-r--r--challenge-110/cristian-heredia/perl/ch_1.pl89
-rw-r--r--challenge-110/cristian-heredia/python/ch_1.py54
-rw-r--r--challenge-110/cristian-heredia/python/ch_2.py31
3 files changed, 174 insertions, 0 deletions
diff --git a/challenge-110/cristian-heredia/perl/ch_1.pl b/challenge-110/cristian-heredia/perl/ch_1.pl
new file mode 100644
index 0000000000..f70be995c9
--- /dev/null
+++ b/challenge-110/cristian-heredia/perl/ch_1.pl
@@ -0,0 +1,89 @@
+=begin
+ 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
+=end
+=cut
+
+use strict;
+use warnings;
+
+#Variables
+my $file_name = "";
+my $text = <<'END_MESSAGE';
+0044 1148820341
++44 1148820341
+44-11-4882-0341
+(44) 1148820341
+00 1148820341
+END_MESSAGE
+
+#regex
+ #+nn nnnnnnnnnn
+ #\+\d{2}\s\d{10}
+ #(nn) nnnnnnnnnn
+ #\(\d{2}\)\s\d{10}
+ #nnnn nnnnnnnnnn
+ #\d{4}\s\d{10}
+
+#Call function
+askFileName();
+print "Output\n";
+readTelf($file_name);
+
+#Check the valid phone numbers
+sub askFileName {
+
+ print "Please, introduce the name of the file\n";
+ $file_name = <>;
+ $file_name =~ s/\s//g;
+
+ #Check that the file exists, if not, it's created
+ if ( !-f $file_name) {
+ createFile();
+ }
+}
+
+#Create the file with a specific text
+sub createFile {
+
+ open (F, ">>$file_name") || die "Could not open file: $!\n";
+ print F "$text";
+ close F;
+}
+
+sub readTelf {
+
+ my $file = shift;
+ open(FILE, $file) or die "Could not read from $file\n";
+
+ #Read the file, line by line and find the valid phone numbers
+ while( my $line = <FILE>) {
+ if ($line =~ /\+\d{2}\s\d{10}/ or $line =~ /\(\d{2}\)\s\d{10}/ or $line =~ /\d{4}\s\d{10}/) {
+ print "$line";
+ }
+ }
+}
+
+
+
+
+
diff --git a/challenge-110/cristian-heredia/python/ch_1.py b/challenge-110/cristian-heredia/python/ch_1.py
new file mode 100644
index 0000000000..fff5965db1
--- /dev/null
+++ b/challenge-110/cristian-heredia/python/ch_1.py
@@ -0,0 +1,54 @@
+'''
+ 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 re
+import os
+
+#Variables
+text = """
+0044 1148820341
++44 1148820341
+44-11-4882-0341
+(44) 1148820341
+00 1148820341
+"""
+
+def askFileName():
+ file_name = input("Please, introduce the name of the file\n")
+
+ #We check that the file exists, if not, the file is created with a specific text
+ if (os.path.isfile(file_name) == False):
+ f = open(file_name, 'w')
+ f.write(str(text))
+ f.close()
+ print ("Output:")
+ readTelf(file_name)
+
+def readTelf(file_name):
+ with open(file_name, 'r') as my_file:
+ for row in my_file:
+ if (re.match(r"\+\d{2}\s\d{10}", row)) or (re.match(r"\(\d{2}\)\s\d{10}", row)) or (re.match(r"\d{4}\s\d{10}", row)):
+ print(row,end="")
+
+askFileName() \ No newline at end of file
diff --git a/challenge-110/cristian-heredia/python/ch_2.py b/challenge-110/cristian-heredia/python/ch_2.py
new file mode 100644
index 0000000000..fdf11429fc
--- /dev/null
+++ b/challenge-110/cristian-heredia/python/ch_2.py
@@ -0,0 +1,31 @@
+'''
+ 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 csv
+import sys
+
+
+with open(sys.argv[1], 'r') as my_file:
+ data_CSV = csv.reader(my_file)
+ list_CSV = list(data_CSV)
+result = map(list, zip(*list_CSV))
+for r in result:
+ for j in r:
+ print(j, end= ', ')
+ print() \ No newline at end of file