Last active
November 28, 2018 18:30
-
-
Save CompSciRocks/31bcbecac962228883c4bf15d3963e07 to your computer and use it in GitHub Desktop.
Working solution to the 2016 AP Computer Science Free Response Question String Formatter - https://compsci.rocks/string-formatter-solution/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int leftoverSpaces( List<String> wordList, int formattedLen ) { | |
/* Implementation not shown */ | |
return formattedLen - totalLetters( wordList ) - ( basicGapWidth( wordList, formattedLen ) * wordList.size() - 1 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int totalLetters( List<String> wordList ) { | |
/* Part A */ | |
int let = 0; | |
for ( String word : wordList ) { | |
let += word.length(); | |
} | |
return let; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int basicGapWidth( List<String> wordList, int formattedLen ) { | |
/* Part B */ | |
return ( formattedLen - totalLetters( wordList ) ) / ( wordList.size() - 1 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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