Saturday, February 23, 2019

List Comprehensions - Hacker Rank Solution

Let's learn about list comprehensions! You are given three integers  and  representing the dimensions of a cuboid along with an integer .
You have to print a list of all possible coordinates given by  on a 3D grid where the sum of is not equal to . Here, 
Input Format
Four integers  and  each on four separate lines, respectively.
Constraints
Print the list in lexicographic increasing order.
Sample Input 0
1
1
1
2
Sample Output 0
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
Explanation 0
Concept
You have already used lists in previous hacks. List comprehensions are an elegant way to build a list without having to use different for loops to append values one by one. This example might help.
Example: You are given two integers x and y . You need to find out the ordered pairs ( i , j ) , such that ( i + j ) is not equal to n and print them in lexicographic order.( 0 <= i <= x ) and ( 0 <= j <= y) This is the code if we dont use list comprehensions in Python.
python x = int ( raw_input()) y = int ( raw_input()) n = int ( raw_input()) ar = [] p = 0 for i in range ( x + 1 ) : for j in range( y + 1): if i+j != n: ar.append([]) ar[p] = [ i , j ] p+=1 print ar Other smaller codes may also exist, but using list comprehensions is always a good option. Code using list comprehensions:
python x = int ( raw_input()) y = int ( raw_input()) n = int ( raw_input()) print [ [ i, j] for i in range( x + 1) for j in range( y + 1) if ( ( i + j ) != n )]
Sample Input 1
2
2
2
2
Sample Output 1
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 2], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 2], [1, 1, 1]

List Comprehensions - Hacker Rank Solution

List comprehensions are an elegant way where lists can be built without having to use different for loops to append values one by one.
Solution

Python 2

a, b, c, n = [int(raw_input()) for _ in xrange(4)]
print [[x,y,z] for x in xrange(a + 1) for y in xrange(b + 1) for z in xrange(c + 1) if x + y + z != n]

10 comments:

  1. Could you please explain why we use a+1 , b+1 and c+1 ?

    And also could you please suggest me what is wrong with my code :

    if __name__ == '__main__':
    x,y,z,n = (int(input()) for _ in range(4))

    for x in range(x+1):
    for y in range(y+1):
    for z in range(z+1):
    if x+y+z != n:
    print([x,y,z])


    "I will be grateful for any help you can provide."

    ReplyDelete
    Replies
    1. Answer to your 1st question : he has used a+1,b+1,c+1 because there might be assignment issues that might give you error!

      and your code is perfectly alright but it's not printing the values the one after the other within a single list.

      use this following method instead:
      print([[a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if(a+b+c!=n)])

      The above code is similar to your logic but it prints the values inside a single list!

      Delete
    2. Because range() function will provide numbers from 0 to n-1

      Delete
    3. coz range function will give you parametres till n-1 if you give n as input .coz the function range(n) is not inclusive for n its exclusive for it. hope you get it.

      Delete
  2. if we use range x which means it will take 0 and 1 not 2
    range does not take the last value

    ReplyDelete
    Replies
    1. increment the range by adding +1 at the end. It will take the last value. for eg: for x in range(n+1)
      print(x)

      Delete
  3. if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    print( [[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 ) ])

    ReplyDelete
  4. if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    print( [[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 ) ])

    ReplyDelete

Powered by Blogger.