You are viewing a single comment's thread. Return to all comments →
Here is Python 3 solution from my HackerrankPractice repository:
x, y, z, n = [int(input()) for _ in range(4)]
listOfAnswers = [[i, j, k] for i in range(x + 1) for j in range(y + 1) for k in range(z + 1) if i + j + k != n]
Feel free to ask if you have any questions :)
I'm trying to teach myself Python, and reading this question honestly made me want to give it up. I had no idea what was being asked or how to go about it. If you have the time could you give me an idea of your step by step process for coming to this solution, as far as you can remember? I generally understand what you've done here, but I want to know how to come to a similar conclusion when I encounter a similar problem.
me too Ç_Ç
The problem was written very academically. I feel your pain. Weirdly enough they provide an example later on that is actually the answer....
Ive been finding myself working backwards when I 'figure' a hard problem. I usually don't know why the solution works, so I try to reverse engineer all the elements that work it.
[[l,j,k] for l in range(x+1) for j in range(y+1) for k in range(z+1) if ((j+l+k) != n)]
we create a placeholder list [l,j,k] that iterates over a range from zero to the provided integers. since range is not inclusive, we can find this range by saying range(x+1) since it will provide a list of 0...1...2. we then define how each placeholder int iterates by each subsequent range declaration. After we provide the context of the iteration, we provide an if statement that the sum of all three iterables can't equal n. How the heck the computer does that magic...I don't know.
Someone correct me but this is the best I understand the solution. Even if I'm very wrong, just explaining it helps me understand it.
bro can you please explain the code?