Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I previously posted a Reservoir-Sampling program Reservoir-Sampling program, which was basically a test version of this one. This is an assignment. In the code below, I use RunTimeException to control when scanner finishes reading data. Reasons for this being:

I previously posted a Reservoir-Sampling program, which was basically a test version of this one. This is an assignment. In the code below, I use RunTimeException to control when scanner finishes reading data. Reasons for this being:

I previously posted a Reservoir-Sampling program, which was basically a test version of this one. This is an assignment. In the code below, I use RunTimeException to control when scanner finishes reading data. Reasons for this being:

Retained original code (was edited after answers)
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Edited code:

import java.io.BufferedInputStream;
import java.util.Scanner;
public class Subset {
 
 public static void main(String[] args) {
 int sampleSizek = Integer.parseInt(args[0]);
 String[] samplearr = new String[sampleSize];String[k];
 int i = 0;
 //String initializestr; the sample from reservoir
 while((str=stdin.readString())!=null && i < samplearr.length){
 sample[i++]arr[i++] = StdIn.readString();str;
 // StdIn : a class in the provided JAR // System.out.println(str);
 }
 try{
 for(; ;(str=stdin.readString())!=null; i++){
 int randomIndexr = (int)(Math.random()*(i + 1));
 if(randomIndexr < sampleSizek){
 sample[randomIndex]arr[r] = StdIn.readString();str;
 }
 //System.out.println(str);
 }
 }catch(RuntimeException e){
 // do nothing}
 }catch(RuntimeException e){ // do nothing } 
 for(String s : samplearr){
 System.out.print(s + " ");
 }
 System.out.println("\n");
 }
}

StdIn.readString():

class stdin{
 private stdin(){}
 private static Scanner sc = new Scanner(new BufferedInputStream(System.in));
 public static String readString(){
 return scannersc.next();
}
}

Edit: to To clear out confusion, iI am posting what the apiAPI and the instructor demands.:

Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}
Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}

Edited code:

public class Subset {
 
 public static void main(String[] args) {
 int sampleSize = Integer.parseInt(args[0]);
 String[] sample = new String[sampleSize];
 int i = 0;
 // initialize the sample from reservoir
 while(i < sample.length){
 sample[i++] = StdIn.readString(); // StdIn : a class in the provided JAR.
 }
 try{
 for(; ; i++){
 int randomIndex = (int)(Math.random()*(i + 1));
 if(randomIndex < sampleSize){
 sample[randomIndex] = StdIn.readString();
 }
 }
 }catch(RuntimeException e){
 // do nothing
 } 
 for(String s : sample){
 System.out.print(s + " ");
 }
 
 }
}

StdIn.readString():

public static String readString(){
 return scanner.next();
}

Edit: to clear out confusion, i am posting what the api and the instructor demands.

Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Subset {
 
 public static void main(String[] args) {
 int k = Integer.parseInt(args[0]);
 String[] arr = new String[k];
 int i = 0;
 String str; 
 while((str=stdin.readString())!=null && i < arr.length){
 arr[i++] = str;
  // System.out.println(str);
 }
 try{
 for(; (str=stdin.readString())!=null; i++){
 int r = (int)(Math.random()*(i + 1));
 if(r < k){
 arr[r] = str;
 //System.out.println(str);
 }
 }
 }catch(RuntimeException e){ // do nothing } 
 for(String s : arr){
 System.out.print(s + " ");
 }
 System.out.println("\n");
 }
}
class stdin{
 private stdin(){}
 private static Scanner sc = new Scanner(new BufferedInputStream(System.in));
 public static String readString(){
 return sc.next();
}
}

To clear out confusion, I am posting what the API and the instructor demands:

Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}
added 1428 characters in body
Source Link
Somjit Nag
  • 185
  • 1
  • 8

Edit: to clear out confusion , i am posting what the api and the instructor demands.

Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}

Edit: to clear out confusion , i am posting what the api and the instructor demands.

Subset client. Write a client program Subset.java that takes a command-line integer k, 
reads in a sequence of N strings from standard input using StdIn.readString(), 
and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. 
You may assume that k = 0 and no greater than the number of string on standard input.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
C BB
G AA
A BB
 CC
% echo A B C D E F G H I | java Subset 3 BB
E BB
F CC
G BB
Your client should use only constant space plus one object either of type Deque or of type RandomizedQueue;
use generics properly to avoid casting and compiler warnings. It should also use time and space proportional to 
at most N in the worst case, where N is the number of strings on standard input. 
(For an extra challenge, use space proportional to k.) It should have the following API.
public class Subset {
 public static void main(String[] args)
}
deleted 14 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
deleted 200 characters in body
Source Link
Somjit Nag
  • 185
  • 1
  • 8
Loading
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 206 characters in body
Source Link
Somjit Nag
  • 185
  • 1
  • 8
Loading
Source Link
Somjit Nag
  • 185
  • 1
  • 8
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /