-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoListComprehensions-2.py
70 lines (65 loc) · 2.31 KB
/
doListComprehensions-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/env python3
#
# hackerrank.com
# https://www.hackerrank.com/challenges/list-comprehensions
#
# INPUT: four lines each with an integer
# specifying dimensions of cube X,Y,Z and
# constraint N
# OUTPUT: list of all possible coordinates (i,j,k) on 3D grid
# where the sum of i+j+k != N
# 0 <= i <= X
# 0 <= j <= Y
# 0 <= k <= Z
#
# Using list comprehensions
# ( to build lists without having to use different for loops to append)
# eliminates need for lambda function
# more readable than using map()
# more compact than using for loop
#
# 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 )
#
# add a filter:
#
# [ 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.
#
# multi-variate list comprehensions
# A list comprehension consists of brackets containing an expression
# followed by a for clause, then zero or more for or if clauses. The
# result will be a new list resulting from evaluating the expression
# in the context of the for and if clauses which follow it. For example,
# this listcomp combines the elements of two lists if they are not equal:
# >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
#
# nested:
# The initial expression in a list comprehension can be any arbitrary
# expression, including another list comprehension.
# >>> [[row[i] for row in matrix] for i in range(4)]
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
ans_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]
# Print the list in lexicographic increasing order.
ans_list.sort()
print(repr(ans_list))