You are viewing a single comment's thread. Return to all comments →
Ok, after hours trying to actually understand the problem, and after trying to solve it in reverse, I hope to bring some clarity for those who are having a hard time trying to solve or to understand what exactly needs to be done and what the codes posted here are actually doing. I did this using Python 3.
Ignoring the mathematical terms, the problem wants you to create a list in a concise way (that's called 'List Comprehension'). Which consists on creating a list using 'for' and, possibly, 'if' (in case you want an extra condition in that list).
In this case, it asks you to create multiple lists using 3 'for' clauses: i, j and k. In each theses clauses you will choose an range to vary from 0, which will be called x (for clause i), y (for clause j) and z (for clause k).
The challenge here is to create every possible list that will show all values from the chosen ranges (x, y and z) in a way that the the predecessor number will change to the next value from the range only AFTER the successor number shows all it's values first.
Before trying an example, first a reminder:
As seen in previous challenges, in a 'for' clause, the value of a range will vary (as a standard) from 0 to the predecessor number in that range. I.E.: for A in range(2): will output 0 and 1, but not 2.
Now, let's try to solve it with a simple example: I'll set both, x, y and z to 1, just to make it easier.
This is the formula in a clearer way (not in a single line, but I believe it's best for beginners to understand):
listijk =  #an empty list to be filled as following
for i in range(x + 1): #it will output 0, and will only output 1 after the next conditions ('for j' and 'for k') are met.
for j in range (y + 1): #it will output 0, and will only output 1 after the next condition ('for k') is met.
for k in range (z + 1): #it will output 0 and then 1.
listijk.append([i,j,k]) #it will add i, j and k values to the list
print(listijk) #print the list properly filled
(copy and paste the code in an code editor for clearer reading)
In the example above, that's what will happen behind the scenes, in this exact order:
i = 0, j = 0, k = 0
i = 0, j = 0, k = 1
i = 0, j = 1, k = 0
i = 0, j = 1, k = 1
i = 1, j = 0, k = 0
i = 1, j = 0, k = 1
i = 1, j = 1, k = 0
i = 1, j = 1, k = 1
It will print exactly like that:
[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]
As you can see, it made 8 different output variations.
Now, to the last part of the problem...
It creates one extra condition: it will NOT print the outputs which the sum of the values inherited by i, j and k equals to the integer 'n'.
So, this time, the code will become:
listijk = 
for i in range(x + 1):
for j in range (y + 1):
for k in range (z + 1):
if i + j + k != n: #before printing the result, it will exclude the output which 'i' + 'j' + 'k' is the same as 'n'.
In this case, let's suppose that 'n' is set to 2:
i = 0, j = 0, k = 0 #will print
i = 0, j = 0, k = 1 #will print
i = 0, j = 1, k = 0 #will print
i = 0, j = 1, k = 1 #will NOT print (1 + 1 = 2)
i = 1, j = 0, k = 0 #will print
i = 1, j = 0, k = 1 #will NOT print (1 + 1 = 2)
i = 1, j = 1, k = 0 #will NOT print (1 + 1 = 2)
i = 1, j = 1, k = 1 #will print
And then, it will print the following 5 outputs:
[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]
That's an extense explanation, but I'm just trying to make it as clear as possible for those who, just like me, are having a hard time figuring out what the problem wants and what the codes posted here and in the examples are really doing. Hope it helps someone.
Not wrong, but you have not used list comprehension. Consider a list called 'a' with even and odd numbers. Now suppose you want to create a list 'b' that contains only the even numbers of list 'a'.
One way to do this in the usual way with a loop:
Using list comprehension this would look like this single line:
The challenge in this problem is creating nested list comprehesions, which can be tricky. See PEP 202.
(Sorry for the broken formatting, but it's not possible to code-format in replies here in HR.)
Thanks a ton , it's because of you I was able to understand what's required from the problem
Thank you. The problem is actually lacking clearer explaination.