feat: add quiz 2 counter.
This commit is contained in:
parent
651bc79d2e
commit
a4624b477f
|
@ -6,3 +6,4 @@ set(CMAKE_CXX_STANDARD 11)
|
|||
add_subdirectory(third-party/googletest-1.17.0)
|
||||
|
||||
add_subdirectory(mixplus)
|
||||
add_subdirectory(counter)
|
11
counter/CMakeLists.txt
Normal file
11
counter/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
|
||||
|
||||
add_library(libcount STATIC ${SRC})
|
||||
|
||||
add_executable(counter main.cpp)
|
||||
|
||||
target_link_libraries(counter libcount)
|
||||
|
||||
add_subdirectory(tests)
|
15
counter/include/counter.h
Normal file
15
counter/include/counter.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace counter
|
||||
{
|
||||
struct Counter
|
||||
{
|
||||
static std::unordered_map<std::string, long> count(const std::string& input);
|
||||
|
||||
static void countPair(std::unordered_map<std::string, long>& map, const std::string& input);
|
||||
|
||||
|
||||
static void countTriple(std::unordered_map<std::string, long>& map, const std::string& input);
|
||||
};
|
||||
}
|
21
counter/main.cpp
Normal file
21
counter/main.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <counter.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(const int argc, char** argv)
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
std::cout << "ERROR: Need a string." << std::endl;
|
||||
}
|
||||
|
||||
const std::string input(argv[1]);
|
||||
|
||||
const auto map = counter::Counter::count(input);
|
||||
|
||||
for (const auto& pair : map)
|
||||
{
|
||||
std::cout << pair.first << ' ' << pair.second << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
34
counter/src/counter.cpp
Normal file
34
counter/src/counter.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include "counter.h"
|
||||
|
||||
|
||||
void counter::Counter::countPair(std::unordered_map<std::string, long>& map, const std::string& input)
|
||||
{
|
||||
for (int i = 1; i < input.size(); i++)
|
||||
{
|
||||
const auto pair = input.substr(i - 1, 2);
|
||||
|
||||
map[pair] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void counter::Counter::countTriple(std::unordered_map<std::string, long>& map, const std::string& input)
|
||||
{
|
||||
for (int i = 2; i < input.size(); i++)
|
||||
{
|
||||
const auto pair = input.substr(i - 2, 3);
|
||||
|
||||
map[pair] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, long> counter::Counter::count(const std::string& input)
|
||||
{
|
||||
std::unordered_map<std::string, long> map;
|
||||
|
||||
countPair(map, input);
|
||||
countTriple(map, input);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
10
counter/tests/CMakeLists.txt
Normal file
10
counter/tests/CMakeLists.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
enable_testing()
|
||||
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} TEST_SRC)
|
||||
|
||||
add_executable(
|
||||
counter_tests
|
||||
${TEST_SRC}
|
||||
)
|
||||
|
||||
target_link_libraries(counter_tests GTest::gtest_main libcount)
|
0
counter/tests/smoke-tests.cpp
Normal file
0
counter/tests/smoke-tests.cpp
Normal file
Loading…
Reference in New Issue
Block a user