aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-110/cheok-yin-fung/java/PhoneNumber.java70
-rw-r--r--challenge-110/cheok-yin-fung/java/TransposeFile.java94
-rw-r--r--challenge-110/cheok-yin-fung/perl/ch-1.pl18
-rw-r--r--challenge-110/cheok-yin-fung/perl/ch-2.pl32
4 files changed, 214 insertions, 0 deletions
diff --git a/challenge-110/cheok-yin-fung/java/PhoneNumber.java b/challenge-110/cheok-yin-fung/java/PhoneNumber.java
new file mode 100644
index 0000000000..1dd7bccf60
--- /dev/null
+++ b/challenge-110/cheok-yin-fung/java/PhoneNumber.java
@@ -0,0 +1,70 @@
+// The Weekly Challenge 110
+// Task 1 Valid Phone Numbers
+
+import java.io.File;
+import java.util.Scanner;
+import java.io.FileNotFoundException;
+
+// Usage: java PhoneNumber [file name]
+
+public class PhoneNumber
+{
+ public static void main(String[] args) throws Exception
+ {
+ try {
+ File file = new File(args[0]);
+ Scanner sc = new Scanner(file);
+ while (sc.hasNextLine()) {
+ String num = sc.nextLine();
+ if ( checkHead(num) && checkMid(num) &&
+ num.substring(5).trim().length() == 10 && checkTail(num))
+ System.out.println(num);
+ }
+ } catch (FileNotFoundException e) {
+ System.out.println(e.getMessage());
+ System.exit(0);
+ } catch (IndexOutOfBoundsException e) {
+ System.out.println("Usage: java PhoneNumber [file name]");
+ System.exit(0);
+ }
+ }
+
+ private static boolean checkHead(String tel)
+ {
+ tel = tel.trim();
+ if (tel.charAt(0) == '+'
+ && isNumeric(tel.charAt(1))
+ && isNumeric(tel.charAt(2)) )
+ return true;
+
+ if (tel.charAt(0) == '(' && tel.charAt(3) == ')'
+ && isNumeric(tel.charAt(1))
+ && isNumeric(tel.charAt(2)))
+ return true;
+
+ boolean temp_bl = true;
+ for (int a = 0; a < 4 && temp_bl ; a++)
+ temp_bl = temp_bl && isNumeric(tel.charAt(a));
+ return temp_bl;
+ }
+
+ private static boolean checkMid(String tel)
+ {
+ return tel.charAt(4) == ' ';
+ }
+
+ private static boolean checkTail(String tel)
+ {
+ boolean temp_bl = true;
+ for (int i = 0; i < 10 && temp_bl ; i++)
+ temp_bl = temp_bl && isNumeric(tel.charAt(5+i));
+ return temp_bl;
+ }
+
+ private static boolean isNumeric(char ch)
+ {
+ if (ch >= '0' && ch <= '9') return true;
+ else return false;
+ }
+
+}
diff --git a/challenge-110/cheok-yin-fung/java/TransposeFile.java b/challenge-110/cheok-yin-fung/java/TransposeFile.java
new file mode 100644
index 0000000000..438d070b47
--- /dev/null
+++ b/challenge-110/cheok-yin-fung/java/TransposeFile.java
@@ -0,0 +1,94 @@
+// The Weekly Challenge - 110
+// Task 2 Transpose File
+
+import java.io.File;
+import java.util.Scanner;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+
+// Usage: java TransposeFile [CSV file]
+
+public class TransposeFile
+{
+ private static int maxNumOfColOriginally = 1;
+ private static int numOfRowOriginally;
+ public static void main(String[] args) throws Exception
+ {
+ try {
+ File file = new File(args[0]);
+ Scanner sc = new Scanner(file);
+ ArrayList<ArrayList<String>> original = new ArrayList<>();
+ while (sc.hasNextLine()) {
+ String line = sc.nextLine();
+ ArrayList<String> row_in = new ArrayList<>();
+ row_in = parse(line.trim());
+ original.add(row_in);
+ }
+ numOfRowOriginally = original.size();
+
+ //For testing
+ /* System.out.println("Original File:");
+ for (ArrayList<String> row : original) {
+ for (String item : row)
+ System.out.println(item);
+ System.out.println("");
+ }
+ System.out.println("original rows num:" + numOfRowOriginally);
+ System.out.println("original max col num:" + maxNumOfColOriginally); */
+
+ int maxRow = maxNumOfColOriginally;
+ for (int i=0; i < maxRow; i++) {
+ if (original.get(0).size() > i )
+ System.out.print(original.get(0).get(i));
+ for (int k=1; k < numOfRowOriginally; k++) {
+ if (i < original.get(k).size() )
+ System.out.print("," + original.get(k).get(i));
+ else
+ System.out.print(",");
+ }
+ System.out.println();
+ }
+
+ } catch (FileNotFoundException e) {
+ System.out.println(e.getMessage());
+ System.exit(0);
+ } catch (IndexOutOfBoundsException e) {
+ System.out.println("Usage: java Transpose [CSV file]");
+ System.exit(0);
+ } catch (Exception e) {
+ System.out.println("Error(s) in input file.");
+ }
+ }
+
+ private static ArrayList<String> parse(String into)
+ {
+ ArrayList<String> group = new ArrayList<>();
+ String[] pre_group = into.split(",");
+ for (int j=0, i = 0; i < pre_group.length; ) {
+ j = i;
+ String term = pre_group[i];
+ if (pre_group[i].startsWith("\"") && !pre_group[i].endsWith("\"") ) {
+ do {
+ j = j+1;
+ term += "," + pre_group[j];
+ } while (!pre_group[j].endsWith("\"")) ;
+ group.add(term);
+ i = j+1;
+ } else if (pre_group[i].startsWith("\"") && pre_group[i].length() == 1) {
+ do {
+ j = j+1;
+ term += "," + pre_group[j];
+ } while (!pre_group[j].endsWith("\"")) ;
+ group.add(term);
+ i = j+1;
+ } else {
+ group.add(term);
+ i = i+1;
+ }
+ }
+ if (maxNumOfColOriginally < group.size())
+ maxNumOfColOriginally = group.size();
+ return group;
+ }
+
+}
diff --git a/challenge-110/cheok-yin-fung/perl/ch-1.pl b/challenge-110/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..a036df0b9f
--- /dev/null
+++ b/challenge-110/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+# The Weekly Challenge 110
+# Task 1 Phone Number
+# Usage: perl ch-1.pl < [file]
+use strict;
+use warnings;
+
+my $s;
+while ($s = <STDIN>) {
+ chomp($s);
+ if (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";
+ }
+}
diff --git a/challenge-110/cheok-yin-fung/perl/ch-2.pl b/challenge-110/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..f5b7a432bb
--- /dev/null
+++ b/challenge-110/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+# The Weekly Challenge #110 Task 2
+# Transpose File
+use strict;
+use warnings;
+use Text::CSV_XS qw/csv/;
+
+my $file_name = $ARGV[0];
+
+my $csv_file = csv (in => $file_name, sep_char => ",");
+
+my $old_num_row = scalar @{$csv_file};
+my $old_max_num_col = 1;
+for my $field (@{$csv_file}) {
+ my $len = scalar @{$field};
+ $old_max_num_col = $len if $len > $old_max_num_col;
+}
+
+for my $i (0..$old_max_num_col-1) {
+ print $csv_file->[0]->[$i];
+ for my $k (1..$old_num_row-1) {
+ print ",";
+ my $item = $csv_file->[$k]->[$i];
+ if (defined($item)) {
+ print "\"" if $item =~ /,/;
+ print $item;
+ print "\"" if $item =~ /,/;
+ }
+ }
+ print "\n";
+}
+