Last active
August 29, 2015 14:24
-
-
Save steffiwilson/1e77c151d200f5bbda86 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer Challenge 220 [Easy]: Mangling Sentences
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
//https://www.reddit.com/r/dailyprogrammer/comments/3aqvjn/20150622_challenge_220_easy_mangling_sentences/ | |
class manglingSentences { | |
public static void main(String[] args) { | |
//proof of concept for word sorter - punctuation stays in place, letters are alphabetized, uppercase positions preserved | |
//System.out.println(wordSorter(stringToArray("Q^azC-;%bDEn"))); | |
String inputSentenceTest = "The quick brown fox jumps over the lazy dog."; | |
String inputSentence1 = "This challenge doesn't seem so hard."; | |
String inputSentence2 = "There are more things between heaven and earth, Horatio, than are dreamt of in your philosophy."; | |
String challengeInput1 = "Eye of Newt, and Toe of Frog, Wool of Bat, and Tongue of Dog."; | |
String challengeInput2 = "Adder's fork, and Blind-worm's sting, Lizard's leg, and Howlet's wing."; | |
String challengeInput3 = "For a charm of powerful trouble, like a hell-broth boil and bubble."; | |
System.out.println(sentenceMangler(inputSentence1)); | |
System.out.println(sentenceMangler(inputSentence2)); | |
System.out.println(sentenceMangler(challengeInput1)); | |
System.out.println(sentenceMangler(challengeInput2)); | |
System.out.println(sentenceMangler(challengeInput3)); | |
} | |
public static String sentenceMangler(String sentence) { | |
String[] splitSentence = sentence.split(" "); | |
String mangledSentence = ""; | |
for (int i = 0; i < splitSentence.length; i++) { | |
mangledSentence = mangledSentence + wordSorter(stringToArray(splitSentence[i])) + " "; | |
} | |
return mangledSentence; | |
} | |
public static String wordSorter(char[] input) { | |
for (int i = 0; i < input.length; i++) { | |
if (Character.isLetter(input[i])) { | |
char smallestFound = input[i]; | |
int indexOfSmallest = i; | |
//find smallest element in the array that comes after the current one | |
for (int j = i; j < input.length; j++) { | |
if ((Character.toLowerCase(input[j]) < Character.toLowerCase(smallestFound)) && Character.isLetter(input[j])) { | |
smallestFound = input[j]; | |
indexOfSmallest = j; | |
} | |
} | |
//swap smallestFound and the current index input[i] | |
//preserve capitalization of both | |
if (smallestFound != input[i]) { //only swap if input[i] is not already in the correct position | |
if (Character.isUpperCase(smallestFound) && Character.isLowerCase(input[i])) { | |
input[indexOfSmallest] = Character.toLowerCase(smallestFound); | |
input[i] = Character.toUpperCase(input[i]); | |
input = swap(input, i, indexOfSmallest); | |
} | |
else if (Character.isLowerCase(smallestFound) && Character.isUpperCase(input[i])) { | |
input[indexOfSmallest] = Character.toUpperCase(smallestFound); | |
input[i] = Character.toLowerCase(input[i]); | |
input = swap(input, i, indexOfSmallest); | |
} | |
else { | |
input = swap(input, i, indexOfSmallest); | |
} | |
} | |
} //end if that places a character if the current character is not punctuation | |
} //end for, input[] now has the correctly sorted array | |
String sortedWord = ""; | |
for (int i = 0; i < input.length; i++) { | |
sortedWord = sortedWord + input[i]; | |
} | |
return sortedWord; | |
} | |
public static char[] swap(char[] array, int index1, int index2) { | |
char temp = array[index1]; | |
array[index1] = array[index2]; | |
array[index2] = temp; | |
return array; | |
} | |
public static char[] stringToArray(String input) { | |
char[] output = new char[input.length()]; | |
for (int i = 0; i < input.length(); i++) { | |
output[i] = input.charAt(i); | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment