diff --git a/include/find_the_smallest/to_smallest.h b/include/find_the_smallest/to_smallest.h new file mode 100644 index 0000000..5d15624 --- /dev/null +++ b/include/find_the_smallest/to_smallest.h @@ -0,0 +1,7 @@ +#include + +class ToSmallest +{ +public: + static std::vector smallest(long long n); +}; diff --git a/src/find_the_smallest/to_smallest.cpp b/src/find_the_smallest/to_smallest.cpp new file mode 100644 index 0000000..5028f88 --- /dev/null +++ b/src/find_the_smallest/to_smallest.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +/** + * https://www.codewars.com/kata/573992c724fc289553000e95/train/cpp + */ +class ToSmallest { +public: + static std::vector smallest(long long n); +}; + +class NumberUtils { +private: + NumberUtils() = default; + +public: + static std::vector digits(long long n); + + static long long int from_digits(const std::vector &digits); + + static unsigned int length(long long n); +}; + +class Result { +public: + const long long int value; + const unsigned int i; + const unsigned int j; + + Result(long long int value, const unsigned int i, const unsigned int j) : value(value), i(i), j(j) {} + + [[nodiscard]] std::vector to_vector() const { + return {value, i, j}; + } + + bool operator<(const Result &rhs) const { + return value < rhs.value; + } +}; + +class IntPair { +private: + IntPair() : i(0), j(0) {} + + IntPair(int i, int j) : i(i), j(j) {} + +public: + const int i; + const int j; + + static std::vector potential_results(long long n); + + [[nodiscard]] long long moveDigit(long long n) const; + + static std::vector cartesian_product(unsigned long n); +}; + +std::vector NumberUtils::digits(long long int n) { + std::vector digits; + + while (n > 0) { + digits.insert(digits.begin(), n % 10); + n /= 10; + } + + return digits; +} + +long long int NumberUtils::from_digits(const std::vector &digits) { + long long int n = digits.at(0); + + for (int i = 1; i < digits.size(); i++) { + n = n * 10 + digits.at(i); + } + + return n; +} + +unsigned int NumberUtils::length(long long int n) { + return std::to_string(n).length(); +} + +std::vector IntPair::cartesian_product(unsigned long n) { + std::vector pairs; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + pairs.push_back(IntPair(i, j)); + } + } + + return pairs; +} + +std::vector IntPair::potential_results(long long int n) { + unsigned int length = NumberUtils::length(n); + const std::vector &pairs = IntPair::cartesian_product(length); + + std::vector results; + + for (const IntPair &pair: pairs) { + long long int value = n; + + value = pair.moveDigit(n); + results.emplace_back(value, pair.i, pair.j); + } + + return results; +} + +long long IntPair::moveDigit(long long int n) const { + std::vector digits = NumberUtils::digits(n); + int digit = digits.at(i); + + digits.erase(digits.begin() + i); + digits.insert(digits.begin() + j, digit); + + return NumberUtils::from_digits(digits); +} + +std::vector ToSmallest::smallest(long long int n) { + std::vector results = IntPair::potential_results(n); + auto min = std::min_element(begin(results), end(results)); + + return min->to_vector(); +} diff --git a/test/find_the_smallest/to_smallest.cpp b/test/find_the_smallest/to_smallest.cpp new file mode 100644 index 0000000..2e2b5e5 --- /dev/null +++ b/test/find_the_smallest/to_smallest.cpp @@ -0,0 +1,25 @@ +#include +#include "find_the_smallest/to_smallest.h" + +using namespace igloo; + +#include + +void testequal(std::vector ans, std::vector sol) { + Assert::That(ans, Equals(sol)); +} + +static void dotest(long long n, std::vector expected) { + testequal(ToSmallest::smallest(n), expected); +} + +Describe(smallest_Tests) { + It(Fixed_Tests) { + dotest(261235, {126235, 2, 0}); + dotest(209917, {29917, 0, 1}); + dotest(285365, {238565, 3, 1}); + dotest(269045, {26945, 3, 0}); + dotest(296837, {239687, 4, 1}); + dotest(261235209917281352, {26123529917281352, 7, 0}); + } +};