[Python 문제풀이] 12일차

2020. 7. 20. 11:0003. Resources/Python

728x90
반응형

Day_12

Intro

Q19. areEquallyStrong

Call two arms equally strong if the heaviest weights they each are able to lift are equal.

Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms.

Given your and your friend's arms' lifting capabilities find out if you two are equally strong.

제출 코드

def areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight):
    if yourLeft >= yourRight:
        yourStrong = yourLeft
        yourWeak = yourRight
    else:
        yourStrong = yourRight
        yourWeak = yourLeft

    if friendsLeft >= friendsRight:
        friendsStrong = friendsLeft
        friendsWeak = friendsRight
    else:
        friendsStrong = friendsRight
        friendsWeak = friendsLeft

    if (yourStrong == friendsStrong) and (yourWeak == friendsWeak):
        return True
    else:
        return False

작성 흐름

# Case: true
# yourLeft = 10
# yourRight = 15
# friendsLeft = 15
# friendsRight = 10

# Case: false
yourLeft = 15
yourRight = 10
friendsLeft = 15
friendsRight = 9

# 맨 처음엔 둘의 합을 가지고 계산하는 조건을 넣으려고 했으나, 생각해보니 강한 팔과 약한 팔의 값이 같으면 합은 당연히 같은 거라서 따로 조건을 넣어줄 필요가 업성서 뺐음
# yourWeight = yourLeft + yourRight
# friendsWeight = friendsLeft + friendsRight

# 나의 강한 팔 값 찾기
if yourLeft >= yourRight:
    yourStrong = yourLeft
    yourWeak = yourRight
else:
    yourStrong = yourRight
    yourWeak = yourLeft

# 친구의 강한 팔 값 찾기
if friendsLeft >= friendsRight:
    friendsStrong = friendsLeft
    friendsWeak = friendsRight
else:
    friendsStrong = friendsRight
    friendsWeak = friendsLeft

# 비교해서 값 리턴하기
if (yourStrong == friendsStrong) and (yourWeak == friendsWeak):
    print("true")
else:
    print("false")

# if yourWeight == friendsWeight:
#     print("True")
# else:
#     print("False")

Q20. arrayMaximalAdjacentDifference

Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.

제출 코드

def arrayMaximalAdjacentDifference(inputArray):
    difflist = []
    for i in range(len(inputArray)-1):
        difflist.append(abs(inputArray[i]-inputArray[i+1]))

    return max(difflist)

작성 흐름

# Case
# inputArray = [2, 4, 1, 0]
inputArray = [-1, 4, 10, 3, -2]

# 차이 값을 넣을 리스트 선언
difflist = []

# 전체 배열 돌면서 차이의 절대값 넣기
for i in range(len(inputArray)-1):
    difflist.append(abs(inputArray[i]-inputArray[i+1]))

print(max(difflist))
반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 14일차  (0) 2020.07.22
[Python 문제풀이] 13일차  (0) 2020.07.21
[Python 문제풀이] 11일차  (0) 2020.07.19
[Python 문제풀이] 10일차  (0) 2020.07.18
[Python 문제풀이] 9일차  (0) 2020.07.17