Solved

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

Posted on 2009-02-21
200 Views
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
Question by:anxiousjoe

LVL 9

Expert Comment

0

LVL 23

Accepted Solution

'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

LVL 23

Expert Comment

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

Featured Post

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!