-
Notifications
You must be signed in to change notification settings - Fork 16
2 ternary numbers #27
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
c8977bc
921cab5
967a58d
338a11f
5b6a2b2
f9586fc
2a5991d
acb4c81
2ae1a7c
d798aeb
f51f95c
1b1c468
1b29c7f
0450cb2
19fbeac
84ebd3e
3e443bc
9ca24a8
d0e97df
94d2d07
1f8aa49
bcd591d
246e615
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 |
|---|---|---|
|
|
@@ -30,3 +30,9 @@ | |
| *.exe | ||
| *.out | ||
| *.app | ||
|
|
||
| # Build dirs | ||
| build-* | ||
|
|
||
| # .user files | ||
| *.user | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,67 @@ 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. | ||
| */ | ||
|
|
||
| int CharToInt(char c) | ||
| { | ||
| return c - '0'; | ||
| } | ||
|
|
||
| int Power(int number, int index) | ||
| { | ||
| int result = 1; | ||
| for (int i = 0; i < index; i++) | ||
| { | ||
| result *= number; | ||
| } | ||
| return result; | ||
| } | ||
| std::vector<int> ParseDigits(const std::string& number) | ||
| { | ||
| std::vector<int> result; | ||
| for (char ch : number) | ||
| { | ||
| result.push_back(CharToInt(ch)); | ||
| } | ||
| return result; | ||
|
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. тут также можно было бы использовать std::transform для наполения вектора:
Author
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. Такая же ситуация как и выше) |
||
| } | ||
| int TerToDec(const std::string& ternary) | ||
| { | ||
| int result = 0; | ||
| std::vector<int> digits = ParseDigits(ternary); | ||
| size_t i = 0; | ||
| for (auto digit = digits.rbegin(); digit != digits.rend(); ++digit) | ||
| { | ||
| result += *digit * Power(3, i); | ||
| i++; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| TEST(PowerTest, ItReturnsNumberMultipliedByNumberNTimesForIndexN) | ||
| { | ||
| EXPECT_EQ(5, Power(5, 1)); | ||
| EXPECT_EQ(25, Power(5, 2)); | ||
| EXPECT_EQ(625, Power(5, 4)); | ||
| } | ||
| TEST(ParseDigitsTest, ItReturnsVectorWithDigitsForMultipleDigits) | ||
| { | ||
| EXPECT_EQ(std::vector<int>({ 1 }), ParseDigits("1")); | ||
| EXPECT_EQ(std::vector<int>({ 1, 2, 3 }), ParseDigits("123")); | ||
| EXPECT_EQ(std::vector<int>({ 1, 2, 3, 1 }), ParseDigits("1231")); | ||
| } | ||
|
|
||
| TEST(TerToDecTest, ItReturnsZeroForZero) | ||
| { | ||
| EXPECT_EQ(0, TerToDec("0")); | ||
| } | ||
| TEST(TerToDecTest, ItReturnsDecRepresentationForAnyNumber) | ||
| { | ||
| EXPECT_EQ(1, TerToDec("1")); | ||
| EXPECT_EQ(4, TerToDec("11")); | ||
| EXPECT_EQ(13, TerToDec("111")); | ||
| } | ||
| TEST(TerToDecTest, Acceptance) | ||
| { | ||
| EXPECT_EQ(302, TerToDec("102012")); | ||
| } | ||
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.
std::pow уже сделал это за нас :)
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.
If your language provides a method in the standard libraryСлишком буквально это воспринял)))