r/programming • u/SilasX • May 09 '15
"Real programmers can do these problems easily"; author posts invalid solution to #4
https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k
Upvotes
r/programming • u/SilasX • May 09 '15
6
u/hansdieter44 May 09 '15
Thats what I did as well,
you have the potential to run in a fencepost error here (9 digits, but you have 8 slots between them).
That gives you 3 ** 8 = 6561 values to iterate through, and convert to ternary and then merge with the digits. I actually managed to reuse my homegrown 'zip' function from task two here to combine my array of digits with the array of operators & spaces.
I then just looped over this, eval()'d the string (I know, I know) and if the sum was 100 printed/yielded the result.
http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error
1 - 3 I literally wrote down within 10 minutes, but 4 and 5 got me thinking for quite a while. After thinking about the edge-cases I felt a bit stupid because I went over the 1hr mark when I finished. Also, in an interview stress situation there would have been no way I would have succeeded at all tasks.