Created
March 13, 2018 15:17
-
-
Save max-l/0c7eb646b641a8d16985ada1ac1b374f to your computer and use it in GitHub Desktop.
TurquoiseThickJavabytecode created by max_l - https://repl.it/@max_l/TurquoiseThickJavabytecode
This file contains hidden or 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
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.TreeMap; | |
/** | |
* 1. Populate a java.util.TreeMap<String,WordOccurenceCounter> instance, where the key is the word in WordOccurenceCounter | |
* reading the words from ./mobydick.txt file, and print the number of occurrences of the first 100 words in alphabetical order | |
* | |
* Note1: the TreeMap.values() method returns the values in the natural order of the Keys. | |
* | |
* Note2: The text for mobydick.txt should be copied from this link: https://docs.google.com/document/d/1Q-aCHCDj1ZX-KDlS-BBZgSP35iakawDICwv331SLSQU/edit?usp=sharing | |
* | |
* 2.1 Populate a java.util.HashMap<String,WordLineOccurences> instance with the words and line numbers in ./mobydick.txt file, | |
* | |
* 2.2 Populate an ArrayList<WordLineOccurences> with the result of the hashMap's values() method | |
* | |
* 2.3 Implement a comparator (a class that implements Comparator<WordLineOccurences>) that defines a | |
* descending order of WordLineOccurences.occuredInLines.size(). | |
* | |
* 2.4 Use the ArrayList<WordLineOccurences>.sort(...) with your comparator to print the top 100 words with most | |
* occurrences, use the WordLineOccurences.toString() method to print the word followed by the lines on which they occur. | |
* | |
* 3 Implement the missing methods in the BucketHashMap<K,V> class. Use the testBucketHashMap() to | |
* to ensure your implementation is correct. | |
* | |
**/ | |
@SuppressWarnings("serial") | |
class ImplementMeException extends RuntimeException { | |
public ImplementMeException() { | |
super("Implement me and your will learn valuable programming skills !"); | |
} | |
} | |
/** | |
* CHashMap is a minimalist Map implementation | |
*/ | |
interface CHashMap<K,V> { | |
public V lookup(K k); | |
public void put(K k, V v); | |
public void remove(K k); | |
} | |
/** | |
* MyBucketHashMap<K,V> is a partial implementation of CHashMap<K,V> | |
* | |
*/ | |
class MyBucketHashMap<K,V> implements CHashMap<K,V> { | |
class Entry { | |
public final K k; | |
public final V v; | |
public Entry(K k, V v) { | |
this.k = k; | |
this.v = v; | |
} | |
} | |
/* | |
* Buckets are implemented with ArrayList<Entry> | |
*/ | |
private ArrayList<Entry>[] entryBuckets; | |
public MyBucketHashMap(int numberOfBuckets) { | |
// initialize bucket array: | |
entryBuckets = (ArrayList<Entry>[]) new ArrayList<?>[numberOfBuckets]; | |
// fill array with empty buckets: | |
for(int i = 0; i < entryBuckets.length; i++) { | |
entryBuckets[i] = new ArrayList<Entry>(); | |
} | |
} | |
/* | |
* Returns -1 if an Entry e with e.k == theK | |
* */ | |
protected int findIndexOfEntryInBucket(ArrayList<Entry> b, K theK) { | |
for(int i = 0; i < b.size(); i++) { | |
Entry e = b.get(i); | |
if(e.k == theK) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
/* | |
* | |
* Buckets are assigned a slot in the array with : | |
* | |
* k.hashCode() % entryBuckets.length | |
* | |
* */ | |
protected ArrayList<Entry> lookupBucket(K k) { | |
throw new ImplementMeException(); | |
} | |
public V lookup(K k) { | |
throw new ImplementMeException(); | |
} | |
public void remove(K k) { | |
throw new ImplementMeException(); | |
} | |
public void put(K k, V v) { | |
remove(k); | |
throw new ImplementMeException(); | |
} | |
} | |
class WordOccurrenceCounter { | |
public final String word; | |
private int numberOfOccurences = 0; | |
public WordOccurrenceCounter(String word) { | |
this.word = word; | |
} | |
public void incrementNumberOfOccurence() { | |
numberOfOccurences +=1; | |
} | |
public int getNumberOfOccurences() { | |
return numberOfOccurences; | |
} | |
public String toString() { | |
return word +" occured " + numberOfOccurences + " times"; | |
} | |
} | |
/* | |
* WordLineOccurrences holds a word, and a list of all lines in which the word appear. | |
* | |
* */ | |
class WordLineOccurrences { | |
public final String word; | |
private ArrayList<Integer> occuredInLines = new ArrayList<Integer>(); | |
public WordLineOccurrences(String word) { | |
this.word = word; | |
} | |
public void addLineOccurence(int lineNumber) { | |
occuredInLines.add(lineNumber); | |
} | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
for (int l : occuredInLines) { | |
sb.append(","); | |
sb.append(l); | |
} | |
return word +" occured in lines: " + sb.toString(); | |
} | |
} | |
class Main { | |
/* | |
* This method shows how to read words from a text file, use it as a template for exercise 1 and 2 | |
* */ | |
public static void readWords() throws IOException { | |
TreeMap<String, WordOccurrenceCounter> m = new TreeMap<String, WordOccurrenceCounter>(); | |
int lineNumber = 0; | |
for(String l : Files.readAllLines(Paths.get("./mobydick.txt"))) { | |
lineNumber += 1; | |
for(String w: l.split("\\W+")) { | |
//System.out.println("Word "+ w + " encountered on line " + lineNumber); | |
} | |
} | |
int c = 0; | |
for(WordOccurrenceCounter e: m.descendingMap().values()) { | |
c++; | |
System.out.println(e); | |
if(c > 100) break; | |
} | |
} | |
public static void put(MyBucketHashMap<String,Integer> m1, HashMap<String,Integer> m2, String s, Integer i) { | |
m1.put(s, i); | |
m2.put(s, i); | |
} | |
public static void testBucketHashMap() { | |
MyBucketHashMap<String,Integer> m1 = new MyBucketHashMap<String,Integer>(10); | |
HashMap<String,Integer> m2 = new HashMap<String,Integer>(); | |
put(m1,m2, "Bob", 41); | |
put(m1,m2, "Bob", 45); | |
put(m1,m2, "Alice", 54); | |
put(m1,m2, "One", 1); | |
put(m1,m2, "Two", 2); | |
put(m1,m2, "Hello", 456456); | |
put(m1,m2, "World", -456); | |
// test the put method ! | |
if(m1.lookup("Bob") != 45) { | |
throw new RuntimeException("Bob is now 45 !, your put method should *replace* the previous value !"); | |
} | |
// Ensure that m1 behaved exactly like m2 : | |
for(java.util.Map.Entry<String, Integer> e : m2.entrySet()) { | |
String k1 = e.getKey(); | |
Integer i1 = e.getValue(); | |
Integer i2 = m1.lookup(k1); | |
if(i2 == null) { | |
throw new RuntimeException("Expected BucketHashMap to contain a non null value for "+ k1); | |
} | |
if(i2 != i1) { | |
throw new RuntimeException("Expected BucketHashMap to contain value "+ i1 + " for key "+ k1); | |
} | |
} | |
System.out.print("Good work ! BucketHashMap<K,V> behaves exactly like java.util.HashMap<K,V> with the test dataset !"); | |
} | |
public static void main(String[] args) throws Exception { | |
testBucketHashMap(); | |
//readWords(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment