1. 程式人生 > >【MoocPython】章節二作業

【MoocPython】章節二作業

def merge_sort(lst):

      if len(lst) <= 1:
          return lst
      middle = int(len(lst) / 2)
      left = merge_sort(lst[ :middle])
      right = merge_sort(lst[middle:])
      merged = []
      while  left and right :
          merged . append(left. pop(0) if left[0] <= right[0] else right .pop(0))
      merged . extend(right if right else left)
      return merged

data_lst = [6, 202, 100, 301, 38, 8, 1]
print(merge_sort(data_lst))

import datetime
dtstr="20181206"
dt=datetime.datetime.strptime(dtstr,"%Y%m%d")
another_dtstr=dtstr[:4]+'0101'
another_dt=datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(int((dt-another_dt).days)+1)

import string
s='ww-m/23 4j)'
letter=0
space=0
digit=0
other=0
for c in s:
      if c.isalpha():
            letter+=1
      elif c.isspace():
            space+=1
      elif c.isdigit():
            digit+=1
      else:
            other+=1
print("There are %d letters,%d spaces,%d digits and %d other characters in your string."%(letter,space,digit,other))

triange =[1]
print(triange)

triange.append(0)
n=10
for i in range(1,n):
      newline=[]
      for j in range(i+1):
            value=triange[j]+triange[-j-1]
            newline.append(value)
      print(newline)
      triange=newline

      triange.append(0)