-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashCodeReadFile.java
144 lines (80 loc) · 3.49 KB
/
HashCodeReadFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.hashcode;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
public class HashCodeReadFile {
private static String outputFiles = "data/also_big_data.txt";
private static List<Integer> numberOfRotation = new ArrayList<>();
private static List<Integer> pizzaTypes = new ArrayList<>();
private static String [] fileNames = {"data/example_data.in", "data/small_data.in", "data/medium_data.in",
"data/quite_big_data.in", "data/also_big_data.in"};
public static void main(String[] args) throws IOException{
File fr = new File("data/also_big_data.in");
String filePath = fr.getAbsolutePath();
readFiles(filePath);
List<Integer> result = generateValidTypes(numberOfRotation, pizzaTypes);
generatOutPutFile(new File(outputFiles), result);
}
public static List<Integer> generateValidTypes(List<Integer> numberOfRotation, List<Integer> pizzaTypes) {
int maxSlice = numberOfRotation.get(0);
List<Integer> result = new ArrayList<>();
int temp = 0;
for(int row = (pizzaTypes.size()-1); row > -1; row--) {
if((pizzaTypes.get(row) + temp) == maxSlice){
result.add(0,row);
temp = pizzaTypes.get(row) + temp;
}
else if((pizzaTypes.get(row) + temp) < maxSlice) {
result.add(0,row);
temp = pizzaTypes.get(row) + temp;
}
else if((pizzaTypes.get(row)) > maxSlice) {
break;
}
}
return result;
}
public static void readFiles(String fileName) throws IOException{
try{
BufferedReader reader = new BufferedReader(new FileReader( fileName));
String readings;
String firstRead;
firstRead = reader.readLine();
String[] values;
values = firstRead.split(" ");
int max_Slice = Integer.valueOf(values[0]);
int num_of_Pizza = Integer.valueOf(values[1]);
numberOfRotation.add(max_Slice);
numberOfRotation.add(num_of_Pizza);
// create a pizza list
while ((readings = reader.readLine()) != null){
String chars [] = readings.split(" ");
for(String cha : chars){
pizzaTypes.add(Integer.valueOf(cha));
}
}
reader.close();
}catch (FileNotFoundException e){
System.out.println(e);
}catch (IOException e){
System.out.println(e);
}
}
public static void generatOutPutFile(File file, List<Integer> outputValues) throws IOException{
try(Formatter printer = new Formatter(outputFiles)){
printer.format("%d%n", outputValues.size());
System.out.println(outputValues.size());
for(Integer outputdata : outputValues){
printer.format("%d ", outputdata);
System.out.print(outputdata + " ");
}
}catch (IOException e){
System.err.println(e.getMessage());
}
}
}