Part One: Rosetta Stone
Map<string, double> kGramsIn(const string& str, int kGramLength) {
/* TODO: Delete this comment and the other lines here, then implement
* this function.
*/
Map<string, double> kG;
double temp1;
int strLength = str.size();
if(kGramLength <= 0) error("wrong");
if(kGramLength > strLength) EXPECT("wrong");
else{
for(int i=100; i<=100+strLength-kGramLength; i++){
string subStr = str.substr(i-100, kGramLength);
if (kG.containsKey(subStr)){
temp1 = kG.get(subStr);
kG.put(subStr, ++temp1);
}else{
kG.put(subStr, 1);
}
}
}
(void) str;
(void) kGramLength;
return {kG};
}
Map<string, double> normalize(const Map<string, double>& input) {
/* TODO: Delete this comment and the other lines here, then implement
* this function.
*/
Map<string, double> normalizedMap;
if (input.isEmpty()) error("wrong");
else {
Vector<string> keys = input.keys();
Vector<double> values = input.values();
double squareAddUp = 0, rootofSquareAddUp = 0;
for (int i = 0; i < values.size(); i++) {
squareAddUp += values[i] * values[i];
}
rootofSquareAddUp = sqrt(squareAddUp);
if(rootofSquareAddUp==0) error("wrong");
for(int i = 0; i < keys.size(); i++){
normalizedMap.put(keys[i], values[i]/rootofSquareAddUp);
}
}
(void) input;
return {normalizedMap};
}
Map<string, double> topKGramsIn(const Map<string, double>& source, int numToKeep) {
/* TODO: Delete this comment and the other lines here, then implement
* this function.
*/
if(numToKeep < 0) error("wrong");
Map<string, double> topK;
PriorityQueue<string> pq;
Vector<string> keys = source.keys();
Vector<double> values = source.values();
string Uncommon, common;
for(int i=0; i<source.size(); i++){
pq.enqueue(keys[i], values[i]);
}
for(int i=0; i<source.size(); i++){
if((pq.size()-numToKeep) > 0){
Uncommon = pq.dequeue();
}else{
common = pq.dequeue();
topK.put(common, source.get(common));
}
}
(void) source;
(void) numToKeep;
return {topK};
}
double cosineSimilarityOf(const Map<string, double>& lhs, const Map<string, double>& rhs) {
/* TODO: Delete this comment and the other lines here, then implement
* this function.
*/
double cosSim = 0;
Vector<string> lhsKeys = lhs.keys(), sameKeys;
if(lhsKeys.size()){
for(int i=0; i<lhsKeys.size(); i++){
if(rhs.containsKey(lhsKeys[i])){
sameKeys.add(lhsKeys[i]);
//sameKeys[sameKeys.size()] = lhsKeys[i];
}
}
}
if(sameKeys.size()){
for(int i=0; i<sameKeys.size(); i++){
cosSim += lhs.get(sameKeys[i]) * rhs.get(sameKeys[i]);
}
}
(void) lhs;
(void) rhs;
return {cosSim};
}
string guessLanguageOf(const Map<string, double>& textProfile,
const Set<Corpus>& corpora) {
/* TODO: Delete this comment and the other lines here, then implement
* this function.
*/
if(corpora.isEmpty()) error("wrong");
double cosSimTemp, maxCosSim = 0;
string languageName;
for(Corpus i : corpora){
cosSimTemp = cosineSimilarityOf(textProfile, i.profile);
if(cosSimTemp > maxCosSim){
maxCosSim = cosSimTemp;
languageName = i.name;
}
}
(void) textProfile;
(void) corpora;
return languageName;
}
Part Two: Rising Tides
Grid<bool> floodedRegionsIn(const Grid<double>& terrain,
const Vector<GridLocation>& sources,
double height) {
/* TODO: Delete this line and the next four lines, then implement this function. */
Grid<bool> floodLand(terrain.numRows(), terrain.numCols(), 0);
Queue<GridLocation> flood;
for(GridLocation i : sources){
if(height >= terrain.get(i)){
flood.enqueue(i);
floodLand.set(i.row, i.col, 1);
}
}
GridLocation temp, temp1;
while(!flood.isEmpty()){
temp = flood.dequeue();
//up
if(terrain.inBounds(temp.row, temp.col + 1) && floodLand.get(temp.row, temp.col + 1) != 1 && (height >= terrain.get(temp.row, temp.col + 1))){
temp1.row = temp.row;
temp1.col = temp.col + 1;
flood.enqueue(temp1);
floodLand.set(temp1.row, temp1.col, 1);
}
//down
if(terrain.inBounds(temp.row, temp.col - 1) && floodLand.get(temp.row, temp.col - 1) != 1 && height >= terrain.get(temp.row, temp.col - 1)){
temp1.row = temp.row;
temp1.col = temp.col - 1;
flood.enqueue(temp1);
floodLand.set(temp1.row, temp1.col, 1);
}
//left
if(terrain.inBounds(temp.row - 1, temp.col) && floodLand.get(temp.row - 1, temp.col) != 1 && height >= terrain.get(temp.row - 1, temp.col)){
temp1.row = temp.row - 1;
temp1.col = temp.col;
flood.enqueue(temp1);
floodLand.set(temp1.row, temp1.col, 1);
}
//right
if(terrain.inBounds(temp.row + 1, temp.col) && floodLand.get(temp.row + 1, temp.col) != 1 && height >= terrain.get(temp.row + 1, temp.col)){
temp1.row = temp.row + 1;
temp1.col = temp.col;
flood.enqueue(temp1);
floodLand.set(temp1.row, temp1.col, 1);
}
}
(void) terrain;
(void) sources;
(void) height;
return {floodLand};
}