字母排序
s = ‘bfzajciot’ #raw_input(‘input = ‘)
x = s[0]
y = s[0]
n = 0
for i in s:
if n+1 < len(s):
if i <= s[n+1]:
x += s[n+1]
else:
if len(x)>len(y):
y = x
x = s[n+1]
else:
x = s[n+1]
n += 1
else:
break
if len(x) > len(y):
y = x
print ‘Longest substring in alphabetical \
order is:’,y
Using bisection search to make the program faster
balance = 999999
annualInterestRate = 0.18
b = balance
MonthlyInterestRate = annualInterestRate/12.0
lower = balance/12.0
upper = (balance * (1+MonthlyInterestRate)**12) / 12.0
pay = (lower + upper)/2
n=0
i=0
while True:
balance = b
for x in range(1,13):
UpdatedBalanceEachMonth= (balance-pay) *(1+ MonthlyInterestRate)
balance = UpdatedBalanceEachMonth
print 'balance=',balance,'pay=',pay
if balance < -0.02:
upper = pay
pay = lower +(upper - lower)/2
elif balance > 0.02:
lower = pay
pay = pay +(upper - lower)/2
else:
break
n+=1
print 'Lowest Payment:%.2f'%pay