We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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):
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'.
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.
List Comprehensions
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):
(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:
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.