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))     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值