-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzles.java
More file actions
89 lines (83 loc) · 2.6 KB
/
Puzzles.java
File metadata and controls
89 lines (83 loc) · 2.6 KB
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
package puzzles;
/**
* problems pulled from codingbat.com
*
* @author codingbat.com
*/
public class Puzzles {
/*
F or each of the below method signatures, finish implementing the method
*/
/**==================SUM~28==============/
* Problem statement:
* Given an array of ints,
* return true if the sum of all the 2's
* in the array is exactly 8.
* Example input -> output:
* sum28([2, 3, 2, 2, 4, 2]) → true
* sum28([2, 3, 2, 2, 4, 2, 2]) → false
* sum28([1, 2, 3, 4]) → false
*/
public static boolean sum28(int[] input) {
//TODO
return false;
}
/**==================SUM~28==============/
* Problem statement:
* Given an array of ints, return true if every element is a 1 or a 4.
* Example input -> output:
* only14([1, 4, 1, 4]) → true
* only14([1, 4, 2, 4]) → false
* only14([1, 1]) → true
*/
public static boolean only14(int[] input) {
//TODO
return false;
}
public static void main (String[] args) {
{
System.out.print("sum28: ");
boolean sum28Correct = true;
int[][] testInput = new int[][]{
{2, 3, 2, 2, 4, 2}, //true
{1, 2, 3, 4}, //false
{2}, //false
};
boolean[] output = new boolean[]{
true, false, false
};
for (int i = 0; i < 3; i++) {
if (sum28(testInput[i]) != output[i]) {
System.out.println("FAIL");
sum28Correct = false;
continue;
}
}
if (sum28Correct)
System.out.println("PASS");
System.out.println("--------------");
}
{
System.out.print("only14: ");
boolean only14Correct = true;
int[][] testInput = new int[][]{
{1, 4, 1, 4}, //true
{1, 4, 2, 4}, //false
{1, 1, 1, 5}, //false
};
boolean[] output = new boolean[]{
true, false, false
};
for (int i = 0; i < 3; i++) {
if (sum28(testInput[i]) != output[i]) {
System.out.println("FAIL");
only14Correct = false;
continue;
}
}
if (only14Correct)
System.out.println("PASS");
System.out.println("--------------");
}
}
}