another kata

This commit is contained in:
2019-11-24 21:31:45 +01:00
parent d668f6fdd4
commit aa223cf1c5

View File

@@ -0,0 +1,54 @@
#include <string>
#include <map>
#include <vector>
#include <iostream>
std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
std::map<char, int> generateAlphabetScores(){
std::map<char, int> result;
for(int i = 0; i<alphabet.length(); i++){
result[alphabet[i]] = i + 1;
}
return result;
}
std::map<char, int> scores = generateAlphabetScores();
int calcScore(std::string word){
int score = 0;
for(auto c: word){
score += scores[c];
}
return score;
}
std::vector<std::string> split(std::string input, std::string delimiter = " "){
ulong pos = 0;
std::vector<std::string> result;
while((pos = input.find(delimiter)) != std::string::npos){
result.push_back(input.substr(0, pos));
input.erase(0, pos + delimiter.length());
}
// Append remaining element!
result.push_back(input);
return result;
}
std::string highestScoringWord(const std::string &str)
{
int score = 0;
std::string bestWord = "";
for(auto word: split(str)){
int newScore = calcScore(word);
if(newScore > score){
score = newScore;
bestWord = word;
}
}
return bestWord;
}
int main(){
std::cout << highestScoringWord("aa b");
}