EarSketch Asg. 8.1 Text File Input and Splitting This exercise loads the clipAnd
ID: 3603361 • Letter: E
Question
EarSketch Asg. 8.1 Text File Input and Splitting This exercise loads the clipAndMeasures.txt file below using the importFileO function. clipAndMeasures.txt file HIPHOP_DUSTYGROOVE 018:1:7 HIPHOP_DUSTYMOOG_001:2:7 HIPHOP_DUSTYGUITAR_001:4:7 Modify the code below to add track 2 from the second line of the text file. 1 from earsketch import * 3 init( 4 setTempo(100) 6 fileData importFile("http://earsketch.gatech.edu/itec2120/data/clipAndMeasures.txt") 8 lines = fileData . split(" ") 10 track=1 11 first_line lines[0]Explanation / Answer
public class HashTableDoubleHashing
{
/// to store the data
private DataItem array[];
// to store the number of items added
private int itemsInTable;
// to store the number of comparisons used in finding a string
private int numComparisons;
public HashTableDoubleHashing(int size)
{
array = new DataItem[ size ];
// set all entries to null
for( int i=0; i<array.length; i++)
array[i] = null;
// set items added to 0
itemsInTable = 0;
// reset num comparisons
resetNumberComparisons();
}
/// method to insert data into this hash table
public void insert(DataItem data)
{
int key = getHashKey(data.value);
insert(key, data);
// increment the number ofitems added
itemsInTable++;
}
/// to insert the data item at this key position
private void insert(int key, DataItem value)
{
int i = 1;
int k = key;
// find an empty place using linear probing
while( array[k] != null )
{
// get next location
k = getNextIndexDoubleHashing(key,value.value,i);
//System.out.println(" k : " + k);
//System.out.println("in double hashing while");
/// increment i
i++;
}
// insert the value at this location
array[ k ] = value;
}
/// method to generate the has key - this is the hash code function
private int getHashKey(String s)
{
/* using the standard djb2 algorithm*/
int hash = 5381;
for( int i=0; i<s.length(); i++)
{
int k = s.charAt(i);
hash = (hash * 33 + k)%array.length;
}
return (hash % array.length);
}
//the other hash function
private int getHashKey_2(String s)
{
int total = 0;
for( int i=0; i<s.length(); i++)
{
int k = s.charAt(i);
k = (k*k)%array.length;
total = (total + k)%array.length;
}
return (total % array.length);
}
/// to get next index using linear probing
private int getNextIndexDoubleHashing(int key,String s, int i)
{
return (key + i*getHashKey_2(s))%array.length;
}
/// get the number of items
public int getItemsInTable() {
return itemsInTable;
}
// search for an entry in the array
public DataItem search(String searchItem)
{
searchItem = searchItem.toLowerCase();
if( itemsInTable == 0 )
return null;
/// before every search - rest num of comparisons
resetNumberComparisons();
/// hash key for this
int key = getHashKey(searchItem);
int i = 1;
int k = key;
while( !searchItem.equalsIgnoreCase(array[k].value))
{
numComparisons++;
// get next location
k = getNextIndexDoubleHashing(key,searchItem,i);
/// increment i
i++;
}
return array[k];
}
// to get num comparisons
public int getNumComparisons() {
return numComparisons;
}
// to rest the number of hits tried to find the search string
public void resetNumberComparisons()
{
numComparisons = 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.