-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntFilteringApp.java
275 lines (241 loc) · 9.41 KB
/
IntFilteringApp.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package gr.aueb.cf.ch10miniprojects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* Disclaimer : Due to random generation of ints it might not find adequate filtered group on the first run !
*
* Int filtering Application.(Firsts makes a file with 49 random ints).
* Then reads int numbers from that file with values from 1 to 49. Creates all possible combinations of 6 numbers
* and filters them so that they fill the following criteria: 1) Consists of max 4 even numbers. 2) consists of max 4 odd numbers
* 3) consists at most two consecutive numbers. 4) consists at most three numbers with same last digits. 5)Has at most 3 numbers
* in same range of magnitude.
*
* @author Kountouris Panagiotis
*/
public class IntFilteringApp {
public static void main(String[] args) {
int[] arr;
//Filling File with 49 random ints with default append (false). (new ints every time)
messageRandomFill();
try {
fillFileRandomInts();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
//Reading from that file
try {
arr = Arrays.copyOf(readFile() , readFile().length);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
System.out.println("\nBefore sorting:");
printArrayOnConsole(arr);
System.out.println();
System.out.println("\nAfter sorting: ");
printArrayOnConsole(sortMyArray(arr));
System.out.println();
//Filtering and printing
System.out.println("\nGroup of 6 that meet the criteria:");
filterAndPrintSix(arr);
}
/**
* Refills each time from the start the file with 49 random ints. (append = FALSE).
* Prints them also for the user.
* @throws FileNotFoundException Throws file not found exception.
*/
public static void fillFileRandomInts() throws FileNotFoundException {
try (PrintStream randomNumbers = new PrintStream(new FileOutputStream("C:/tmp/randomNumbers.txt"))) {
for(int i = 1; i <= 49; i++) {
randomNumbers.printf(getRandomNumber() + " ");
}
}catch (FileNotFoundException e1){
e1.printStackTrace();
throw e1;
}
}
/**
* Reads ints from a file and returns an array of ints.
* @return the int array.
* @throws FileNotFoundException throws file not found exception.
*/
public static int[] readFile() throws FileNotFoundException {
int[] arr = new int[49];
try (Scanner intFile = new Scanner(new File("C:/tmp/randomNumbers.txt"))) {
for (int i = 0; i < 49; i++) {
if (intFile.hasNextInt()) {
arr[i] = intFile.nextInt();
}
}
}catch(FileNotFoundException e2){
e2.printStackTrace();
throw e2;
}
return arr;
}
/**
* Sorts the array. Returns the array.
* @param arr the array to sort.
* @return the sorted array.
*/
public static int[] sortMyArray(int[] arr) {
Arrays.sort(arr);
return arr;
}
/**
* Groups from the array 6 numbers each time and filters them. Prints the ones that do NOT contain
* 4 even , 4 odd , 4 continuous , 3 with same ending number or 3 in the same range of tens , at the
* console (Standard Output)
* @param arr the array to be examined.
*/
public static void filterAndPrintSix(int[] arr) {
final int EVEN_ODD = 4;
final int CONTINUOUS = 2;
final int IDENTICAL_MOD = 3;
final int SAME_RANGE = 3;
int[] sixArray = new int[6];
for( int i = 0, j = 1 , k = 2 , l = 3 , m = 4 , n = 5; n < arr.length; i++ ,j++ , k++ , l++ , m++ , n++ ){
sixArray[0] = arr[i];
sixArray[1] = arr[j];
sixArray[2] = arr[k];
sixArray[3] = arr[l];
sixArray[4] = arr[m];
sixArray[5] = arr[n];
if(!isEven(sixArray,EVEN_ODD) && (!isOfOdd(sixArray,EVEN_ODD)) && (!isContinuous(sixArray,CONTINUOUS))
&& (!isSameEnding(sixArray,IDENTICAL_MOD)) && (!isSameTen(sixArray,SAME_RANGE)) ){
printArrayOnConsole(sixArray);
}
}
}
/**
* Takes as input an array and an int threshold. Returns true if the array
* contains more even numbers than the given threshold.
* @param arr The given array.
* @param threshold The given threshold.
* @return True if it contains more even ints than the threshold.
*/
public static boolean isEven(int[] arr , int threshold) {
int count = 0;
for (int i : arr) {
if ( i % 2 == 0) count++;
}
return (count > threshold);
}
/**
* Takes as input an array and an int threshold. Returns true if the array
* contains more odd numbers than the given threshold.
* @param arr The given int array.
* @param threshold The given int threshold.
* @return True if it contains more odd ints than the threshold.
*/
public static boolean isOfOdd(int[] arr , int threshold) {
int count = 0;
for(int i : arr) {
if( i % 2 != 0) count++;
}
return (count > threshold);
}
/**
* Takes as input an array and an int threshold. Returns true if the array
* contains more than two continuous numbers than the given threshold (e.g. 13 14 15).
* @param arr The given int array.
* @param threshold The given int threshold.
* @return True if array contains more than two continuous numbers.
*/
public static boolean isContinuous(int[] arr , int threshold) {
int count = 0;
int maxCount = 0;
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] == ((arr[i + 1]) - 1) ){
count++;
if(count >= maxCount){
maxCount = count;
}
}else{
count = 0;
}
}
return (maxCount >= threshold);
}
/**
* Takes as input an array and an int threshold. Returns true if the array contains
* more numbers with the same ending digit than the given threshold.
* @param arr The given int array.
* @param threshold The given int threshold.
* @return True if the array contains more numbers
* with same suffix than the threshold.
*/
public static boolean isSameEnding(int[] arr , int threshold) {
int[] count = new int[10];
boolean maxSameLastDigits = false;
for (int i = 0; i < arr.length; i++) {
count[(arr[i] % 2)] += 1;
}
for (int i : count){
if (i > threshold){ maxSameLastDigits = true;}
}
return maxSameLastDigits;
}
/**
* Takes as input an array and an int threshold. Returns true if the
* array contains more numbers in the same number range of magnitude
* than the given threshold.
* @param arr The given int array.
* @param threshold The given int threshold.
* @return True if the array contains more numbers
* in same magnitude than given threshold.
*/
public static boolean isSameTen(int[] arr , int threshold) {
int[] count = new int[5];
boolean hasInSameRange = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i] >= 0 && arr[i] <= 9){
count[0]++;
}else if(arr[i] >= 10 && arr[i] <= 19){
count[1]++;
}else if(arr[i] >= 20 && arr[i] <= 29){
count[2]++;
}else if(arr[i] >= 30 && arr[i] <= 39){
count[3]++;
}else if(arr[i] >= 40 && arr[i] <= 49){
count[4]++;
}
}
for(int i : count){
if( i > threshold){
hasInSameRange = true;
}
}
return hasInSameRange;
}
/**
* Prints an array on console (std output)
* @param arr The given array.
*/
public static void printArrayOnConsole(int[] arr) {
System.out.println();
for(int i : arr){
System.out.print(i + " ");
}
}
/**
* Generates a random number within the range of 1 and 49.
* @return The random int number.
*/
public static int getRandomNumber(){
return ((int)(Math.random() * 49) + 1);
}
/**
* User message for file writing and file reading.
*/
public static void messageRandomFill() {
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
System.out.println("Filled the .txt file with random 49 int numbers. Proceeds to Reading and sorting. . .");
System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
}
}