more katas

This commit is contained in:
2019-12-09 11:49:52 +01:00
parent aa223cf1c5
commit 97c0e43874
4 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
#include <vector>
#include <iostream>
using namespace std;
vector<int> arrayLeaders(const vector<int>& numbers)
{
std::vector<int> result;
int sum = 0;
for(auto ptr = numbers.end()-1; ptr >= numbers.begin(); ptr--){
if(*ptr > sum)
result.insert(result.begin(), *ptr);
sum += *ptr;
}
return result;
}
int main(){
std::vector<int> temp;
std::vector<int> input = {17,5,3,4};
temp = arrayLeaders(input);
for(auto el: temp){
std::cout << el;
}
}

View File

@@ -0,0 +1,42 @@
#include <algorithm>
#include <cstddef>
#include <string>
#include <iostream>
#include <map>
size_t duplicateCount(const std::string& in); // helper for tests
std::string toLower(const char* input){
std::string temp = input;
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
return temp;
}
size_t duplicateCount(const char* in)
{
// Convert to lower chars
std::string input = toLower(in);
// Start counting for every char
std::map<char, int> my_map;
for(auto character: input){
auto it = my_map.find(character);
if(it != my_map.end()){
it->second += 1;
} else {
my_map[character] = 1;
}
}
// Count how often we have a value higher than 1
int count = 0;
for(auto it: my_map){
if(it.second > 1)
count++;
}
return count;
}
int main(){
std::cout << duplicateCount("TESTtest");
}

View File

@@ -0,0 +1,22 @@
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int maxProduct (vector<int>numbers , int sub_size)
{
// Sort numbers
std::sort(numbers.begin(), numbers.end());
int result = 1;
// Loop over last sub_size elements
for(auto ptr = numbers.end()-1; ptr >= numbers.end() - sub_size; ptr--){
result *= *ptr;
}
return result;
}
int main(){
std::vector<int> numbers = {1,2,3,4,5};
std::cout << maxProduct(numbers, 3);
}

16
Two Sum/main.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include <utility>
#include <vector>
std::pair<std::size_t, std::size_t> two_sum(const std::vector<int>& numbers, int target) {
for(int i = 0; i<numbers.size(); i++){
for(int j = i; j<numbers.size(); j++){
if(numbers[i] + numbers[j] == target && i != j)
return {i, j};
}
}
return {0, 0};
}
int main(){
}