Program/[Kaggle] 30 Days of ML

[30 Days of ML] Day 4

uding9 2021. 8. 8. 16:50
반응형

Tutorial


Booleans

코드에 직접 True or False 를 입력하는 것이 아닌 boolean 연산자를 사용해 boolean 값을 얻음


Comparison Operations

Operation Description Operation Description
a == b a equal to b a != b a not equal to b
a < b a less than b a > b a greater than b
a <= b a less than or equal to b a >= b a greater than or equal to b

n==2는 n과 2가 같은지 비교하는 것이고 n=2 는 n의 값을 2로 변경하는 것임


Combining Boolean Values

boolean 값을 and, or, not 을 사용하여 결합 할 수 있음

andor 보다 먼저 평가되지만 괄호를 사용하는 것이 더 명확함


Conditionals

if , elif , else 구조


Boolean conversion

bool() 함수를 통해 bool로 변환할 수 있음

print(bool(1))
print(bool(0))
print(bool('abc'))
print(bool(''))
True
False
True
False

Exercise


4.

def concise_is_negative(number):
    return number<0 # 답
    # return True if number < 0 else False # 내 코드

내 코드 보다 답이 좀 더 간결한 것 같음

비교 연산자 만으로도 bool 값을 얻을 수 있다는 것을 잊지 말자!


5.

def exactly_one_sauce(ketchup, mustard, onion):
    """Return whether the customer wants either ketchup or mustard, but not both.
    (You may be familiar with this operation under the name "exclusive or")
    """
    return (ketchup and not mustard) or (mustard and not ketchup)

이 문제는 소스 하나만 선택하는 것임

(ketchup O and mustard X) 혹은 (ketchup X 이고 mustard O) 이므로 위와 같이 표현됨


6.

정수에 대해 bool()을 호출하면 0인 경우 False를 반환하고 0이 아닌 경우 True를 반환함

print(bool(100))
print(bool(0))
print(bool(-9))
True
False
True
def exactly_one_topping(ketchup, mustard, onion):
    """Return whether the customer wants exactly one of the three available toppings
    on their hot dog.
    """
    return (int(ketchup)+int(mustard)+int(onion)) == 1 # 내 답
#   return (ketchup+mustard+onion) == 1 # 좀 더 간결하게

boolean에 위와 같이 더하기를 수행하면 암시적(내재적)으로 정수로 변환하여 수행함


7.

(~ing)

블랙잭 문제인데 좀 더 고민을 할 필요가 있는 것 같음

# 푸는 중
def should_hit(dealer_total, player_total, player_low_aces, player_high_aces):
    """Return True if the player should hit (request another card) given the current game
    state, or False if the player should stay.
    When calculating a hand's total value, we count aces as "high" (with value 11) if doing so
    doesn't bring the total above 21, otherwise we count them as low (with value 1). 
    For example, if the player's hand is {A, A, A, 7}, we will count it as 11 + 1 + 1 + 7,
    and therefore set player_total=20, player_low_aces=2, player_high_aces=1.

    """
    if (player_total<dealer_total) and (player_total<=6):
        return True
    else:
        return False

#     if player_total>=20:
#         return False
#     else:
#         return True
반응형