Skip to content

Instantly share code, notes, and snippets.

@alexa984
Last active May 15, 2019 12:25
Show Gist options
  • Save alexa984/b2d7ce7b3ccbab47e5e799272983bee7 to your computer and use it in GitHub Desktop.
Save alexa984/b2d7ce7b3ccbab47e5e799272983bee7 to your computer and use it in GitHub Desktop.
Programming-101-Python-2019-application-tasks
#include"task_1.h"
int main()
{
unordered_map<string, int> inputs;
string line;
while (getline(cin, line))
{
if (line.empty())break;
else
{
string uuid;
int value;
int pos = line.find(" ");
uuid = line.substr(0, pos);
line.erase(0, pos + 1);
value = stoi(line);
insert(inputs, uuid, value);
}
}
cout<<calc_top_three(inputs);
}
#pragma once
#include<iostream>
#include<unordered_map>
#include<set>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
//custom comparator for set of pairs, ordered by second value.
//The pairs with equal second values are ordered alphabetically by first value
struct Comparator
{
bool operator()(const pair <string, int>& lhs, const pair<string, int>& rhs) const
{
if(lhs.second != rhs.second) return lhs.second > rhs.second;
else return lhs.first < rhs.first;
}
};
void insert(unordered_map<string, int> &map, string uuid, int value)
{
unordered_map<string, int>::iterator i = map.find(uuid);
if (i != map.end()) //if the key already exists, sum up our value with the one of the alredy existing
{
i->second += value;
}
else
{
map.insert({ uuid, value });
}
}
string calc_top_three(unordered_map<string, int> input)
{
set<pair<string, int>, Comparator> set_of_output(input.begin(), input.end(), Comparator());
unordered_map<string, int>::iterator it;
//copy to set in order to facilitate sorting for the ouptut
for (it = input.begin(); it != input.end(); ++it)
{
string key = (*it).first;
int value = (*it).second;
set_of_output.insert({key, value});
}
//output to buffer
ostream stream(nullptr);
stringbuf output;
stream.rdbuf(&output);
set<pair<string, int>, Comparator>::iterator i = set_of_output.begin();
int counter = 0;
vector<pair<string, int>> current;
//take top 3
while (counter<3 && i != set_of_output.end())
{
if (!current.empty() && current.back().second != (*i).second)
{
counter++;
vector<pair<string, int>>::iterator p;
int val = (*current.begin()).second;
stream << (*current.begin()).first;
for (p = current.begin()+1; p != current.end(); ++p) stream << ", " << (*p).first;
stream<<": "<< val << endl;
current.clear();
}
current.push_back(*i);
++i;
if (i == set_of_output.end() && counter < 3)
{
vector<pair<string, int>>::iterator p;
int val = (*current.begin()).second;
stream << (*current.begin()).first;
for (p = current.begin() + 1; p != current.end(); ++p) stream << ", " << (*p).first;
stream << ": " << val << endl;
}
}
return output.str();
}
#include"task_2.h"
int main()
{
int num;
set<int> numbers;
//read input
while(true)
{
string line;
getline(cin, line);
if (!line.empty())
{
istringstream iss(line);
while (iss >> num) numbers.insert(num);
}
else break;
}
//print
cout<<printIntervals(numbers);
}
#pragma once
#include <iostream>
#include<string>
#include<sstream>
#include<set>
using namespace std;
string printIntervals(set<int> ordered_numbers)
{
ostream stream(nullptr);
stringbuf output;
stream.rdbuf(&output);
//calculate intervals
int lower_bound = *ordered_numbers.begin();
int upper_bound = *ordered_numbers.begin();
int size_of_interval = 0;
set<int>::iterator it;
for (it = ordered_numbers.begin(); it != ordered_numbers.end(); ++it)
{
if (*it - 1 == upper_bound || it == ordered_numbers.begin())
{
upper_bound = *it;
size_of_interval++;
}
else
{
stream << "[" << lower_bound << ", " << upper_bound << "] with consecutive count of "
<< size_of_interval << endl;
lower_bound = *it;
upper_bound = *it;
size_of_interval = 1;
}
}
//the last interval
stream << "[" << lower_bound << ", " << upper_bound << "] with consecutive count of "
<< size_of_interval << endl;
return output.str();
}
#include"gtest/gtest.h"
#include"../task_1/task_1.h"
TEST(task_1, outputTest_1)
{
unordered_map<string, int> inputs;
insert(inputs, "8239b961379a4f9f854fd19d82b56dc9", 24);
insert(inputs, "8239b961379a4f9f854fd19d82b56dc9", 39);
insert(inputs, "8239b961379a4f9f854fd19d82b56dc9", 39);
insert(inputs, "8239b961379a4f9f854fd19d82b56dc9", 18);
insert(inputs, "8239b961379a4f9f854fd19d82b56dc9", 25);
insert(inputs, "533cc20dc02647cb98c9cc534112e092", 66);
insert(inputs, "533cc20dc02647cb98c9cc534112e092", 12);
ASSERT_EQ(calc_top_three(inputs), "8239b961379a4f9f854fd19d82b56dc9: 145\n533cc20dc02647cb98c9cc534112e092: 78\n");
}
TEST(task_1, outputTest_2)
{
unordered_map<string, int> inputs;
insert(inputs, "f", 1);
insert(inputs, "g", 2);
insert(inputs, "f", 3);
insert(inputs, "h", 10);
insert(inputs, "f", 5);
insert(inputs, "h", 2);
ASSERT_EQ(calc_top_three(inputs), "h: 12\nf: 9\ng: 2\n");
}
TEST(task_1, outputTest_3)
{
unordered_map<string, int> inputs;
insert(inputs, "c", 1);
insert(inputs, "b", 1);
insert(inputs, "a", 1);
ASSERT_EQ(calc_top_three(inputs), "a, b, c: 1\n");
}
TEST(task_1, outputTest_4)
{
unordered_map<string, int> inputs;
insert(inputs, "h451d3cfc", 1);
insert(inputs, "b", 12);
insert(inputs, "h451d3cfc", 1);
insert(inputs, "af4g7cdf", 1);
insert(inputs, "af4g7cdf8k", 100);
insert(inputs, "b", 88);
ASSERT_EQ(calc_top_three(inputs), "af4g7cdf8k, b: 100\nh451d3cfc: 2\naf4g7cdf: 1\n");
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
#include"gtest/gtest.h"
#include"../task_2/task_2.h"
TEST(task_2, outputTest_1)
{
set<int> nums;
nums.insert(123);
nums.insert(567);
nums.insert(124);
nums.insert(568);
nums.insert(-100);
nums.insert(-99);
ASSERT_EQ(printIntervals(nums), "[-100, -99] with consecutive count of 2\n[123, 124] with consecutive count of 2\n[567, 568] with consecutive count of 2\n");
}
TEST(task_2, outputTest_2)
{
set<int> nums;
nums.insert(-181);
nums.insert(-414);
nums.insert(441);
nums.insert(889);
nums.insert(-547);
nums.insert(-313);
nums.insert(622);
nums.insert(679);
nums.insert(782);
nums.insert(-640);
ASSERT_EQ(printIntervals(nums), "[-640, -640] with consecutive count of 1\n[-547, -547] with consecutive count of 1\n[-414, -414] with consecutive count of 1\n[-313, -313] with consecutive count of 1\n[-181, -181] with consecutive count of 1\n[441, 441] with consecutive count of 1\n[622, 622] with consecutive count of 1\n[679, 679] with consecutive count of 1\n[782, 782] with consecutive count of 1\n[889, 889] with consecutive count of 1\n");
}
TEST(task_2, outputTest_3)
{
set<int> nums;
nums.insert(-2);
nums.insert(-1);
nums.insert(0);
nums.insert(1);
nums.insert(2);
nums.insert(3);
nums.insert(4);
nums.insert(5);
nums.insert(6);
ASSERT_EQ(printIntervals(nums), "[-2, 6] with consecutive count of 9\n");
}
TEST(task_2, outputTest_4)
{
set<int> nums;
nums.insert(1);
nums.insert(1);
nums.insert(0);
nums.insert(1);
nums.insert(1);
nums.insert(1);
nums.insert(1);
ASSERT_EQ(printIntervals(nums), "[0, 1] with consecutive count of 2\n");
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment