Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Use a binary search over the parasol head width 2. Use a method to determine

ID: 3748457 • Letter: 1

Question

1. Use a binary search over the parasol head width 2. Use a method to determine width based on input of n and k I don’t want the program to be written for me or anything, I just need some help(hint) with the two questions I have above. If anyone could just help me determine how I would do 1 and 2 with some thorough explanation. Thank you. If it helps, I am supposed to do this in C.

UCF bought k parasol stands to help cover the outside seating. However, UCF has not purchased any parasol tops nor has determined where to place the parasols. UCF has hired you to help protect their students from the weather. They want to buy k identical parasol tops to cover all the seating. Each chair should be completely covered by at least one parasol. UCF does not want to spend too much money, so you will help determine the minimum integral width of the parasol heads such that all the chairs can be covered. Suppose you have 2 parasols stands, and 4 chairs at positions 1, 4, 7, and 11. We could cover all the chairs with parasol widths of 6. See the following diagram, | However, we could also cover all the chairs with parasol widths of 5. See the following diagram. You can find out that regardless of placement it is not possible to cover all chairs using2 parasols with widths less than 5 Input Specification The first line of input contains a two positive integers, and k, (n, k 200,000) representing the number of chairs and the number of parasol stands. The following line will contain n positive, space separated integers representing the position of the chairs in meters from the student union Note that each chair's position will be given in increasing order

Explanation / Answer

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.PrintStream;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class NameGuesserTest {

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

@Before

public void setUpStreams() {

System.setOut(new PrintStream(outContent));

System.setErr(new PrintStream(errContent));

}

@Test

public void testSMITH() {

//Pass an input into the console and then execute the main method of the program

String input = "N N Y N Y N Y N N N Y Y Y Y D ";

InputStream in = new ByteArrayInputStream(input.getBytes());

System.setIn(in);

NameGuesser.main(null);

String rawOutput = outContent.toString();

String[] splitOutput = rawOutput.split(" ");

assertEquals("When looking at the number of guesses needed, we",15,splitOutput.length-1);

String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","SHANKLIN","SPANO","SIRIANNI","SNAVELY","SLAGTER","SLUSARSKI","SMISEK","SMOLSKI","SMITS","SMITHEY","SMITHEE","SMITH"};

for(int i = 0; i < allGuesses.length; i++) {

assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i]));

}

}

@Test

public void testWHITTEN() {

//Pass an input into the console and then execute the main method of the program

String input = "N N N N Y N N N Y Y N N N Y Y D ";

InputStream in = new ByteArrayInputStream(input.getBytes());

System.setIn(in);

NameGuesser.main(null);

String rawOutput = outContent.toString();

String[] splitOutput = rawOutput.split(" ");

assertEquals("When looking at the number of guesses needed, we",16,splitOutput.length-1);

String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","VESEY","WIKER","WARRING","WERKHOVEN","WHITEHORN","WICKS","WHITTING","WHITLEDGE","WHITSELL","WHITTEMORE","WHITTENTON","WHITTENBECK","WHITTEN"};

for(int i = 0; i < allGuesses.length; i++) {

assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i]));

}

}

@Test

public void testCRAIG() {

//Pass an input into the console and then execute the main method of the program

String input = "Y Y N Y N N N N Y N N N N Y D ";

InputStream in = new ByteArrayInputStream(input.getBytes());

System.setIn(in);

NameGuesser.main(null);

String rawOutput = outContent.toString();

String[] splitOutput = rawOutput.split(" ");

assertEquals("When looking at the number of guesses needed, we",15,splitOutput.length-1);

String[] allGuesses = {"LANGLITZ","DURSO","BURROWS","CREST","CHERNEY","COLLELO","CORNFORTH","COUSER","CRAIGWELL","COWLEY","CRABBE","CRAFTER","CRAGUN","CRAIGER","CRAIG"};

for(int i = 0; i < allGuesses.length; i++) {

assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i]));

}

}

@Test

public void testSINGLETARY() {

//Pass an input into the console and then execute the main method of the program

String input = "N N Y N Y Y N N N N Y Y Y N Y D ";

InputStream in = new ByteArrayInputStream(input.getBytes());

System.setIn(in);

NameGuesser.main(null);

String rawOutput = outContent.toString();

String[] splitOutput = rawOutput.split(" ");

assertEquals("When looking at the number of guesses needed, we",16,splitOutput.length-1);

String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","SHANKLIN","SPANO","SIRIANNI","SHREWSBERRY","SIGNS","SIMMERS","SINGEWALD","SINQUEFIELD","SINISCALCHI","SINGLTON","SINGLER","SINGLETERRY","SINGLETARY"};

for(int i = 0; i < allGuesses.length; i++) {

assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i]));

}

}

@Test

public void testALVEREZ() {

//Pass an input into the console and then execute the main method of the program

String input = "Y Y Y Y Y D";

InputStream in = new ByteArrayInputStream(input.getBytes());

System.setIn(in);

NameGuesser.main(null);

String rawOutput = outContent.toString();

String[] splitOutput = rawOutput.split(" ");

assertEquals("When looking at the number of guesses needed, we",6,splitOutput.length-1);

String[] allGuesses = {"LANGLITZ","DURSO","BURROWS","BELLINGHAM","ASHTON","ALVEREZ"};

for(int i = 0; i < allGuesses.length; i++) {

assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i]));

}

}

else{ //ifname is found, return counter// }

System.out.println("Your name, " +lastNames.get(mid) + ", was found with " + counter + "guesses.");

input.close();

return counter;

}

if(high ==low){ //if upper and lower bounds are equal// }

System.out.println("Is your name: " +lastNames.get(mid) + " ? (Y/N)");

userInput = input.next(); //ask if name isfound

if(userInput.equalsIgnoreCase("Y")){ //ifyes, print success, counter, and return counter// }

System.out.println("Your name, " + lastNames.get(mid) + ",was found with " + counter + " guesses.");

input.close();

return counter;

}

else if(userInput.equalsIgnoreCase("N")){//if no, inform user that guesser failed//}

System.out.println("Name not found. Attempted locating with "+ counter + " guesses");

input.close();

return counter;

}

}

}

input.close();

return counter;

}

}