Introduction to computer homework 04 (based on Python)

Posted by anikin on Wed, 27 Oct 2021 12:32:17 +0200

2. (10 points) program exercise 2.2.1 of introduction to computer,

i_number = input("Please enter a binary number")
sum = 0
for i in i_number:
    sum += sum
    sum += int(i)
    
print(sum)

3. (10 points) program exercise 2.2.2 of introduction to computer

in_num = int(input("Please enter a decimal number"))
L = []
while(in_num  >= 1):
    modu = in_num % 2
    in_num = int(in_num / 2)
    L.append(modu)

out_num = ""
for i in range(len(L)-1,-1,-1):
    
    out_num += str(L[i])

print(out_num)

4. (15 points) introduction to programming slot machine game exercise 2.5.4

import random
money = int(input("Please enter the principal"))
test_number = int(input("Please enter the number of tests"))
initial = money
count1 = 0
count2 = 0
for i in range(test_number):
    r = random.random()
    money -= 1
    if(r < 0.6):prize = 0 
    elif(r < 0.8):
        prize = 1
        count1 += 1
    elif(r < 0.9):
        prize = 2
        count1 += 1
    else:
        prize = 3
        count1 += 1
    money += prize
    count2 += 1
    if( money == 0):break
print("We played together",count2,"second")
print("The number of victories is",count1,"second")
print("The amount of money left in hand is",money)

6. (10 points) introduction to programming exercise 2.14. Please do not complete this program recursively. We may ask you how to complete this problem recursively in future exams, but please don't use it now.

def ite_merge(in1,in2):
    out = []
    for i in range(len(in1)+len(in2)):
        if len(in1) == 0 and  len(in2) == 0:
            return out
        elif len(in1) == 0:
            out.append(in2.pop(0))
        elif len(in2) == 0:
            out.append(in1.pop(0))
        elif in1[0] <= in2[0]:
            out.append(in1.pop(0))
        else:
            out.append(in2.pop(0))
    print(out)

ite_merge([1,5,6],[2,3,7])

7,
My uncle wants a loan to buy a house. He knows that you are a computer major at China Normal University and thinks you know everything, so please explain and analyze the calculation method of the loan for him. After you learn basic programming, your natural skills are greatly improved, so you are going to write a Python program to answer your uncle's questions.
This topic includes three levels: exploration, programming and analysis. We should explore the principle of "equal principal and interest", the most common loan method of banks. We should use programming to calculate the repayment amount, the principal and interest paid each year, and finally analyze the advantages and disadvantages. After completing this topic, you are much better than your middle school classmates in the first year of business school!
First, assume that the principal is A, the monthly interest rate is r, and the loan term is n months,
a) (5 points) using the method of "equal principal and interest", please deduce and explain the formula of monthly repayment amount, and please explore online by yourself;
b) (10 points) write a Python program, assuming that the principal of the loan is 800000, the annual interest rate is 6% (the monthly interest rate is 1 / 12 of it), and the loan is 30 years
(1) Calculate the monthly repayment amount;

loan = 8e5
pro = 0.005
month = 30 * 12
repay = loan*pro*pow((1.0+pro),month) /(pow((1.0+pro),month) - 1)
print(repay)

2) List the principal and interest returned each year and all the principal and interest paid; Think about why you have to pay so much interest? Your uncle asked you if most of the money you paid a few years ago was interest?

loan = 8e5
int_rate = 0.005
month = 30 * 12
repay = loan*int_rate*pow((1.0+int_rate),month) /(pow((1.0+int_rate),month)-1)

def pri_int(loan,int_rate,year):
    month = 12*year
    repay_month = loan*int_rate*pow((1.0+int_rate),month) /(pow((1.0+int_rate),month)-1)
    
    
    for i in range(year):
        sum_int = 0
        sum_loan = 0
        repay_loan = 0
        for j in range(12):
            repay_int = loan * int_rate
            sum_int += repay_int
            repay_loan = repay_month - repay_int
            loan -= repay_loan
            sum_loan += repay_loan
        print('The first',i+1,"The repayment of annual interest is",sum_int)
        print('The first',i+1,"The repayment of annual principal is",sum_loan)
            
pri_int(loan,int_rate,30)

c) (5 points) repeat the previous question b), but the loan term is 20 years; Is the interest much less?
d) (10 points) in order to save interest, my uncle wants to repay early. As mentioned earlier, the loan term is 20 years, but at the end of the third year, my uncle repaid the bank principal of 200000 (note the principal) in advance. Note that at this time, my uncle returned the principal returned every month for 3 years and the additional principal of 200000. If the monthly repayment amount remains unchanged in the future (the repayment amount in the last month can be less), how many months will it take to pay off the loan that would have taken 240 months? It is not allowed to use log to calculate on the formula.

loan = 8e5
int_rate = 0.005
month = 240
repay = loan*int_rate*pow((1.0+int_rate),month) /(pow((1.0+int_rate),month)-1)

#The objective is to calculate the number of months remaining to be repaid

repay_month = loan*int_rate*pow((1.0+int_rate),month) /(pow((1.0+int_rate),month)-1)
    

for i in range(3):
   
        
        
    month_load = 0
    for j in range(12):#What should be taught every year
        month_int = loan * int_rate
        month_load = repay_month - month_int
        loan -= month_load
#Repeat for three years to process interest and principal

loan -= 2e5  

count = 0
while(loan > 0):
    month_int = loan * int_rate
    month_load = repay_month - month_int
    loan -= month_load
    count += 1
 
print(count+36)

e) (10 points) another loan calculation method is called "equal principal", please check it online. Please take a 20-year loan of 800000 yuan with an annual interest rate of 6%. For simplicity, take "month" as the unit and not "day" as the unit. Please program to calculate the total amount of interest paid by using the method of equal principal and list the amount repaid each month in the previous 12 months. Please explain the difference between it and equal principal and interest from the perspective of interest. Please analyze the two, equal principal and interest and equal principal, Why do banks like to calculate loans with equal principal and interest?

loan = 8e5
int_rate = 0.005
month = 30 * 12
def pri(loan,int_rate,year):
    month = 12 * year
    month_load = loan / month
    sum_int = 0
    for i in range(year):
        year_int = 0
        for j in range(12):
            month_int = loan * int_rate
            loan -= month_load
            year_int += month_int
            #print("j+1", "month's interest expense is", month_int)
        sum_int += year_int
        #print("year_int", i, "year's interest expense is")
    print("The total interest is",sum_int)
pri(loan,int_rate,30)

Topics: Python