Header Ad

HackerRank List comprehensions solution in python

In this HackerRank List Comprehensions problem solution in python, Let's learn about list comprehensions! You are given three integers x,y and z representing the dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to n. Here, 0<=i<=x; 0<=j<=y; 0<=k<=z. Please use list comprehensions rather than multiple loops, as a learning exercise.

HackerRank List comprehensions solution in python


Problem solution in Python 2 programming.

lst=[int(raw_input())+1 for i in range(4)]
print [[x,y,z] for x in range(lst[0]) for y in range(lst[1]) for z in range(lst[2]) if x+y+z!=lst[3]-1]


Problem solution in Python 3 programming.

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())

    print(list([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))



Problem solution in pypy programming.

if __name__ == '__main__':
    x = int(raw_input())
    y = int(raw_input())
    z = int(raw_input())
    n = int(raw_input())
    print [list([xx,yy,zz]) for xx in range(x+1) for yy in range(y+1) for zz in range(z+1) if xx+yy+zz !=n]


Problem solution in pypy3 programming.

if __name__ == '__main__':
    x = int(input()) + 1
    y = int(input()) + 1
    z = int(input()) + 1
    n = int(input())
    ans = [[i, j, k] for i in range(x) for j in range(y) for k in range(z) if ((i + j + k) != n)]
    print (ans)


Post a Comment

1 Comments

  1. or in pypy3:
    if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    ans = [[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)]
    print (ans)

    ReplyDelete