Skip to content

Instantly share code, notes, and snippets.

@ClassCubeGists ClassCubeGists/LeftOverSpaces.java Secret
Last active Jun 23, 2017

Embed
What would you like to do?
Solution for 2016 AP FRQ StringFormmater
public static int leftoverSpaces( List<String> wordList, int formattedLen ) {
/* Implementation not shown */
return formattedLen - totalLetters( wordList ) - ( basicGapWidth( wordList, formattedLen ) * wordList.size() - 1 );
}
public static int totalLetters( List<String> wordList ) {
/* Part A */
int let = 0;
for ( String word : wordList ) {
let += word.length();
}
return let;
}
public static int basicGapWidth( List<String> wordList, int formattedLen ) {
/* Part B */
return ( formattedLen - totalLetters( wordList ) ) / ( wordList.size() - 1 );
}
public static String format( List<String> wordList, int formattedLen ) {
/* Part C */
int width = basicGapWidth( wordList, formattedLen );
int leftoverRemaining = leftoverSpaces( wordList, formattedLen );
String formatted = "";
for ( int i = 0; i < wordList.size() - 1; i++ ) {
formatted += wordList.get( i );
for ( int s = 1; s <= width; s++ ) {
formatted += " ";
}
if ( leftoverRemaining > 0 ) {
formatted += " ";
leftoverRemaining--;
}
}
formatted += wordList.get( wordList.size() - 1 );
return formatted.trim();
}
public class StringFormatter {
public static int totalLetters( List<String> wordList ) {
/* Part A */
}
public static int basicGapWidth( List<String> wordList, int formattedLen ) {
/* Part B */
}
public static int leftoverSpaces( List<String> wordList, int formattedLen ) {
/* Implementation not shown */
}
public static String format( List<String> wordList, int formattedLen ) {
/* Part C */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.