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.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Python
  3. Basic Data Types
  4. List Comprehensions
  5. Discussions

List Comprehensions

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 676 Discussions, By:

votes

Please Login in order to post a comment

  • gkeswani92 6 years ago+ 0 comments
    x, y, z, n = int(input()), int(input()), int(input()), int(input())
    print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if a + b + c != n ])
    
    340|
    Permalink
  • cassio_augcs 2 years ago+ 0 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'.
                    listijk.append([i,j,k])
    print(listijk)
    

    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.

    162|
    Permalink
  • fisher6 4 years ago+ 0 comments
    print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if sum([i,j,k]) != n])
    
    58|
    Permalink
  • gabobaby 6 years ago+ 0 comments

    The problem states that it wants the output list printed in 'increasing order'. It should be more clear what this means. I initially interpreted this to mean that [1,1,0] would come before [3,0,0] because 1+1+0=2 is less than 3+0+0=3. But this is not the order that the comprehension list method would generate the list. And would likely involve some method of sorting a 3D array that many new users would not know at this point, so I'm assuming this is not what is meant. This problem is not clarified with the chosen example values of x=1, y=1, z=1, and N=2.

    The problem could be addressed by just using larger example values, allowing us to infer from the example output which interpretation of 'increasing order' is intended.

    30|
    Permalink
  • scintilla 6 years ago+ 0 comments

    What does N represent? Why should X+Y+Z not equal N? And what do the subscript i's represent? I don't understand the question.

    26|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature