-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subset.java
34 lines (31 loc) · 1.19 KB
/
Subset.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
/******************************************************************************
* Name: Nick Barnett
* NetID: nrbarnett
* Precept: P04
*
* Partner Name: N/A
* Partner NetID: N/A
* Partner Precept: N/A
*
* Description: Subset. Takes a command-line integer k; reads in a sequence
* of strings from standard input using StdIn.readString(); and prints out
* exactly k of them, uniformly at random.
******************************************************************************/
import edu.princeton.cs.algs4.StdIn;
public class Subset {
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
RandomizedQueue<String> rq = new RandomizedQueue<String>();
while (!StdIn.isEmpty()) {
// reads in a sequence of strings from standard input
String item = StdIn.readString();
if (item != null) { rq.enqueue(item); }
//enqueue the item into the rq data type
}
for (int i = 0; i < k; i++) {
System.out.println(rq.dequeue());
//the dequeue instance method ensures that the printed items only appear once
//in the output
}
}
}