- Upload a working version of ArrayUtility

- Upload a decomplied CSVUtilityGiven
This commit is contained in:
Siwat Sirichai 2020-10-27 12:10:49 +07:00
parent 199c0ead4d
commit 421759516a
16 changed files with 275 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
1st line
2nd line
3rd line
4th line
5th line
6th line
7th line

View File

@ -0,0 +1,11 @@
course-02
Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6
Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5
Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5
Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6
Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4
Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6
Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4
Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4
Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5
Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5
1 course-02
2 Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6
3 Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5
4 Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5
5 Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6
6 Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4
7 Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6
8 Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4
9 Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4
10 Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5
11 Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5

View File

@ -0,0 +1,25 @@
course-04
Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6
Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5
Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5
Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6
Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4
Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6
Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4
Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4
Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5
Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5
Player11,4,6,5,3,5,4,4,3,5,4,2,4,4,5,5,6,3,4
Player12,4,5,5,3,4,5,4,5,4,4,3,6,4,6,4,4,4,4
Player13,6,6,6,3,5,4,6,4,5,5,3,5,3,6,5,4,4,6
Player14,4,6,5,3,5,4,4,4,5,4,4,6,4,6,7,4,4,5
Player15,5,4,5,4,6,6,4,3,5,5,4,5,4,5,5,4,4,5
Player16,5,4,5,3,5,4,5,4,7,8,3,5,4,5,5,4,4,4
Player17,6,4,5,3,5,6,4,3,5,4,3,5,5,9,5,3,3,4
Player18,5,6,5,4,5,6,4,4,5,5,2,5,4,6,7,5,3,7
Player19,4,4,6,4,4,6,4,3,6,7,3,5,4,5,4,4,4,5
Player20,5,4,6,3,4,4,4,3,5,5,3,5,5,6,5,6,4,5
Player21,4,4,7,2,4,5,4,3,5,6,3,7,4,5,4,4,5,4
Player22,6,4,7,4,5,6,5,4,5,5,4,7,4,4,5,5,4,5
Player23,5,4,7,3,5,5,6,4,5,6,3,5,4,6,4,4,3,6
Player24,6,4,6,4,4,4,4,3,5,4,5,5,3,6,5,3,5,5
1 course-04
2 Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6
3 Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5
4 Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5
5 Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6
6 Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4
7 Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6
8 Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4
9 Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4
10 Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5
11 Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5
12 Player11,4,6,5,3,5,4,4,3,5,4,2,4,4,5,5,6,3,4
13 Player12,4,5,5,3,4,5,4,5,4,4,3,6,4,6,4,4,4,4
14 Player13,6,6,6,3,5,4,6,4,5,5,3,5,3,6,5,4,4,6
15 Player14,4,6,5,3,5,4,4,4,5,4,4,6,4,6,7,4,4,5
16 Player15,5,4,5,4,6,6,4,3,5,5,4,5,4,5,5,4,4,5
17 Player16,5,4,5,3,5,4,5,4,7,8,3,5,4,5,5,4,4,4
18 Player17,6,4,5,3,5,6,4,3,5,4,3,5,5,9,5,3,3,4
19 Player18,5,6,5,4,5,6,4,4,5,5,2,5,4,6,7,5,3,7
20 Player19,4,4,6,4,4,6,4,3,6,7,3,5,4,5,4,4,4,5
21 Player20,5,4,6,3,4,4,4,3,5,5,3,5,5,6,5,6,4,5
22 Player21,4,4,7,2,4,5,4,3,5,6,3,7,4,5,4,4,5,4
23 Player22,6,4,7,4,5,6,5,4,5,5,4,7,4,4,5,5,4,5
24 Player23,5,4,7,3,5,5,6,4,5,6,3,5,4,6,4,4,3,6
25 Player24,6,4,6,4,4,4,4,3,5,4,5,5,3,6,5,3,5,5

View File

@ -0,0 +1,59 @@
import java.util.Arrays;
public class ArrayUtility {
/**
* combines a given 2D array into 1D array
* for example,
* { {1, 2}, {3, 4, 5} } -> {1, 2, 3, 4, 5}
* @param d
* @return 1D array of combine all elements from 2D array
*/
public static int[] merge(int[][] d) {
int length = 0;
for(int i = 0;i < d.length;i++)length+=d[i].length;
int[] merged_array = new int[length];
int current_index = 0;
for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) {
merged_array[current_index] = d[i][j];
current_index++;
}
return merged_array;
}
/**
* splits a given 1D array into 2D array with n equal parts
* if the given array cannot be split into n equal part
* this method should return 2D array of size 0,0 (new int[0][0])
* @param d
* @param n number of parts for the target, 0 < n <= d.length
* @return 2D array
*/
public static int[][] split(int[] d, int n) {
if(n<=0) return new int[0][0]; //Return to prevent ArithmeticException: / by zero and negative array index
if (d.length%n!=0)return new int[0][0]; //Return, not array is not splittable
int[][] splitted_array = new int[n][d.length/n]; //Create the splitted_array array and initialize it
for(int i = 0;i < n;i++)for(int j = 0;j<d.length/n;j++)splitted_array[i][j] = d[j*n+i];
return splitted_array;
}
public static void show(String[] x) {
System.out.println(Arrays.toString(x));
}
public static void show(String[][] x2d) {
for (String[] x : x2d) {
show(x);
}
}
public static void show(int[] x) {
System.out.println(Arrays.toString(x));
}
public static void show(int[][] x2d) {
for (int[] x : x2d) {
show(x);
}
}
}

View File

@ -0,0 +1,49 @@
public class ArrayUtilityTest {
public static void main(String[] args) {
testMerge();
testSplit();
}
public static void myTestSplit() {
// add your code and some testcases the test split() similar to testSplit()
}
public static void myTestMerge() {
// add your code and some testcases the test merge() similar to testMerge()
}
// do no modify this method
private static void testSplit() {
int[] c = { 1, 2, 3, 4, 5, 6 };
ArrayUtility.show(ArrayUtility.split(c, 1));
System.out.println("Expected:\n[1, 2, 3, 4, 5, 6]\n----------");
ArrayUtility.show(ArrayUtility.split(c, 2));
System.out.println("Expected:\n[1, 2, 3]\n[4, 5, 6]\n----------");
ArrayUtility.show(ArrayUtility.split(c, 3));
System.out.println("Expected:\n[1, 2]\n[3, 4]\n[5, 6]\n----------");
ArrayUtility.show(ArrayUtility.split(c, 4));
System.out.println("Expected: Array size 6 cannot be splitted into 4 parts\n----------");
ArrayUtility.show(ArrayUtility.split(c, 5));
System.out.println("Expected: Array size 6 cannot be splitted into 5 parts\n----------");
ArrayUtility.show(ArrayUtility.split(c, 6));
System.out.println("Expected:\n[1]\n[2]\n[3]\n[4]\n[5]\n[6]\n----------");
ArrayUtility.show(ArrayUtility.split(c, 0));
System.out.println("Expected: n must be >= 0\n----------");
ArrayUtility.show(ArrayUtility.split(c, -1));
System.out.println("Expected: n must be >= 0\n----------");
ArrayUtility.show(ArrayUtility.split(c, 7));
System.out.println("Expected: n cannot be greater than array's size, 6\n----------");
}
// do not modify this method
private static void testMerge() {
int[][] a = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 }, { 10 } };
ArrayUtility.show(ArrayUtility.merge(a));
System.out.println("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - Expected");
int[][] b = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 }, { 7, 8, 9, 10 }, { 10, 11, 12, 13 } };
ArrayUtility.show(ArrayUtility.merge(b));
System.out.println("[1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13] - Expected");
}
}

View File

@ -0,0 +1,42 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class CSVUtility {
/**
* reads all lines in given filename into an array of String
* @param filename
* @return array of String, each element in array is each line in file
* @throws FileNotFoundException
*/
public static String[] readFile(String filename) throws FileNotFoundException {
// ENTER CODE HERE
Scanner sc = new Scanner(new File(filename));
int length = 0;
for(length = 0;sc.hasNext();length++)sc.nextLine();
String[] filedata = new String[length];
sc = new Scanner(new File(filename));
sc = new Scanner(new File(filename)).useDelimiter(",");
for(int i = 0;i<=length;i++)filedata[i]=sc.next();
return filedata;
}
/**
* read given CSV file into 2D array of String
* @param filename CSV file
* @param header true if CSV file has a header line. The first line is header. The data start from second line.
* @return
* @throws FileNotFoundException
*/
public static String[][] readCSV(String filename, boolean header) throws FileNotFoundException {
// ENTER CODE HERE
return null;
}
}

View File

@ -0,0 +1,51 @@
//
// Source code recreated from a .class file by SNLinter
// (powered by FernFlower decompiler)
//
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class CSVUtilityGiven {
public CSVUtilityGiven() {
}
public static String[] readFile(String var0) throws FileNotFoundException {
Scanner var1 = new Scanner(new File(var0));
ArrayList var2 = new ArrayList();
while(var1.hasNext()) {
var2.add(var1.nextLine());
}
var1.close();
String[] var3 = new String[var2.size()];
for(int var4 = 0; var4 < var3.length; ++var4) {
var3[var4] = (String)var2.get(var4);
}
return var3;
}
public static String[][] readCSV(String var0, boolean var1) throws FileNotFoundException {
String[] var2 = readFile(var0);
int var3 = var1 ? 1 : 0;
String[][] var4 = new String[var2.length - var3][];
int var5 = 0;
for(int var6 = var3; var5 < var4.length; ++var6) {
var4[var5] = var2[var6].split(",");
++var5;
}
return var4;
}
public static void main(String[] var0) throws FileNotFoundException {
ArrayUtility.show(readCSV("course-db.csv", false));
ArrayUtility.show(readCSV("golf-score.csv", false));
}
}

View File

@ -0,0 +1,31 @@
import java.io.FileNotFoundException;
public class CSVUtilityTest {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Expected:");
ArrayUtility.show(CSVUtilityGiven.readFile("data.txt"));
System.out.println("Your result:");
ArrayUtility.show(CSVUtility.readFile("data.txt"));
System.out.println("----------------------------------------");
System.out.println("Expected:");
ArrayUtility.show(CSVUtilityGiven.readCSV("data.txt", false));
System.out.println("Your result:");
ArrayUtility.show(CSVUtility.readCSV("data.txt", false));
System.out.println("----------------------------------------");
System.out.println("Expected:");
ArrayUtility.show(CSVUtilityGiven.readCSV("data.txt", true));
System.out.println("Your result:");
ArrayUtility.show(CSVUtility.readCSV("data.txt", true));
System.out.println("----------------------------------------");
System.out.println("Expected:");
ArrayUtility.show(CSVUtilityGiven.readCSV("course-db.csv", false));
System.out.println("Your result:");
ArrayUtility.show(CSVUtility.readCSV("course-db.csv", false));
System.out.println("----------------------------------------");
System.out.println("Expected:");
ArrayUtility.show(CSVUtilityGiven.readCSV("course-db.csv", true));
System.out.println("Your result:");
ArrayUtility.show(CSVUtility.readCSV("course-db.csv", true));
}
}

Binary file not shown.

View File

@ -1,2 +0,0 @@
module Assignment9 {
}