Created
April 15, 2017 20:39
-
-
Save eduzol/376c68b07d2d67d4ea973c5b65146d6c to your computer and use it in GitHub Desktop.
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
class Pramp { | |
public static void main(String[] args) { | |
String doc = "practice makes perfect. get perfect by practice. just practice!"; | |
Pramp solution = new Pramp(); | |
List<Ocurrence> ocurrences = solution.process(doc); | |
//testing | |
//unique , counts, sort descending order | |
// alphanumeric | |
//hashmap (word, number of ocurrence) | |
//iterate through hashmap. keySet <- | |
//List <Ocurrence > | |
/* | |
class Ocurrence { | |
String word; | |
Integer count; //<-- sort key | |
} | |
*/ | |
//sort bubbleSort , | |
} | |
//O(n) | |
public List<Ocurrence> process(String doc){ | |
HashMap<String, Integer> map = new HashMap<String, Integer>(); | |
List<Ocurrence> ocurrences = new ArrayList<Ocurrence>(); | |
//tokenize | |
List<String> words = doc.split(' '); | |
// [practice, makes , perfect, get, perfect, by, practice., just, practice!] | |
//reduceToAlphaNum | |
//populate hashamp | |
//O(n) | |
for ( String word : words ){ | |
String w = reduceToAlphanumeric(word); //pracitce! -> practice | |
Integer wordCount = map.get(w);//check if entry is already in dictionary | |
if ( wordCount == null ){ | |
//first time in hashmap | |
map.put(w, 1);//practice:1 | |
}else{ | |
//there is already a word entry | |
map.put(w, ++wordCount); //practice: 3 | |
} | |
} | |
//unsorted: | |
//[ practice: 3, makes:1, perfect:2, get:1, by:1 ] | |
//O(n) | |
for( KeySet entry : map.keySet() ){ // {practice:3} | |
Ocurrence ocurrence = new Ocurrence(); | |
ocurrence.setWord(entry.getKey()); | |
ocurrence.setCount(map.get(entry.getKey())); | |
ocurrences.add(ocurrence); | |
} | |
// sort List | |
//Log(n) | |
Arrays.Sort( ocurrences ); | |
return ocurrences; | |
} | |
public class Ocurrence implements Comparable { | |
public String word; | |
public Integer count; //<-- sort key | |
//getter setter | |
public int compareTo(Ocurrence other) { | |
return other.count - this.count; | |
} | |
} | |
//public boolean isAlphaNumeric(String word){} | |
// reduceToAlphaNum(String s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment