• Status: Solved
• Priority: Medium
• Security: Public
• Views: 217

# Need to solve this problem in specified time Limit of 2 secs

This is kinda my job in office... I need to write a code for this problem .. and am in confused state
any1 mind helping me with this??

Input description
The input file contains several values (less than twomillion) written on cheques
Output description
For each line of input, print the number in numbers.
Example
Input :
thirteen
fourteen
one-thousand
one-million-three-hundred-and-twenty-five-thousand-nine-hundred-and-seventy-nine

Output:
13
14
1000
1325979
0
anxiousjoe
• 2
1 Solution

Commented:
'one-million-three-hundred-and-twenty-five-thousand-nine-hundred-and-seventy-nine'

Strictly speaking, this isn't a valid number.. you can only have one "and" in a number, and it indicates where the decimal point is:   1,000,300.25900.79   makes no sense.

But in any case, your first step should be to tokenize the number.
Then use a table to interpret each word as a weight.

i.e.  "five hundred"  is two tokens,  five and hundred.
You want to interpret that as  "5 * 100"

With
one-million-three-hundred-and-twenty-five-thousand-nine-hundred-and-seventy-nine

You should ignore the and tokens, since they are used incorrectly.

one = 1
x million = 1000000
+ three = 3
x hundred = 100
+  twenty = 20
x five = 5
x thousand = 1000
+  nine = 9
x hundred = 100
+   seventy = 70
x nine = 9

In case it isn't clear:  when you encounter a term that modulo 10 equals zero, such as  "hundred", "thousand", "million",  "billion", or "trillion",  that is the start of a new term.

Terms are added together.   Tokens within the same term are multiplied with each other.

So  "two thousand three hundred seventy-nine"
is 4 terms.

"two thousand",   "three hundred",  "seventy",  and "nine"
2 * 1000
+ 3 * 100
+ 7 * 10
+ 9

You can treat  "seventy" as short for   "seven ten",   and  "fifteen" as  "ten five",

You have two options,  either enter them in a manual weigths table, or you need to define substitution rules  to "normalize"  the number into a format your algorithm will handle correctly.

``````
struct { char* text;  long long weight; }
numbertab[] = {
{ "hundred", 100},
{ "thousand", 1000},
{ "million", 1000000},
{ "billion", 1000000000},
{ "trillion", 1000000000000LL },
{   "one", 1 },   {   "two", 2 },   {   "three", 3 },
{   "four", 4 },  {   "five", 5 },  {   "six", 6 },
{   "seven", 7 }, {   "eight", 8 }, {   "nine", 9 },
{   "ten", 10 },  {   "eleven", 11 },{   "twelve", 12 },
{   "thirteen", 13 },{   "fourteen", 14 },{   "fifteen", 15 },
{   "sixteen", 16 }, {   "seventeen", 17 },{   "eighteen", 18 },
{   "nineteen", 19 },{   "twenty", 20 },

{   "thirty", 30},   { "fourty", 40 },   { "fifty", 50},
{   "sixty", 60},    { "seventy", 70 },  { "eighty", 80 },
{   "ninety", 90}
};
``````
0

Commented:
Actually, what would translate properly is:

one = 1 x million = 1000000
+ three = 3
x hundred = 100
+  twenty = 20
x   five = 5
x thousand = 1000
+  nine = 9
x hundred = 100
+   seventy = 70
x nine = 9

Suggesting use of a stack.   You would push down a new context (and add up the sub-terms) when you see a number mod 10 == 0,    and pop the frame when you see a number mod 10 == 0    that is larger than your stack head.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.