Skip to content

Commit 07f8a22

Browse files
committed
Solve 'Find the smallest' kata
1 parent e4bf177 commit 07f8a22

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <vector>
2+
3+
class ToSmallest
4+
{
5+
public:
6+
static std::vector<long long> smallest(long long n);
7+
};

src/find_the_smallest/to_smallest.cpp

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <vector>
2+
#include <algorithm>
3+
4+
/**
5+
* https://www.codewars.com/kata/573992c724fc289553000e95/train/cpp
6+
*/
7+
class ToSmallest {
8+
public:
9+
static std::vector<long long> smallest(long long n);
10+
};
11+
12+
class NumberUtils {
13+
private:
14+
NumberUtils() = default;
15+
16+
public:
17+
static std::vector<unsigned short> digits(long long n);
18+
19+
static long long int from_digits(const std::vector<unsigned short> &digits);
20+
21+
static unsigned int length(long long n);
22+
};
23+
24+
class Result {
25+
public:
26+
const long long int value;
27+
const unsigned int i;
28+
const unsigned int j;
29+
30+
Result(long long int value, const unsigned int i, const unsigned int j) : value(value), i(i), j(j) {}
31+
32+
[[nodiscard]] std::vector<long long> to_vector() const {
33+
return {value, i, j};
34+
}
35+
36+
bool operator<(const Result &rhs) const {
37+
return value < rhs.value;
38+
}
39+
};
40+
41+
class IntPair {
42+
private:
43+
IntPair() : i(0), j(0) {}
44+
45+
IntPair(int i, int j) : i(i), j(j) {}
46+
47+
public:
48+
const int i;
49+
const int j;
50+
51+
static std::vector<Result> potential_results(long long n);
52+
53+
[[nodiscard]] long long moveDigit(long long n) const;
54+
55+
static std::vector<IntPair> cartesian_product(unsigned long n);
56+
};
57+
58+
std::vector<unsigned short> NumberUtils::digits(long long int n) {
59+
std::vector<unsigned short> digits;
60+
61+
while (n > 0) {
62+
digits.insert(digits.begin(), n % 10);
63+
n /= 10;
64+
}
65+
66+
return digits;
67+
}
68+
69+
long long int NumberUtils::from_digits(const std::vector<unsigned short> &digits) {
70+
long long int n = digits.at(0);
71+
72+
for (int i = 1; i < digits.size(); i++) {
73+
n = n * 10 + digits.at(i);
74+
}
75+
76+
return n;
77+
}
78+
79+
unsigned int NumberUtils::length(long long int n) {
80+
return std::to_string(n).length();
81+
}
82+
83+
std::vector<IntPair> IntPair::cartesian_product(unsigned long n) {
84+
std::vector<IntPair> pairs;
85+
86+
for (int i = 0; i < n; i++) {
87+
for (int j = 0; j < n; j++) {
88+
pairs.push_back(IntPair(i, j));
89+
}
90+
}
91+
92+
return pairs;
93+
}
94+
95+
std::vector<Result> IntPair::potential_results(long long int n) {
96+
unsigned int length = NumberUtils::length(n);
97+
const std::vector<IntPair> &pairs = IntPair::cartesian_product(length);
98+
99+
std::vector<Result> results;
100+
101+
for (const IntPair &pair: pairs) {
102+
long long int value = n;
103+
104+
value = pair.moveDigit(n);
105+
results.emplace_back(value, pair.i, pair.j);
106+
}
107+
108+
return results;
109+
}
110+
111+
long long IntPair::moveDigit(long long int n) const {
112+
std::vector<unsigned short> digits = NumberUtils::digits(n);
113+
int digit = digits.at(i);
114+
115+
digits.erase(digits.begin() + i);
116+
digits.insert(digits.begin() + j, digit);
117+
118+
return NumberUtils::from_digits(digits);
119+
}
120+
121+
std::vector<long long> ToSmallest::smallest(long long int n) {
122+
std::vector<Result> results = IntPair::potential_results(n);
123+
auto min = std::min_element(begin(results), end(results));
124+
125+
return min->to_vector();
126+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <igloo/igloo_alt.h>
2+
#include "find_the_smallest/to_smallest.h"
3+
4+
using namespace igloo;
5+
6+
#include <vector>
7+
8+
void testequal(std::vector<long long> ans, std::vector<long long> sol) {
9+
Assert::That(ans, Equals(sol));
10+
}
11+
12+
static void dotest(long long n, std::vector<long long> expected) {
13+
testequal(ToSmallest::smallest(n), expected);
14+
}
15+
16+
Describe(smallest_Tests) {
17+
It(Fixed_Tests) {
18+
dotest(261235, {126235, 2, 0});
19+
dotest(209917, {29917, 0, 1});
20+
dotest(285365, {238565, 3, 1});
21+
dotest(269045, {26945, 3, 0});
22+
dotest(296837, {239687, 4, 1});
23+
dotest(261235209917281352, {26123529917281352, 7, 0});
24+
}
25+
};

0 commit comments

Comments
 (0)