Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68787ce

Browse files
Add files via upload
1 parent 584c978 commit 68787ce

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎List_Comprehention.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 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,
2+
3+
# Input Format
4+
5+
# Four integers
6+
# and
7+
8+
# each on four separate lines, respectively.
9+
10+
# Constraints
11+
12+
# Print the list in lexicographic increasing order.
13+
14+
# Sample Input 0
15+
16+
# 1
17+
# 1
18+
# 1
19+
# 2
20+
21+
# Sample Output 0
22+
23+
# [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
24+
25+
# Explanation 0
26+
27+
# Concept
28+
29+
# 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.
30+
31+
# 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.
32+
33+
# 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
34+
# Other smaller codes may also exist, but using list comprehensions is always a good option. Code using list comprehensions:
35+
36+
# 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 )]
37+
38+
# Sample Input 1
39+
40+
# 2
41+
# 2
42+
# 2
43+
# 2
44+
45+
# Sample Output 1
46+
47+
# [[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]]
48+
49+
50+
51+
if __name__ == '__main__':
52+
x = int(raw_input())
53+
y = int(raw_input())
54+
z = int(raw_input())
55+
n = int(raw_input())
56+
57+
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)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /