-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntSet.java
executable file
·226 lines (185 loc) · 4.5 KB
/
IntSet.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
import java.util.ArrayList;
public class IntSet {
public static void main(String[] args) {
IntSet devin = new IntSet(100);
System.out.println(devin.contains(30));
devin.add(30);
System.out.println(devin.contains(30));
//Contains All Test Code
IntSet kenny = new IntSet(100);
IntSet david = new IntSet(100);
IntSet franzese = new IntSet(100);
kenny.add(1);
kenny.add(11);
kenny.add(21);
kenny.add(31);
franzese.add(1);
franzese.add(11);
david.add(22);
System.out.println(kenny.containsAll(franzese));
System.out.println(kenny.containsAll(david));
//
//retainAll test code
IntSet isha1 = new IntSet(10);
isha1.add(1);
isha1.add(2);
isha1.add(3);
isha1.add(4);
IntSet isha2 = new IntSet(10);
isha2.add(1);
isha2.add(3);
isha2.add(5);
isha2.add(7);
isha1.retainAll(isha2);
isha1.print();
//containsPrimeFactors test code
IntSet henry = new IntSet(6);
henry.add(2);
henry.add(5);
henry.add(3);
System.out.println("containsPrime Factors test -- should be true, it is: " + henry.containsPrimeFactors(30));
}
boolean[] arr;
public IntSet(int size) {
arr = new boolean[size];
}
void add(int i) {
arr[i] = true;
}
void remove(int i) {
arr[i] = false;
}
boolean contains(int i) {
return arr[i];
}
//BULK FUNCTIONS:
/*
returns true if s is a subset of set
*/
boolean containsAll(IntSet s) {
for (int i = 0; i < s.arr.length; i++) {
if(s.contains(i) && !this.contains(i)){
return false;
}
}
return true;
}
/*
add all items in set s to this set.
*/
void addAll(IntSet s) {
for (int i = 0; i < arr.length; i++) {
if(s.contains(i)) this.add(i);
}
}
/*
remove all items from this set that are not in set s (intersection)
TODO:
*/
void retainAll(IntSet s) {
ArrayList<Integer> intersection = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (!(s.contains(i))) {
arr[i]=false;
}
}
}
/*
remove all items in s from this set
*/
void removeAll(IntSet s) {
for(int i= 0; i< arr.length; i ++) {
if (s.contains(i)) this.remove(i);
}
}
/*
increment every number in the set by 1
*/
void incrementAll(IntSet s) {
for (int i = arr.length-1; i > 0; i--) {
if (arr[i-1] == true) arr[i] = true;
else arr[i] = false;
}
arr[0] = false;
if (arr[arr.length-1]){
boolean[] newArr = new boolean[arr.length+1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
newArr[newArr.length-1] = true;
arr = newArr;
}
}
/*
for any number in set s that is in this set, multiply this set by multiplier
*/
void multiplyAll(IntSet s, int multiplier) {
}
/*
does the set contain all of the prime factors of number
*/
boolean containsPrimeFactors(int number) {
IntSet primeFactors = new IntSet(number);
int i=2;
while(number>1) {
if(number%i == 0) {
primeFactors.add(i);
number=number/i;
} else {
i++;}
}
if(containsAll(primeFactors)){
return true;
} else {
return false;
}
}
/*
all the subsets that can be generated of subSetSize size
*/
IntSet[] subSets(int subSetSize) {
return null;
}
/*
the subSetSize largest items
*/
IntSet maxSubSet(int subSetSize) {
return null;
}
/*
return an array of two sets,
the set of numbers above divide, and those below
*/
IntSet[] discrete(int divide) {
return null;
}
/*
finds this intersection of this and all sets in the array
*/
boolean containsAll(IntSet[] s) {
return false;
}
/*
print the set pretty like.
*/
void print() {
}
/*
finds the gcd of the set
*/
int gcd() {
return 0;
}
/*
finds the mean of the set
*/
int mean() {
return 0;
}
/*
returns a set with only the bottom n items in the set
*/
IntSet bottom(int n) {
return null;
}
}