MIT 6.00.1X problem set

problem set 1 (1)
count = 0
num = 0
while count < len(s):
    if s[count] in 'aeiou':
        num += 1
    count += 1
print('Number of vowels: '+str(num))

problem set 1 (2)  find bob (can overlapping)

num = 0
s = 'bobobo'
def find_bob(char):
    if char != 'bob':
       return 0
    else:
        return 1
for count in range (len(s)-2):
    char = s[count:count+3]
    num += find_bob(char)
print('Number of times bob occurs is: '+str(num))
    

problem set 1 (3)  find  第一个 longest substring 按照字母表顺序 

longest_substring = ''
s = 'abcbcd'
def find_substring(i,longest_substring):
    substring = s[i]
    while i < len(s)-1: 
         if s[i] <= s[i+1]:
               substring = substring + s[i+1]
               i += 1
         else:
               break
    if len(longest_substring) >= len(substring):
        return longest_substring
    else:
        return substring 
for i in range (len(s)):
     longest_substring = find_substring(i,longest_substring)
print('Longest substring in alphabetical order is: '+longest_substring)      

problem set 2 (1)  还钱 

pay = 10
balance = 4773
annualInterestRate = 0.2
while True:
    updatebalance = balance
    for i in range(11):
        updatebalance = (updatebalance-pay)*(annualInterestRate/12.0+1)
    if updatebalance - pay <=0:
        break
    else:
        pay += 10
print('Lowest Payment: '+str(pay))     

problem set 2 (2)  精确到小数点后2位 还钱

balance = 320000
annualInterestRate = 0.2
low = balance/12.0
monthlyrate = annualInterestRate/12.0
high = balance*(1+monthlyrate)**12/12.0
while True:
    updatebalance = balance
    pay = (low+high)/2
    for i in range(11):
        updatebalance = (updatebalance-pay)*(monthlyrate+1)    
    if updatebalance  - pay <=0:
        if updatebalance - pay > -0.01:
            break
        high = pay
    else:
        low = pay
pay = round(pay,2)       
print('Lowest Payment: '+str(pay))     

你可能感兴趣的:(MIT,6.00.1.X,学习笔记,学习笔记)