HackerRank 3D Surface Area
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# print(A)
# 9, (1+2+4), 9, (1+3+4), (1+2+1), (4+3+4), 5, 7
first = H * W
second = 0
for i in A:
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0) W = int(first_multiple_input1) A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A):
# Write your code here
# print(A)
# top and buttom
area = 2 * H * W
# checks the limit
def check(i, j):
return Ax+iy+j if 0 <= x+i < H and 0 <= y+j < W else 0 # remaining surfaces
for x in range(H):
for y in range(W):
# (0, 1), (0, -1), (1, 0), (1, 1)
for i, j in zip(xi, yi):
# gives area from adjacent cells
area += max(0, Axy - check(i, j)) return area
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0) W = int(first_multiple_input1) A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=GYaLhyfGRhM