Alphabet mathematics
Given an equation such as
ABCD + BDC = EAEA
one can replace each letter with a digit (0-9) in order to come to a correct solution. That is, you'll replace all the As with one digit, all the Bs with another, and so on. Two distinct letters will not map to the same digit. A leading digit in a term will not map to 0.
Your code must create the function calculate(left, operation, right, answer), which will be called multiple times and should print (to stdout) answers in the following format:
calculate("ABCD", "+", "BDC", "EAEA")
1538 + 583 = 2121 1547 + 574 = 2121 1574 + 547 = 2121 1583 + 538 = 2121 3658 + 685 = 4343 3685 + 658 = 4343
The letters used will be uppercase letters from A to Z. Numbers may also be intermixed in the given arguments, which should not be replaced.
Operations can be one of * (multiplication), + (addition), - (subtraction), ^ (exponentiation), or ?. If it is ?, then it can be any one of *, +, -, or ^ and all should be tried (and the correct operation printed for each one).
Your results should be sorted.
Bad data will not be fed into the function, so error checking on input is not necessary.
More examples
calculate("AA", "+", "BC", "100")
11 + 89 = 100 22 + 78 = 100 33 + 67 = 100 44 + 56 = 100 66 + 34 = 100 77 + 23 = 100 88 + 12 = 100
calculate("XYZ", "+", "XY", "6QQ")
546 + 54 = 600 576 + 57 = 633 586 + 58 = 644
calculate("X", "?", "X", "Y")
1 + 1 = 2 2 * 2 = 4 2 + 2 = 4 2 ^ 2 = 4 3 * 3 = 9 3 + 3 = 6 4 + 4 = 8
calculate("AAAAAAAA", "+", "B", "C")
<<code>><</code>>
(no output printed)
Prize
A ticket to BlizzCon 2009. You must be at least 18 years of age in order to participate in this contest. If you participate in another contest on WowAce/CurseForge for a BlizzCon 2009 ticket, you will only be able to redeem one. You must use the ticket for yourself or you may gift it to another author (i.e. not just your girlfriend), not for resale. We will not be paying travel costs. You must be an author on either CurseForge or WowAce to participate.
Runners-up will receive 500 Curse Points or 1 year of free Curse Premium of a user of your choosing. If you choose to opt out of a BlizzCon ticket, you may still be a Runner-up.
Other info
We have the right to revoke your right to participate at any time.
Your submission must be completely your own work, you're free to use resources to figure out how you would code the solution, as long as all the code is your own.
Your submission must run in Lua 5.1.4, in a standalone environment (not in WoW). You cannot use any external libraries outside of what Lua itself provides.
Winners
- Alphabet Mathematics solution v1, Jacob Dlougach. by Dlougach
Very clean implementation, and the use of coroutines in order to generate permutations was a nice way of handling it.
- Shefki's Entry by Shefki
Very clean and well-documented
Facts
- Started
- 22 May 2009
- Closing
- 15 Jun 2009
- Judging finished
- 16 Jun 2009
- Entries
- 12
- Type
- Code
- Stage
- Finished - Contest is finished and winners have been chosen.
- Code language
- Lua