[캐글] Courses - Python(3)

HO94·2021년 6월 23일
0

캐글

목록 보기
5/17

2021.06.23

5. Loops and List Comprehensions

6. Strings and Dictionaries (Tutorial)

for문과 list comprehension 대해 공부했다.
list comprehensioin을 어렴풋이 알고만 있었는데, 이번 파트에서 좀 더 알게됐다.

  1. R and Python have some libraries (like numpy and pandas) compare each element of the list to 2 (i.e. do an 'element-wise' comparison) and give us a list of booleans like [False, False, True, True].
    Implement a function that reproduces this behaviour, returning a list of booleans corresponding to whether the corresponding element is greater than n.
def elementwise_greater_than(L, thresh):
    """
    Return a list with the same length as L, where the value at index i is 
    True if L[i] is greater than thresh, and False otherwise.
    elementwise_greater_than([1, 2, 3, 4], 2)
    [False, False, True, True]
    """
    return [i > thresh for i in L]
# Check your answer
q2.check()

이렇게 풀면 되나,,? 하고 풀고 채점했더니 맞았다..!

그리고 4번 문제
카지노 슬롯실행의 평균 이득(손실)액을 구하는 문젠데,,

  1. On average, how much money can you expect to gain (or lose) every time you play the machine? The casi keeps it a secret, but you can estimate the average value of each pull using a technique called the Monte Carlo method. To estimate the average outcome, we simulate the scenario many times, and return the average result.
    Complete the following function to calculate the average value per play of the slot machine.
from random import *
def estimate_average_slot_payout(n_runs):
    """Run the slot machine n_runs times and return the average net profit per run.
    Example calls (note that return value is nondeterministic!):
    =>estimate_average_slot_payout(1)
    -1
    => estimate_average_slot_payout(1)
    0.5
    """
    payout = []
    for i in range(n_runs):
        payout.append(round(uniform(0.0, 100.0), 3) - 1.0)
    return sum(payout) / len(payout)

uniform으로 난수를 생성하고 소수점 셋째자리에서 -1 달러한 금액을 나오게 했는데,,
대부분 0이 나와야하는걸 어떻게 해야하는지 모르겠다.
답을 보고 풀어보려했는데 답에 코드가 없어서 일단 보류

6. Strings and Dictionaries는 튜토리얼만 봤고, 문제는 내일 풀 예정이다.

0개의 댓글