TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
digit_separation.cpp
Go to the documentation of this file.
1
18#include <algorithm>
19#include <cassert>
20#include <cmath>
21#include <cstdint>
22#include <iostream>
23#include <vector>
24
29namespace greedy_algorithms {
30
36 public:
41
48 std::vector<std::int64_t> digitSeparationReverseOrder(
49 std::int64_t largeNumber) const {
50 std::vector<std::int64_t> result;
51 if (largeNumber != 0) {
52 while (largeNumber != 0) {
53 result.push_back(std::abs(largeNumber % 10));
54 largeNumber /= 10;
55 }
56 } else {
57 result.push_back(0);
58 }
59 return result;
60 }
61
68 std::vector<std::int64_t> digitSeparationForwardOrder(
69 std::int64_t largeNumber) const {
70 std::vector<std::int64_t> result =
71 digitSeparationReverseOrder(largeNumber);
72 std::reverse(result.begin(), result.end());
73 return result;
74 }
75};
76
77} // namespace greedy_algorithms
78
83static void tests() {
85
86 // Test case: Positive number
87 std::int64_t number = 1234567890;
88 std::vector<std::int64_t> expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
89 std::vector<std::int64_t> expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
90 std::vector<std::int64_t> reverseOrder =
92 assert(reverseOrder == expectedReverse);
93 std::vector<std::int64_t> forwardOrder =
95 assert(forwardOrder == expectedForward);
96
97 // Test case: Single digit number
98 number = 5;
99 expectedReverse = {5};
100 expectedForward = {5};
101 reverseOrder = ds.digitSeparationReverseOrder(number);
102 assert(reverseOrder == expectedReverse);
103 forwardOrder = ds.digitSeparationForwardOrder(number);
104 assert(forwardOrder == expectedForward);
105
106 // Test case: Zero
107 number = 0;
108 expectedReverse = {0};
109 expectedForward = {0};
110 reverseOrder = ds.digitSeparationReverseOrder(number);
111 assert(reverseOrder == expectedReverse);
112 forwardOrder = ds.digitSeparationForwardOrder(number);
113 assert(forwardOrder == expectedForward);
114
115 // Test case: Large number
116 number = 987654321012345;
117 expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
118 expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
119 reverseOrder = ds.digitSeparationReverseOrder(number);
120 assert(reverseOrder == expectedReverse);
121 forwardOrder = ds.digitSeparationForwardOrder(number);
122 assert(forwardOrder == expectedForward);
123
124 // Test case: Negative number
125 number = -987654321012345;
126 expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
127 expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
128 reverseOrder = ds.digitSeparationReverseOrder(number);
129 assert(reverseOrder == expectedReverse);
130 forwardOrder = ds.digitSeparationForwardOrder(number);
131 assert(forwardOrder == expectedForward);
132}
133
138int main() {
139 tests(); // run self test implementation
140
141 return 0;
142}
A class that provides methods to separate the digits of a large positive number.
std::vector< std::int64_t > digitSeparationForwardOrder(std::int64_t largeNumber) const
Implementation of digitSeparationForwardOrder method.
std::vector< std::int64_t > digitSeparationReverseOrder(std::int64_t largeNumber) const
Implementation of digitSeparationReverseOrder method.
DigitSeparation()
Default constructor for the DigitSeparation class.
static void tests()
self test implementation
int main()
main function
for string class