Created
October 20, 2009 22:59
-
-
Save jarodl/214702 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
#include <iostream> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
#define DAYS_IN_YEAR 360 // In order to simplify the program | |
#define DAYS 30 // use 30 days in each month. | |
#define MONTHS 12 // This still gives a rough estimate on how | |
// well the hashing functions work. | |
int hash1(int month, int day); | |
int hash2(int month, int day); | |
string formatDate(int month, int day); | |
int main() | |
{ | |
// for hash1 | |
string year1[DAYS_IN_YEAR]; | |
// for hash2 | |
string year2[DAYS_IN_YEAR]; | |
int numOfDays = 0; | |
int collisions1 = DAYS_IN_YEAR; | |
int collisions2 = DAYS_IN_YEAR; | |
// clear both arrays | |
for ( int i = 0; i < DAYS_IN_YEAR; i++ ) | |
year1[i] = year2[i] = ""; | |
for ( int i = 1; i <= MONTHS; i++ ) { | |
for ( int j = 1; j <= DAYS; j++ ) { | |
year1[ hash1(i, j) ] = formatDate(i, j); | |
year2[ hash2(i, j) ] = formatDate(i, j); | |
} | |
} | |
// total collisions for hash1 | |
for ( int i = 0; i < DAYS_IN_YEAR; i++ ) { | |
if ( year1[i] != "" ) | |
numOfDays += 1; | |
} | |
collisions1 -= numOfDays; | |
numOfDays = 0; | |
// total collisions for hash2 | |
for ( int i = 0; i < DAYS_IN_YEAR; i++ ) { | |
if ( year2[i] != "" ) | |
numOfDays += 1; | |
} | |
collisions2 -= numOfDays; | |
//for ( int i = 0; i < DAYS_IN_YEAR; i++ ) { | |
// cout << "index: " << i << '\t' << year1[i] << endl; | |
cout << "hash1" << '\t' << "hash2" << endl | |
<< "-------" << '\t' << "-------" << endl | |
<< collisions1 << '\t' << collisions2 << endl; | |
return 0; | |
} | |
int hash1(int month, int day) | |
{ | |
return ( ( month + (3 * day) ) % 101 ); | |
} | |
int hash2(int month, int day) | |
{ | |
return ( ( day + ( 3 * month ) ) % 101 ); | |
} | |
string formatDate(int month, int day) | |
{ | |
stringstream ss; | |
ss << month << "/" << day; | |
return ( ss.str() ); | |
} |
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
#include <iostream> | |
#include <string> | |
using namespace std; | |
int hash( string value ); | |
int main() | |
{ | |
string words[10]; | |
string w[] = { "AN", "AS", "AT", "BE", "DO", "GO", "HE", | |
"IF", "IS", "IT", "ME", "NO", "ON", "SO", "TO", "WE" }; | |
string w2[] = { "AN", "AS", "AT", "BE", "DO", "GO", "HE", | |
"IF", "IS", "IT", "ME", "NO", "ON", "SO", "TO", "WE", | |
"AD", "AM", "AX", "BY", "HI", "ID", "IN", "MA", "MY", | |
"OF", "OR", "OX", "PA", "PI", "UP", "US" }; | |
int words2count[10]; | |
for ( int i = 0; i < 10; i++ ) | |
words2count[i] = 0; | |
string states[10]; | |
int statecount[10]; | |
for ( int i = 0; i < 10; i++ ) | |
statecount[i] = 0; | |
string s[] = { "AL", "AK", "AZ", | |
"AR", "CA", "CO", "CT", "DE", "FL", "GA", | |
"HI", "ID", "IL", "IN", "IA", "KS", "KY", | |
"LA", "ME", "MD", "MA", "MI", "MN", "MS", | |
"MO", "MT", "NE", "NV", "NH", "NJ", "NM", | |
"NY", "NC", "ND", "OH", "OK", "OR", "PA", | |
"RI", "SC", "SD", "TN", "TX", "UT", "VT", | |
"VA", "WA", "WV", "WI", "WY" }; | |
// 3a. | |
//for ( int i = 0; i < 10; i++ ) | |
//words[ hash( w[i] ) ] = w[i]; | |
//for ( int i = 0; i < 10; i++ ) | |
//cout << words[i] << endl; | |
// 3b. | |
//for ( int i = 0; i < 32; i++ ) { | |
//int index = hash( w2[i] ); | |
//if ( index < 10 ) { | |
//int tmp = words2count[ index ]; | |
//words2count[ index ] = tmp + 1; | |
//} | |
//} | |
//for ( int i = 0; i < 10; i++ ) | |
//cout << words2count[i] << endl; | |
// | |
for ( int i = 0; i < 50; i++ ) | |
{ | |
states[ hash( s[i] ) ] = s[i]; | |
statecount[ hash( s[i] ) ] += 1; | |
} | |
for ( int i = 0; i < 10; i++ ) | |
cout << states[i] << endl; | |
for ( int i = 0; i < 10; i++ ) | |
cout << statecount[i] << endl; | |
} | |
int hash( string value ) | |
{ | |
int first = int(value[0]) - 64; | |
int second = int(value[1]) - 64; | |
return ( 2 * ( first ) + 7 * ( second ) ) % 11; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment