From 270a7de8aed488e9cffd080da540d734e7d98b1d Mon Sep 17 00:00:00 2001 From: Cris-HD Date: Sun, 2 May 2021 19:51:28 +0200 Subject: Added challenge 110 solution --- challenge-110/cristian-heredia/perl/ch_1.pl | 89 +++++++++++++++++++++++++++ challenge-110/cristian-heredia/python/ch_1.py | 54 ++++++++++++++++ challenge-110/cristian-heredia/python/ch_2.py | 31 ++++++++++ 3 files changed, 174 insertions(+) create mode 100644 challenge-110/cristian-heredia/perl/ch_1.pl create mode 100644 challenge-110/cristian-heredia/python/ch_1.py create mode 100644 challenge-110/cristian-heredia/python/ch_2.py 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 = ) { + 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 -- cgit