Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f1e36b9
Added task
Ryanarre Jun 27, 2018
da7d9de
Added task
Ryanarre Jun 27, 2018
5d02e18
Test Input_0_Get_Zero - red
Ryanarre Jun 27, 2018
9d98a02
Test Input_0_Get_Zero - green
Ryanarre Jun 27, 2018
e3d6405
Test Input_1_Get_One - red
Ryanarre Jun 27, 2018
d0e66ce
Test Input_1_Get_One - green
Ryanarre Jun 27, 2018
01e2f58
Test Numbers from 0 to 9 - refactoring
Ryanarre Jun 27, 2018
e8cc6c6
Test OutOfBounds - red
Ryanarre Jun 27, 2018
50c0588
Test OutOfBounds - green
Ryanarre Jun 27, 2018
75c814d
Test Input_10_Get_Ten - red
Ryanarre Jun 27, 2018
491c27c
Test Input_10_Get_Ten - green
Ryanarre Jun 27, 2018
c0b4be3
Test Input_14_Get_Fourteen - red
Ryanarre Jun 27, 2018
e8b25f2
Test Input_14_Get_Fourteen - green
Ryanarre Jun 27, 2018
6128b10
Test Numbers from 10 to 19 - refactoring
Ryanarre Jun 27, 2018
68927d0
Test Input_21_Get_TwentyOne - red
Ryanarre Jun 27, 2018
32bec45
Test Input_21_Get_TwentyOne - green
Ryanarre Jun 27, 2018
d539ac0
Test Input_35_Get_ThirtyFive - red
Ryanarre Jun 27, 2018
c6510ac
Test Input_35_Get_ThirtyFive - green
Ryanarre Jun 27, 2018
6fd83a8
Test Numbers from 0 to 99 - refactoring
Ryanarre Jun 27, 2018
28b2a13
Test Input_100_Get_OneHundred - red
Ryanarre Jun 27, 2018
8e2bc6b
Test Input_135_Get_OneHundredAndThirtyFive - red
Ryanarre Jun 27, 2018
74bc357
Test Input_135_Get_OneHundredAndThirtyFive - green
Ryanarre Jun 27, 2018
d91147e
Test Numbers from 0 to 999 - refactoring
Ryanarre Jun 27, 2018
bcc56b2
Test Input_1024_Get_OneThroursandTwentyFour - red
Ryanarre Jun 27, 2018
6490eab
Test Input_1024_Get_OneThroursandTwentyFour - green
Ryanarre Jun 27, 2018
f2233d0
Test Input_2993_Get_TwoThoursandNineHundredAndNinetyThree - red
Ryanarre Jun 27, 2018
fee54af
Test Input_2993_Get_TwoThoursandNineHundredAndNinetyThree - fixed and…
Ryanarre Jun 27, 2018
2f6f569
Fixed spellings and test
Ryanarre Jun 27, 2018
3166485
Acceptance test - failed
Ryanarre Jun 27, 2018
8c9d9a7
Fixed tests and made acceptance green
Ryanarre Jun 27, 2018
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions tdd_intro/homework/09_numbers/09_numbers.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(../../gtest.pri)

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

DEFINES += NOMINMAX

SOURCES += \
test.cpp
215 changes: 215 additions & 0 deletions tdd_intro/homework/09_numbers/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
Say in C++
Given a number from 0 to 99,999, spell out that number in English.

Step 1
Handle the basic case of 0 through 99.

If the input to the program is 22, then the output should be 'twenty-two'.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

0
14
50
98
-1
100
Step 2
Put it all together to get nothing but plain English.

12345 should give twelve thousand three hundred forty-five.

The program must also report any values that are out of range.

Extensions
Use and (correctly) when spelling out the number in English:

14 becomes "fourteen".
100 becomes "one hundred".
120 becomes "one hundred and twenty".
1002 becomes "one thousand and two".
1323 becomes "one thousand three hundred and twenty-three".
*/

#include <gtest/gtest.h>
#include <string>
#include <map>

std::map<int, std::string> s_0To19Numbers = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"},
{10, "ten"},
{11, "eleven"},
{12, "twelve"},
{13, "thirteen"},
{14, "fourteen"},
{15, "fifteen"},
{16, "sixteen"},
{17, "seventeen"},
{18, "eighteen"},
{19, "nineteen"}
};

std::map<int, std::string> s_20To90Numbers = {
{20, "twenty"},
{30, "thirty"},
{40, "forty"},
{50, "fifty"},
{60, "sixty"},
{70, "seventy"},
{80, "eighty"},
{90, "ninety"}
};

std::string s_hundred("hundred");
std::string s_thoursand("thousand");

std::string GetTensPart(int number)
{
int tens = (number % 100) / 10;

if (tens > 1)
{
std::string tenPart = s_20To90Numbers.find(tens * 10)->second;

int ones = number % 10;
if (ones != 0)
{
return tenPart + "-" + s_0To19Numbers.find(ones)->second;
}
else
{
return tenPart;
}
}
else
{
return s_0To19Numbers.find(number)->second;
}
}


std::string GetSpelling(int number)
{
if (number < 0 || number > 99999)
{
throw std::out_of_range("Out of bounds value!");
}

std::string thoursandStr;
std::string hundredsStr;
std::string tensStr;

int thoursands = number / 1000;

if (thoursands > 0)
{
thoursandStr = GetTensPart(thoursands) + " " + s_thoursand;

number -= thoursands * 1000;
}

int hundreds = number / 100;

if (hundreds > 0)
{
hundredsStr = s_0To19Numbers.find(hundreds)->second + " " + s_hundred;

number -= hundreds * 100;
}

tensStr = GetTensPart(number);

std::string finalStr;

if (!thoursandStr.empty())
{
finalStr += thoursandStr + " ";
}

if (hundredsStr.empty())
{
finalStr += tensStr;
}
else if (tensStr.empty() || tensStr == "zero")
{
finalStr += hundredsStr;
}
else
{
finalStr += hundredsStr + " and " + tensStr;
}

return finalStr;
}

TEST(GetSpelling, Input_0_Get_Zero)
{
EXPECT_EQ("zero", GetSpelling(0));
}

TEST(GetSpelling, Input_1_Get_One)
{
EXPECT_EQ("one", GetSpelling(1));
}

TEST(GetSpelling, OutOfBounds)
{
EXPECT_THROW(GetSpelling(-1), std::out_of_range);
}

TEST(GetSpelling, Input_10_Get_Ten)
{
EXPECT_EQ("ten", GetSpelling(10));
}

TEST(GetSpelling, Input_14_Get_Fourteen)
{
EXPECT_EQ("fourteen", GetSpelling(14));
}

TEST(GetSpelling, Input_21_Get_TwentyOne)
{
EXPECT_EQ("twenty-one", GetSpelling(21));
}

TEST(GetSpelling, Input_35_Get_ThirtyFive)
{
EXPECT_EQ("thirty-five", GetSpelling(35));
}

TEST(GetSpelling, Input_100_Get_OneHundred)
{
EXPECT_EQ("one hundred", GetSpelling(100));
}

TEST(GetSpelling, Input_135_Get_OneHundredAndThirtyFive)
{
EXPECT_EQ("one hundred and thirty-five", GetSpelling(135));
}

TEST(GetSpelling, Input_1024_Get_OneThroursandTwentyFour)
{
EXPECT_EQ("one thousand twenty-four", GetSpelling(1024));
}

TEST(GetSpelling, Input_2993_Get_TwoThoursandNineHundredAndNinetyThree)
{
EXPECT_EQ("two thousand nine hundred and ninety-three", GetSpelling(2993));
}

TEST(GetSpelling, AcceptanceTest)
{
EXPECT_EQ("twelve thousand three hundred and forty-five", GetSpelling(12345));
}
3 changes: 2 additions & 1 deletion tdd_intro/homework/homework.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ SUBDIRS += \
05_word_wrapp \
06_bank_ocr \
07_filecopier \
08_timer
08_timer \
09_numbers