-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomWord.java
More file actions
29 lines (25 loc) · 840 Bytes
/
RandomWord.java
File metadata and controls
29 lines (25 loc) · 840 Bytes
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
/* *****************************************************************************
* Name: Jaya Mukherjee
* Coursera User ID: ******
* Last modified: March 07, 2021
**************************************************************************** */
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
public class RandomWord {
public static void main(String[] args) {
String champ = null;
int index = 1;
if (!StdIn.isEmpty()) {
champ = StdIn.readString();
}
while (!StdIn.isEmpty()) {
String word = StdIn.readString();
if (StdRandom.bernoulli(1.0 / index)) {
champ = word;
}
index++;
}
StdOut.println(champ);
}
}