Skip to content

Commit 60e0fba

Browse files
committed
Solve 'Find the smallest' kata
1 parent e4bf177 commit 60e0fba

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-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

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