series: iteration 1
This commit is contained in:
parent
ff9a749710
commit
3196a562f4
2 changed files with 70 additions and 0 deletions
54
cpp/series/series.cpp
Normal file
54
cpp/series/series.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include "series.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace series {
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
inline int char_to_digit(char ch)
|
||||
{
|
||||
if (!isdigit(ch))
|
||||
throw domain_error("not a digit");
|
||||
|
||||
return ch - '0';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
digits_t digits(const string &input)
|
||||
{
|
||||
digits_t result(input.size());
|
||||
|
||||
transform(input.cbegin(), input.cend(), result.begin(), char_to_digit);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
slice_t slice(const string &input, size_t size)
|
||||
{
|
||||
if (size > input.size())
|
||||
throw domain_error("not enough digits to slice");
|
||||
|
||||
if (size == 0)
|
||||
return slice_t();
|
||||
|
||||
digits_t input_digits = digits(input);
|
||||
slice_t result;
|
||||
|
||||
auto begin = input_digits.cbegin();
|
||||
auto end = begin + size - 1;
|
||||
|
||||
while (end != input_digits.cend())
|
||||
result.push_back(digits_t(begin++, ++end));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
16
cpp/series/series.h
Normal file
16
cpp/series/series.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#define EXERCISM_RUN_ALL_TESTS
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace series {
|
||||
|
||||
typedef std::vector<int> digits_t;
|
||||
typedef std::vector<std::vector<int>> slice_t;
|
||||
|
||||
digits_t digits(const std::string &input);
|
||||
slice_t slice(const std::string &input, std::size_t size);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue