- 파이썬 연습겸 R입문 중간고사 문제 파이썬으로 풀어봄

1번 문제

(a)

2**-5 + 2**3
8.03125

(b)

33**0.5
5.744562646538029

(c)

sum_ = 0
for k in range(1, 101):
    sum_ += 1 / ((k+1) ** 2)
    
print(sum_)
0.6350819297898338

- 리스트 컴프리핸션

sum([1/((k+1)**2) for k in range(1, 101)])
0.6350819297898338

2번 문제

(a)

import numpy as np
x = np.arange(-10, 10.5, 0.5)
print(x)
[-10.   -9.5  -9.   -8.5  -8.   -7.5  -7.   -6.5  -6.   -5.5  -5.   -4.5
  -4.   -3.5  -3.   -2.5  -2.   -1.5  -1.   -0.5   0.    0.5   1.    1.5
   2.    2.5   3.    3.5   4.    4.5   5.    5.5   6.    6.5   7.    7.5
   8.    8.5   9.    9.5  10. ]

(b)

def f(x):
    if abs(x) > 5:
        return x
    elif abs(x) < 2:
        return 0
    else:
        return 5

y = list(map(f, x))
print(y)
[-10.0, -9.5, -9.0, -8.5, -8.0, -7.5, -7.0, -6.5, -6.0, -5.5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0]

3번 문제

from math import exp

def f(x):
    return 2*x + 3

def g(x):
    return exp(x) / (1 + exp(x))

def h(x):
    return max(x, 0)

x1 = list(map(f, x))
x2 = list(map(g, x1))
x3 = list(map(h, x2))
print(x3)
[4.1399375473943306e-08, 1.12535162055095e-07, 3.059022269256247e-07, 8.315280276641321e-07, 2.2603242979035742e-06, 6.144174602214718e-06, 1.670142184809518e-05, 4.5397868702434395e-05, 0.00012339457598623172, 0.00033535013046647816, 0.0009110511944006454, 0.0024726231566347748, 0.006692850924284856, 0.017986209962091555, 0.04742587317756679, 0.11920292202211755, 0.2689414213699951, 0.5, 0.7310585786300049, 0.8807970779778824, 0.9525741268224333, 0.9820137900379085, 0.9933071490757152, 0.9975273768433652, 0.9990889488055994, 0.9996646498695335, 0.9998766054240138, 0.9999546021312976, 0.9999832985781519, 0.9999938558253978, 0.999997739675702, 0.9999991684719723, 0.9999996940977731, 0.9999998874648379, 0.9999999586006245, 0.9999999847700205, 0.9999999943972036, 0.9999999979388464, 0.999999999241744, 0.9999999997210532, 0.9999999998973812]

4번 문제

(a)

import matplotlib.pyplot as plt

n = 10
l = [0]*n

for i in range(1, n+1):
    l[i-1]  = 1/(2**i)

l_ = np.cumsum(l)

plt.plot(list(range(n)), l_)
[<matplotlib.lines.Line2D at 0x15879d61f70>]
sum = 0
n = 10
x = 5 ## 예시

for i in range(n):
    sum += x**i
    
print(exp(x),"\n", sum)
148.4131591025766 
 2441406

- 모든 x에 대해 성립해야 되는데 아니다

5번 문제

x = a = 10 #예시
salary = a

for i in range(1, 19):
    a *= 1.08
    salary += a
    
for j in range(20, 29):
    a *= 0.75
    salary += a
    
print(salary / x)
52.53420212516463

6번 문제

- (a) : 참

- (b) : 거짓

- (c) : 거짓

- (d) : 거짓

7번 문제

n = 100
doors = [False] * (n+1)

for i in range(1, n+1):
    for j in range(i, n+1):
        if j % i == 0:
            if doors[j] == True:
                doors[j] = False
            else:
                doors[j] = True

sum_ = 0
for k in range(1, n+1):
    if doors[k] == True:
        sum_ += 1
        
print(sum_)
10

8번 문제

dist = 35
dp = [0] * 456 
dp2 = [0] * 456
x = np.array(range(1, 457)) / 100

for i in np.arange(10, 0, -0.5):
    for j in range(456):
        
        if dp2[j] == 0:
            dp[j] = round(dp[j] + round(x[j], 3), 3)
            
        if x[j] > i and dp2[j] == 0: ## 0은 False이다 
            if dp[j] - round((x[j] - i), 3) < dist:
                dp2[j] = -1
            elif dp[j] - round((x[j] - i), 3) >= dist:
                dp2[j] = 1
            
        elif x[j] <= i and dp[j] >= dist and dp2[j] == 0:
            dp2[j] = 1
    x += 1

(a)

dp2[0] # 사망
-1
dp2[66] # 사망
-1
dp2[217] # 생존
1
dp2[455] # 사망
-1

(b)

sum_ = 0
for k in range(456):
    if dp2[k] == 1:
        sum_ += 1
        
print(sum_)
85

- 전체 생존자는 85명

- 고찰

- 이것때문에 정신나감

- 그리고 처음 dp 설정할 때 전부 0으로 해야됐는데 -1로 해서 또 정신나감

a = np.array([0, 0])
b = [0] * 2
a[0] += 0.01
print(a[0])
0

- 왜 0인지 이해가 안됨

- 왜 0.01이 아니냐?

b[0] += 0.01
print(b[0])
0.01

- ㅋㅋㅋㅋㅋㅋㅋㅋㅋ

- 넘파이 어레이는 0인데 그냥 리스트는 0.01이다

- 이유는 모른다

9번 문제

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/miruetoto/yechan/master/_notebooks/round2.csv")
mat = np.matrix(df)
mat.shape
(5513, 2)

(a)

plt.plot(mat[:,0], mat[:,1], '.k')
[<matplotlib.lines.Line2D at 0x200885d74c0>]

(b)

mat[0,:]
matrix([[ 12, 313]], dtype=int64)

(c)

np.matrix([[0,-1], [-1,0]]) @ np.matrix([12,313]).T
matrix([[-313],
        [ -12]])

(d)

mat2 = mat

for i in range(5513):
    mat2[i,:] = (np.matrix([[0,-1], [-1,0]]) @ mat[i,:].T).T

(e)

plt.plot(mat2[:,0], mat2[:,1], '.r')
[<matplotlib.lines.Line2D at 0x200886dc190>]

- 위에서 mat2 = mat이라고 했는데 그러면 안됨

id(mat2), id(mat)
(2203309972272, 2203309972272)

- 메모리 주소가 동일해서 mat2를 바꾸면 mat도 바뀌어서 아래와 같이 된다

plt.plot(mat[:,0], mat[:,1], '.r')
[<matplotlib.lines.Line2D at 0x20088745760>]

- 그럼 어떻게? ---> 깊은복사!

- mat 다시 초기화

mat = np.matrix(df)
plt.plot(mat[:,0], mat[:,1], '.k')
[<matplotlib.lines.Line2D at 0x200887ad7c0>]
mat3 = mat.copy()
for i in range(5513):
    mat3[i,:] = (np.matrix([[0,-1], [-1,0]]) @ mat[i,:].T).T
plt.plot(mat3[:,0], mat2[:,1], '.r')
[<matplotlib.lines.Line2D at 0x20088815970>]
id(mat), id(mat3)
(2201311857904, 2201313255712)

- 메모리 주소가 다르다

plt.plot(mat[:,0], mat[:,1], '.k')
[<matplotlib.lines.Line2D at 0x20088877850>]

- mat3를 바꿨지만 깊은복사를 하였기에 mat에는 영향을 끼치지 않음