|
| 1 | +question of the day: https://codefights.com/challenge/ChcFLSa3rfJsKNgkC |
| 2 | + |
| 3 | +The fight on whether to store numbers starting with their most |
| 4 | +significant digit or their least significant digit has been going |
| 5 | +on for years. It even got a name and is called the Endian War by |
| 6 | +some specialists. |
| 7 | + |
| 8 | +Joe Stoy in his (excellent, by the way) book "Denotational Semantics", |
| 9 | +tells the following story about Alan Turing: "...One early British |
| 10 | +computer had numbers running from right to left (because the spot |
| 11 | +on an oscilloscope tube runs from left to right, but in serial |
| 12 | +logic the least significant digits are dealt with first). Turing |
| 13 | +used to mystify audiences at public lectures when, quite by |
| 14 | +accident, he would slip into this mode even for decimal arithmetic, |
| 15 | +and write things like 73+42=16...". |
| 16 | + |
| 17 | +You are given an expression that was presumably written by Alan Turing. |
| 18 | +Return true if it is a correct expression written in the little-endian |
| 19 | +decimal format, or return false otherwise. |
| 20 | + |
| 21 | +## Example |
| 22 | + |
| 23 | +For expression = 73+42=16, the output should be |
| 24 | +endianWar(expression) = true. |
| 25 | + |
| 26 | +In the little-endian decimal format, the expression becomes |
| 27 | +37 + 24 = 61, which is correct. |
| 28 | + |
| 29 | +For expression = "5+8=13", the output should be |
| 30 | +endianWar(expression) = false. |
| 31 | + |
| 32 | +In the little-endian decimal format, the result of the expression should |
| 33 | +be 31. |
| 34 | + |
| 35 | +## Ideas |
| 36 | + |
| 37 | +Let's break the expression string up and check the math. Reverse the |
| 38 | +two addends and sum them, then check against the reversed little-endian |
| 39 | +sum. |
| 40 | + |
| 41 | +`O(n)` to do the splits on the expression, `O(n)` to reverse the numbers |
| 42 | +and convert into ints. `O(1)` to check the math of the expression. |
| 43 | + |
| 44 | +## Code |
| 45 | + |
| 46 | +[Python](./endianWars.py) |
| 47 | + |
| 48 | +## Follow up |
0 commit comments