Professional Documents
Culture Documents
100 미사용)
Closed Book, PC
def eq(x):
return x**2 - 1
2. 아래의
(25) 코드를 보고 물음에 답하시오
Python .
def myCalc(s):
total = 0
tmp = "0"
for s1 in s:
if s1 in "0123456789.":
tmp += s1
elif s1 == "=":
return total
else:
if s1 == "+":
total = total + float(tmp)
if s1 == "-":
total = total - float(tmp)
if s1 == "*":
total = total * float(tmp)
print("total =", total)
tmp = "0"
뒷면 계속...
- 1 -
3. (20) Recursion 을 사용해 구현한 factorial 계산 함수 factR() 이 있다 .
def factR(n):
"""Assumes int n > 0. Returns n!"""
if n == 1:
return n
else:
return n*factR(n - 1)
def testFact(n):
print(n, "! =", factR(n))
4 ! = 24
(b) 의 계산과정에서
Factorial 이 되는 횟수를 함수의 마지막 줄로 문을
factR() call testFact() print
Hint: global
4 ! = 24
Total no of calls of factR during calculation = ...
로 제공되는 함수의 사용 없이 구현
의 이용
sum(), max(), min() .
- 2 -