Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@
*.exe
*.out
*.app

# Build dirs
build-*

# .user files
*.user
64 changes: 64 additions & 0 deletions tdd_intro/homework/02_ternary_numbers/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::pow уже сделал это за нас :)

Copy link
Author

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
Слишком буквально это воспринял)))

{
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут также можно было бы использовать std::transform для наполения вектора:
http://www.cplusplus.com/reference/algorithm/transform/

    std::string in = "123";
    std::vector<int> out(3);
    std::transform(in.cbegin(), in.cend(), out.begin(), [] (char ch) { return ch - '0'; });
    EXPECT_EQ(std::vector<int>({1,2,3}), out);

Copy link
Author

Choose a reason for hiding this comment

The 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"));
}