-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework3.java
More file actions
207 lines (171 loc) · 6.03 KB
/
Copy pathHomework3.java
File metadata and controls
207 lines (171 loc) · 6.03 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
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
/* Homework3.java */
public class Homework3 {
/**
* smoosh() takes an array of ints. On completion the array contains
* the same numbers, but wherever the array had two or more consecutive
* duplicate numbers, they are replaced by one copy of the number. Hence,
* after smoosh() is done, no two consecutive numbers in the array are the
* same.
*
* Any unused elements at the end of the array are set to -1.
*
* For example, if the input array is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ],
* it reads [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ] after smoosh()
* completes.
*
* @param ints the input array.
**/
public static void smoosh(int[] ints) {
// Fill in your solution here. (Ours is fourteen lines long, not counting
// blank lines or lines already present in this file.)
int i=0;
while(i<ints.length){
if(ints[i]!=-1){
for(int j=i+1;j<ints.length;j++){
if(ints[i]==ints[j])
ints[j]=-1;
else
break;
}
}else{
}
i++;
}
i=0;
while(i<ints.length){
if(ints[i]==-1){
for(int l=i+1;l<ints.length;l++){
if(ints[l]!=-1){
ints[i]=ints[l];
ints[l]=-1;
break;
}
}
}
i++;
}//while loop
}
/**
* stringInts() converts an array of ints to a String.
* @return a String representation of the array.
**/
private static String stringInts(int[] ints) {
String s = "[ ";
for (int i = 0; i < ints.length; i++) {
s = s + Integer.toString(ints[i]) + " ";
}
return s + "]";
}
/**
* main() runs test cases on your smoosh and squish methods. Prints summary
* information on basic operations and halts with an error (and a stack
* trace) if any of the tests fail.
**/
public static void main(String[] args) {
String result;
int i;
System.out.println("Let's smoosh arrays!\n");
int[] test1 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
System.out.println("smooshing " + stringInts(test1) + ":");
smoosh(test1);
result = stringInts(test1);
System.out.println("MAIN RESULT IS \n "+result);
TestHelper.verify(result.equals(
"[ 3 7 4 5 2 0 8 5 -1 -1 -1 -1 -1 -1 ]"),
"BAD SMOOSH!!! No cookie.");
int[] test2 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
System.out.println("smooshing " + stringInts(test2) + ":");
smoosh(test2);
result = stringInts(test2);
System.out.println(result);
TestHelper.verify(result.equals(
"[ 6 3 6 3 6 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 ]"),
"BAD SMOOSH!!! No cookie.");
int[] test3 = {4, 4, 4, 4, 4};
System.out.println("smooshing " + stringInts(test3) + ":");
smoosh(test3);
result = stringInts(test3);
System.out.println(result);
TestHelper.verify(result.equals("[ 4 -1 -1 -1 -1 ]"),
"BAD SMOOSH!!! No cookie.");
int[] test4 = {0, 1, 2, 3, 4, 5, 6};
System.out.println("smooshing " + stringInts(test4) + ":");
smoosh(test4);
result = stringInts(test4);
System.out.println(result);
TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"),
"BAD SMOOSH!!! No cookie.");
System.out.println("\nLet's squish linked lists!\n");
int[] test5 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
SList list5 = new SList();
for (i = 0; i < test5.length; i++) {
list5.insertEnd(new Integer(test5[i]));
}
System.out.println("squishing " + list5.toString() + ":");
list5.squish();
result = list5.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ 3 7 4 5 2 0 8 5 ]"),
"BAD SQUISH!!! No biscuit.");
int[] test6 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
SList list6 = new SList();
for (i = 0; i < test6.length; i++) {
list6.insertEnd(new Integer(test6[i]));
}
System.out.println("squishing " + list6.toString() + ":");
list6.squish();
result = list6.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ 6 3 6 3 6 3 ]"),
"BAD SQUISH!!! No biscuit.");
int[] test7 = {4, 4, 4, 4, 4};
SList list7 = new SList();
for (i = 0; i < test7.length; i++) {
list7.insertEnd(new Integer(test7[i]));
}
System.out.println("squishing " + list7.toString() + ":");
list7.squish();
result = list7.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ 4 ]"),
"BAD SQUISH!!! No biscuit.");
int[] test8 = {0, 1, 2, 3, 4, 5, 6};
SList list8 = new SList();
for (i = 0; i < test8.length; i++) {
list8.insertEnd(new Integer(test8[i]));
}
System.out.println("squishing " + list8.toString() + ":");
list8.squish();
result = list8.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"),
"BAD SQUISH!!! No biscuit.");
SList list9 = new SList();
System.out.println("squishing " + list9.toString() + ":");
list9.squish();
result = list9.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ ]"),
"BAD SQUISH!!! No biscuit.");
System.out.println("\nLet's twin linked lists!\n");
System.out.println("twinning " + list6.toString() + ":");
list6.twin();
result = list6.toString();
System.out.println(result);
TestHelper.verify(result.equals(
"[ 6 6 3 3 6 6 3 3 6 6 3 3 ]"),
"BAD TWIN!!! No gravy.");
System.out.println("twinning " + list7.toString() + ":");
list7.twin();
result = list7.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ 4 4 ]"),
"BAD TWIN!!! No gravy.");
System.out.println("twinning " + list9.toString() + ":");
list9.twin();
result = list9.toString();
System.out.println(result);
TestHelper.verify(result.equals("[ ]"),
"BAD TWIN!!! No gravy.");
}
}