pytho学习旅途`# merge sort

归并排序

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

pytho学习旅途

import datetime
dtstr = "20181206"
# 转换当前格式日期
dt = datetime.datetime.strptime(dtstr,"%Y%m%d")
#print(dt)
# 切片获取年份
another_dtstr = dtstr[:4] + '0101'
#print(another_dtstr)
another_dt = datetime.datetime.strptime(another_dtstr,"%Y%m%d")
print(int((dt-another_dt).days)+1)

pytho学习旅途

# 输入一行字符,分别统计其中英文字母,空格,数,和其他字符数
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))

pytho学习旅途

# 打印杨辉三角的前十行

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)

相关文章:

  • 2022-01-18
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-10-29
  • 2022-01-07
  • 2021-11-21
猜你喜欢
  • 2021-11-30
  • 2021-09-11
  • 2022-01-11
  • 2021-05-01
  • 2021-06-26
  • 2021-10-07
  • 2021-11-28
相关资源
相似解决方案