List Comprehensions in Python - Hacker Rank Solution


List Comprehensions in Python - Hacker Rank Solution


Problem


Tutorial :

Concept :
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. These examples might help. The simplest form of a list comprehension is:
[ expression-involving-loop-variable for loop-variable in sequence ]
This will step over every element in a sequence, successively setting the loop-variable equal to every element one at a time. It will then build up a list by evaluating the expression-involving-loop-variable for each one. This eliminates the need to use lambda forms and generally produces a much more readable code than using map() and a more compact code than using a for loop.
>> ListOfNumbers = [ x for x in range(10) ] # List of integers from 0 to 9
>> ListOfNumbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List comprehensions can be nested where they take the following form:
[ expression-involving-loop-variables for outer-loop-variable in outer-sequence for inner-loop-variable in inner-sequence ]

This is equivalent to writing:
results = []
for outer_loop_variable in outer_sequence:
    for inner_loop_variable in inner_sequence:
        results.append( expression_involving_loop_variables )

The final form of list comprehension involves creating a list and filtering it similar to using the filter() method. The filtering form of list comprehension takes the following form:
[ expression-involving-loop-variable for loop-variable in sequence if boolean-expression-involving-loop-variable ]
This form is similar to the simple form of list comprehension, but it evaluates boolean-expression-involving-loop-variable for every item. It also only keeps those members for which the boolean expression is True.
>> ListOfThreeMultiples = [x for x in range(10) if x % 3 == 0] # Multiples of 3 below 10
>> ListOfThreeMultiples
[0, 3, 6, 9]



Problem Statement :

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. You have to 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



Input Format :

Four integers X,Y,Z and N 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 arr
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 2 :

[[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], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# List Comprehensions in Python - Hacker Rank Solution
if __name__ == '__main__':
    x = int(raw_input())
    y = int(raw_input())
    z = int(raw_input())
    n = int(raw_input())
    # List Comprehensions in Python - Hacker Rank Solution START
    output = [];
    abc = [];
    for X in range(x+1):
        for Y in range(y+1):
            for Z in range(z+1):
                if X+Y+Z != n:
                    abc = [X,Y,Z];
                    output.append(abc);
print(output);
# List Comprehensions in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
5 Comments
  • Hari Sreekanth Upadrasta
    Hari Sreekanth Upadrasta Saturday, December 12, 2020

    [(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]

    • Unknown
      Unknown Thursday, January 21, 2021

      in lexicographic increasing order

  • Pawans
    Pawans Thursday, September 16, 2021

    What is the use of n in this.

    • Camooo
      Camooo Sunday, February 20, 2022

      This isn't using list comprehension

  • Anonymous
    Anonymous Sunday, October 02, 2022

    it is told in the question to use list comprehension
    so you ur long ans is correct also not correct because it dont follow the question
    the answer should be
    listComprehensions = [[i,j,k] for i in range(z +1) for j in range(y + 1) for k in range(z + 1) if (i + j + k) != n];
    print(listComprehensions)

Add Comment
comment url