CY's Take on PWC#110
If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).
Do tell me, if I am wrong or you strongly oppose my statements!
Well, I wake up early and get some time to blog about The Weekly Challenge again (in addition, correct some parts of my submitted code ‐ a brain with good rest well spots bugs!).
Both Perl and (guest language) Java codes for the two tasks have been done this week.
Task 1: Valid Phone Number
File file = new File(args[0]); Scanner sc = new Scanner(file); while (sc.hasNextLine()) { // blah blah blah }
- Check the first 4 characters;
- check the character in between regional calling codes and the latter codes is an empty space character;
- finally check the last 10 characters are digits.
Perl codes:
while ($s = <STDIN>) {
chomp($s);
if ( length $s == 15 &&
substr($s, 4, 1) eq " " &&
substr($s, -11) =~ / \d{10}/ &&
(substr($s,0,4) =~ /\d{4}/ ||
substr($s,0,4) =~ /\+\d{2}/ ||
substr($s,0,4) =~ /\(\d{2}\)/)
) {
print $s, "\n";
}
}
if ( checkHead(num) && checkMid(num) && num.substring(5).trim().length() == 10 && checkTail(num)) System.out.println(num); // blah blah blah private static boolean isNumeric(char ch) { if (ch >= '0' && ch <= '9') return true; else return false; }
Task 2: Transpose File
task2_str2.csv """",",",char ok,pdl,int cu,uml,str
task2_str4.csv """",okay,char ok,pdl,int cu,uml,str
task2_ex2.csv (when the number of fields in each row is not the same) name,age,sex Mohammad,45,m,n Joe,20,m Julie,35,f Cristina,10,f
Expected Output name,Mohammad,Joe,Julie,Cristina age,45,20,35,10 sex,m,m,f,f ,n,,,
my $item = $_[0];
if (defined($item)) {
print "\"" if $item =~ /,/;
print $item;
print "\"" if $item =~ /,/;
}
}
# the example from task statement name,Mohammad,Joe,Julie,Cristina age,45,20,35,10 sex,m,m,f,f # task2_str2.csv ",ok,cu ",",pdl,uml char,int,str # task2_str4.csv ",ok,cu okay,pdl,uml char,int,str # task2_ex2.csv name,Mohammad,Joe,Julie,Cristina age,45,20,35,10 sex,m,m,f,f ,n,,,
# task2_str2.csv """",ok,cu
",",pdl,uml
char,int,str
# task2_str4.csv """",ok,cu okay,pdl,uml char,int,str
Oooops... it seems that the """" for the output is better. See RFC 4180 Section 2, point 7. ...
Stay alert and healthy! □
Leave a comment