무냐의 개발일지
[OSSU] <MITx 6.00.1x_Intro CS> Week2/ Problem Set 2 본문
👩💻 문제 #1 (minimum금액 갚을 때, 1년 후 최종 balance)
Problem 1 - Paying Debt off in a Year
Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly payment required by the credit card company each month.
The following variables contain values as described below:
- balance - the outstanding balance on the credit card
- annualInterestRate - annual interest rate as a decimal
- monthlyPaymentRate - minimum monthly payment rate as a decimal
For each month, calculate statements on the monthly payment and remaining balance. At the end of 12 months, print out the remaining balance. Be sure to print out no more than two decimal digits of accuracy - so print
Remaining balance: 813.41
instead of
Remaining balance: 813.4141998135
So your program only prints out one thing: the remaining balance at the end of the year in the format:
Remaining balance: 4784.0
A summary of the required math is found below:
Monthly interest rate= (Annual interest rate) / 12.0
Minimum monthly payment = (Minimum monthly payment rate) x (Previous balance)
Monthly unpaid balance = (Previous balance) - (Minimum monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
해설 ) 신용카드 내야할 돈을 balance라고 본다
balance (b) = 0
지불한 돈 payment (p)
내야할 돈 b-p
안 낸 돈에 대한 이자 연이율 r --> 내야할 이자 : (b-p)* (1 + r/12)
그럼 다음 달의 balance --> (b-p) * (1 + r/12) --> 내고 남은 빚에 대해 이자 붙어서 지급해야겠지
💡 풀이
#problem #1
def creditbalance(balance, annualInterestRate, monthlyPaymentRate):
#새로운 balance
n = 0
while n != 12:
balance = balance*(1-monthlyPaymentRate) * (1 + annualInterestRate/12)
n += 1
print('Remaining balance : ', (round(balance,2)))
creditbalance(5000, 0.18, 0.02)
#결과
4691.11
✍️ 해설
비교적 쉽게 풀어냈다. 12번을 반복적으로 계산해야 되니까 ! 횟수 계산만 잘 해주면 될 듯
🥳 배운점
재무일 했던 게 쓸데가 있구나? ㅋㅋㅋ
👩💻 문제 #2 (1년 후 전액 상환하기 위해, 고정된 minimum payment 금액 구하기)
💡 풀이
#problem #2
def Payingoff(balance, annualInterestRate):
monthlyInterestRate= annualInterestRate/12
monthlyPayment = 10
original_balance = balance
while True:
for _ in range(12):
balance = (balance - monthlyPayment)*(1 + monthlyInterestRate)
if balance <= 0:
break
else:
balance = original_balance
monthlyPayment += 10
print('Lowest Payment: ', monthlyPayment)
Payingoff(5000, 0.12)
✍️ 해설
12번 반복 상환한 최종 balance 를 일단 구한다.
그 balance가 0보다 작아지는 순간, break 이고 우리는 monthlyPayment를 알맞게 구한거다
아직 0이 안된 경우에는, balance를 기존 balance로 다시 초기화 하고, monthlyPayment를 증가시켜준다
🥳 배운점
약간의 도움을 받았다 ㅜ
original balance를 따로 저장해둬서, for 문 안에 내가 원하는 값이 안나온 경우, monthlyPayment금액만 변경해준 채로 다시 초깃값부터 시작해야 된다는 점을 간과했다
👩💻 문제 #3 (2번에서 Bisection search 사용해서 좀 더 빠르게)
Well then, how can we calculate a more accurate fixed monthly payment than we did in Problem 2 without running into the problem of slow code? We can make this program run faster using a technique introduced in lecture - bisection search!
- lower bound : 이자율 0% 면, original balance / 12 만큼 매달 상환하면 된다
- upper bound : 한 번도 갚지 않아서 이자가 쭉 쌓인 값이 최대값이겠지 --> (original balance * (1 + monthly 이자율)^12) / 12
Write a program that uses these bounds and bisection search to find the smallest monthly payment to the cent (no more multiples of $10) such that we can pay off the debt within a year. Try it out with large inputs, and notice how fast it is (try the same large inputs in your solution to Problem 2 to compare!). Produce the same return value as you did in Problem 2.
💡 풀이
#problem #3
def Payoff(balance, annualInterestRate):
monthlyInterestRate= annualInterestRate/12
original_balance = balance
lower = balance/12
upper = (balance*(1 + monthlyInterestRate)**12)/12
while abs(balance) > 0.01:
balance = original_balance
mid = (lower+upper)/2
for _ in range(12):
balance = (balance - mid)*(1+monthlyInterestRate)
if balance > 0:
lower = mid
elif balance < 0:
upper = mid
print('Lowerst Payment: ',round(mid, 2))
Payoff(320000, 0.2)
✍️ 해설
2번 문제랑 똑같이 balance 계산하는 수식을 for문 안에 써주고, 0보다 크고 작음에 따라 lower, upper bound를 조정해준다.
그리고 다시 while문으로 올라가기 때문에, 이 때 다시 balance를 초기값으로 초기화해주고, mid값도 새로 갱신해주고서 다시 루프를 시작한다.
'OSSU_CS coursework' 카테고리의 다른 글
[OSSU] <UBCx HtC1x_How to Code> / 3a_How to Design Worlds (0) | 2024.04.25 |
---|---|
[OSSU] <UBCx HtC1x_How to Code> / 2_How to Design Data (0) | 2024.04.24 |
[OSSU] <MITx 6.00.1x_Intro CS> Final Exam (0) | 2024.04.23 |
복잡한 문제를 쉽게 풀어주는 Recursion (Divide & Conquer algorithm), String (1) | 2024.04.17 |
[OSSU] <MITx 6.00.1x_Intro CS> Week1/ Problem Set 1 (0) | 2024.04.16 |