List Comprehensions | HackerRank
List Comprehensions | HackerRank
You will learn about list comprehensions.
www.hackerrank.com
Goal
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.
Solution
You can simply make a condition statement {(i + j + k) != n} to append all (i,j,k) combinations into list and print the list.
There are two simple ways to solve this problem. You can use simply multiple loops, but you can use easily loop comprehensions.
1) The for statement
# List Comprehensions
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
results = []
for i in range( x + 1):
for j in range( y + 1):
for k in range(z + 1):
if (i + j + k) != n:
results.append([i, j, k])
print(results)
# List Comprehensions
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
results = [[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(results)
'software Engineering > (Python)HackerRank' 카테고리의 다른 글
[Python] HackerRank - Nested Lists (0) | 2023.01.31 |
---|---|
[Python] HackerRank - Find the Runner-Up Score! (0) | 2023.01.29 |
[Python] HackerRank - Print Function (0) | 2023.01.27 |
[Python] HackerRank - Write a function (0) | 2023.01.26 |
[Python] HackerRank - Loops (0) | 2023.01.21 |