-
Notifications
You must be signed in to change notification settings - Fork 1
02 ternary numbers #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c09e2dd
104b9d6
1d7fe3c
7685d73
b15d2ff
40b205b
2f6a6d3
efb05b3
b477a84
63c4ce5
6f8672c
c28f0eb
7560a4d
86291ee
b9b28ca
a600113
511a529
0f93668
ae29dd3
61c3393
a80d664
cbc8d47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,88 @@ The last place in a ternary number is the 1's place. The second to last is the 3 | |
|
|
||
| If your language provides a method in the standard library to perform the conversion, pretend it doesn't exist and implement it yourself. | ||
| */ | ||
|
|
||
| #include <cmath> | ||
|
|
||
| const unsigned int MaxNumber = 2; | ||
| const unsigned int PowNumeber = 3; | ||
| const unsigned int IndexMultiplier = 3; | ||
|
|
||
| unsigned int TernaryNumber(unsigned int index, unsigned int number) | ||
| { | ||
| if (number > MaxNumber) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| return number * static_cast<unsigned int>(std::pow(PowNumeber, index)); | ||
| } | ||
|
|
||
| unsigned int ConvertTernaryNumbers(const std::string &numbers) | ||
| { | ||
| if (numbers.empty()) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| unsigned int sum = 0; | ||
| for (auto rIter = numbers.rbegin(); rIter != numbers.rend(); ++rIter) | ||
| { | ||
| unsigned int number = std::stoul(std::string(1, *rIter)); | ||
| if (number > MaxNumber) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| unsigned int rIterIndex = rIter - numbers.rbegin(); | ||
| unsigned int place = rIterIndex == 0 ? 1 : rIterIndex * IndexMultiplier; | ||
| unsigned int num = TernaryNumber(place, number); | ||
|
|
||
| sum += num; | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, OneNumber) | ||
| { | ||
| ASSERT_EQ(3, TernaryNumber(1, 1)); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, WrongNumber) | ||
| { | ||
| ASSERT_EQ(0, TernaryNumber(1, 3)); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, OneIndexTwoNumber) | ||
| { | ||
| ASSERT_EQ(6, TernaryNumber(1, 2)); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, EmptyNumberString) | ||
| { | ||
| ASSERT_EQ(0, ConvertTernaryNumbers("")); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, OneNumberString) | ||
| { | ||
| ASSERT_EQ(3, ConvertTernaryNumbers("1")); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, MultipleNumberString) | ||
| { | ||
| ASSERT_EQ(27, ConvertTernaryNumbers("10")); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 было правильно, а 27 нет. |
||
| } | ||
|
|
||
| TEST(TernaryNumbers, WrongMultipleNumberString) | ||
| { | ||
| ASSERT_EQ(0, ConvertTernaryNumbers("13")); | ||
| } | ||
|
|
||
| TEST(TernaryNumbers, Acceptence) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. функция работает неправильно https://www.epochconverter.com/numbers/bin-oct-dec-hex |
||
| { | ||
| ASSERT_EQ(0, ConvertTernaryNumbers("3102")); | ||
| ASSERT_EQ(0, ConvertTernaryNumbers("0")); | ||
| ASSERT_EQ(6, ConvertTernaryNumbers("02")); | ||
| ASSERT_EQ(19743, ConvertTernaryNumbers("1022")); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| TEMPLATE = subdirs | ||
|
|
||
| SUBDIRS += \ | ||
| 00_intro \ | ||
| 01_leap_year \ | ||
| 02_ternary_numbers \ | ||
| 03_bank_ocr \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
слишком большой шаг, один тест не мог привести к циклу с различными условиями и скорее всего на этом этапе тернарный оператор в 46 строке не полностью покрыт