Problem Statement
I need software that can read written numbers and convert them to integers.
The numbers are always between 1 and 99 inclusive, but may be misspelled.
The correct spelling of numbers is given by the following grammar, in which
'<' '>' '::=' and '|' are metacharacters and all others are literal:
- <NUMBER> ::= <ONES> | <TEEN> | <TENS> | <TENS>-<ONES>
- <ONES> ::= one | two | three | four | five | six | seven | eight | nine
- <TEEN> ::= ten | eleven | twelve | thirteen | fourteen | fifteen | sixteen | seventeen | eighteen | nineteen
- <TENS> ::= twenty | thirty | forty | fifty | sixty | seventy | eighty | ninety
The method should return -1 if there is more than one value that is closest to number.
Definition
- Class:
- Speller
- Method:
- value
- Parameters:
- String
- Returns:
- int
- Method signature:
- int value(String number)
- (be sure your method is public)
Constraints
- number will contain between 1 and 20 characters inclusive.
- Each character in number will be between ASCII 33 and ASCII 126 inclusive.
Examples
"forty-two"
Returns: 42
"FORTY-TWO"
Returns: -1
"forty-two" and " fifty-two" are both a distance of 8 from "FORTY-TWO". No other number is closer."eightene"
Returns: 18
"eighteen" is 2 misspelled letters away from "eightene""fi"
Returns: -1
All numbers are infinitely far away, since there are no 2 character strings that represent numbers.