Re:Two problems I had to solve in my Oxford interview (2013)
by elevensies on Monday May 11, 2015 @08:21PM
(#9528749)
Well, here is my non-zero-handling answer to the first
And with zero handling (maybe)
Edit * 2nd try
[ Parent
| on HN ]
Re:Two problems I had to solve in my Oxford interview (2013)
by joshvm on Monday May 11, 2015 @11:11PM
(#9529367)
My solution, looks pretty similar to your second.
I think this should work:
0 Z2
1 Z3
2 I4
3 J0,3 -> 5 // If r0 != 0, jump to add it to r2
4 J3,4 -> 9 // r0 == 0, force jump to check r1
5 I2 // add r0
6 I3
7 J0,3 -> 5
8 Z3
9 J1,3 -> 11 // If r1 != 0, jump to add it to r2
10 J3,4 -> exit // r1 == 0, force jump to exit
11 I2 // add r1
12 I3
13 J1,3 -> 11
14 exit
Phew that took a while. The simple case was easy enough. I guess exceptional candidates would be able to crack that out in an interview, otherwise they'd move you onto the balls.Edited!
Re:Two problems I had to solve in my Oxford interview (2013)
by elevensies on Monday May 11, 2015 @11:25PM
(#9529415)
Couple corrections I think:
- Register 4 should be set to zero before incrementing in instruction 2.
- inst. 4 should compare 0,4 , register 1 is unknown at this pt.
- inst. 3 should jump to 5
Re:Two problems I had to solve in my Oxford interview (2013)
by joshvm on Tuesday May 12, 2015 @01:00AM
(#9529688)
Yep 3->5 is a typo.
r4 could be zeroed, but I was lazy and assumed that as long as it's greater than zero it doesn't matter (since it's always compared to zero).
Instruction 4 should be cmp(3,4) to force a jump to 9.
EDIT: Although thinking about it, it doesn't make much difference. It has the same effect as doing cmp(0,4) since you know r4 > 0 and r0 == 0.
Thanks!
Re:Two problems I had to solve in my Oxford interview (2013)
by Chinjut on Monday May 11, 2015 @08:41PM
(#9528832)
In your zero-handling program, you seem to suppose the first "J0,2" and "J1,3" commands take the arrow on equality, while the second "J0,2" and "J1,3" commands instead take the arrow on inequality.
Re:Two problems I had to solve in my Oxford interview (2013)
by elevensies on Monday May 11, 2015 @09:26PM
(#9528996)
oops! I changed it but I'm not sure if this is the right way to do it
Re:Two problems I had to solve in my Oxford interview (2013)
by corruptio on Tuesday May 12, 2015 @03:07AM
(#9529976)
My solution:
0: Z 2
1: Z 3
2: J 0 2 -> 4
3: J 1 3 -> 7
4: I 2
5: J 0 2 -> 4
6: J 1 3 -> 7
7: I 2
8: I 3
9: J 1 3 -> 7
Handles the zero case. Only uses 4 cells total.
Re:Two problems I had to solve in my Oxford interview (2013)
by elevensies on Tuesday May 12, 2015 @08:26AM
(#9531188)
Instruction 6 is a nop? if mem. 1 not-equals mem. 3, jump to 7, or else fall thru to 7?
I don't think you can depend on jumping in instruction 3, because you don't control the value of mem. 1, it's input. So if mem. 3 = 0, instructions 2 and 3 will always lead to instruction four.