Article 5PHVZ Programming Challenge: English Speaking Calculator

Programming Challenge: English Speaking Calculator

by
astrogeek
from LinuxQuestions.org on (#5PHVZ)
Things have been relatively quiet in the Programming forum of late! So it is a good time for a fresh Programming Challenge!

While outfitting a new laptop over recent months I have revisited an exercise from my personal archives which had its start some years ago, but has been interesting enough to explore and extend more than once over the intervening time: a simple calculator which accepts english language descriptions of arithmetic expressions, symbolic notation, or both as input, and writes an english language result. For example:

Code:> twelve times seven plus three
eighty seven
> twelve times 7 + 3
eighty seven
> 12 * seven plus 3
eighty seven
> eighty four million three hundred twenty one thousand seven hundred thirty six plus negative eighty four million seven hundred thirty six
three hundred twenty one thousandI have explored several ways of doing this using different programming languages and approaches to the problem, all of which have been enjoyable and educational, but I have never actually produced what I consider a full and complete implementation - which I am now of a mind to do. But most things, especially ideas, are always more enjoyable when shared so I invite everyone to share the fun and their own ideas in the...

English Speaking Calculator Programming Challenge

The challenge here is to write an application which evaluates simple signed integer arithmetic expressions, whether expressed as numeric symbols, english words or both, and shows the result as a properly formed english word description and optionally as the numeric representation.

I do not want to constrain others by the methods I have already explored so I suggest the following only as a minimal program requirement, subject to change and agreement by participants:

* Must accept input expressed as english words, common math symbols, or both using infix notation
* Must reject invalid or nonsensical input with a suitable error message
* Must perform signed integer addition, subtraction, multiplication and division operations
* Must observe correct order of operations, grouping and associativity in all evaluations
* Must produce the result as properly formed english word descriptions, optionally as numeric values
* Must specify and enforce the range of values over which it will produce valid results
* All results must be mathematically correct, or must produce an error message

Additionaly, and optionally ...

* May support parenthesized expressions
* May support additional operations, exponentiation, etc.
* May support logical operators
* May support common english variations such as 'eleven hundred' for 1,100 (but not 'one hundred two hundred' for 10,200!)
* May be interactive or operate as a filter

I suggest we limit to infix input notation mostly to make it easier for us to compare examples between implementations.

Also, an internet search for 'english language calculator' does produce at least a few hits, but I have strictly avoided following any of them because the whole point of the original exercise was to work out my own solutions. You may choose your own starting points and references, but please refrain from simply posting links to other online solutions here - in other words, let's constrain this thread to our own ideas and variations, please!

I also suggest use of each member's LQ blog space as the place to post complete code, and limit forum posts to specific implementation ideas and examples of interest.

The main idea here is to have fun and share ideas and methods as we explore this particular problem space - and see where it leads! Let's have fun and learn some new tricks!

(Note: As I have a bit of a head start I'll refrain from posting my own code until everyone has a chance to form their own ideas and join the discussion!)
latest?d=yIl2AUoC8zA latest?i=cnqHvJ4CDW8:rXE6nyPnFQs:F7zBnMy latest?i=cnqHvJ4CDW8:rXE6nyPnFQs:V_sGLiP latest?d=qj6IDK7rITs latest?i=cnqHvJ4CDW8:rXE6nyPnFQs:gIN9vFwcnqHvJ4CDW8
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments