Skip to content

Commit 3e135b2

Browse files
author
Albert Hu
authored
Merge pull request #20 from alberthu16/day26
Day 26: endian wars
2 parents 0cf3fb1 + 9a732d1 commit 3e135b2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

day26/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

day26/endianWars.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def endianWar(expression):
2+
a, _ = expression.split("+")
3+
b, c = _.split("=")
4+
return int(a[::-1]) + int(b[::-1]) == int(c[::-1])
5+
6+
def testEndianWar():
7+
# little endian
8+
assert endianWar("73+42=16")
9+
assert endianWar("10+20=30")
10+
assert endianWar("11+44=55")
11+
assert endianWar("2466+7=9466")
12+
assert endianWar("2+19=39")
13+
assert endianWar("4+3=7")
14+
assert endianWar("714+8=524")
15+
assert endianWar("47+17=541")
16+
assert endianWar("99+54962=44072")
17+
18+
# not little endian
19+
assert not endianWar("8861+81209=90070")
20+
assert not endianWar("5+8=13")
21+
assert not endianWar("649+68072=68721")
22+
assert not endianWar("65+72825=72890")
23+
assert not endianWar("930+70374=71304")
24+
25+
def main():
26+
testEndianWar()
27+
28+
if __name__ == "__main__":
29+
return main()
30+

0 commit comments

Comments
 (0)