Commit 9236e539 by BellCodeEditor

auto save

parent 0db28b07
Showing with 69 additions and 5 deletions
......@@ -4,8 +4,10 @@ name='刘强'
power=66
hero=['赵一',30,'丁二',37,'孙五',52,'王猛',89,'周亮',98]
# 自动排序
for i in range(len(hero)):
if i%2==1 and hero[i]>=power:
hero.insert(i-1,name)
for i in range(1,len(hero),2):
if hero[i] >= power:
hero.insert(i-1,name)
hero.insert(i,power)
print(hero)
\ No newline at end of file
break
print(hero)
print(hero[-6::2])
\ No newline at end of file
s1 = list(input('字符串'))
for i in range(len(s1)):
for j in range(i,len(s1)):
if s1[i] > s1[j]:
s1[i],s1[j] = s1[j],s1[i]
print(s1)
low = 0
high = len(s1)-1
num = ord('s')
while True:
mid = low+(high-low)*(num-ord(s1[low]))//(ord(s1[high]) - ord(s1[low]))
if ord(s1[mid]) == num:
print(mid)
break
elif ord(s1[mid]) > num:
high = mid - 1
elif low > high:
print('no')
break
else:
low = mid + 1
#1.创建分治函数
def MergeSort(alist): #alist代表的是一个列表
'''
我们需要将alist不断的通过递归分成两个列表left , right 长度小于等于1的时候停止
'''
#设置基线条件 列表的长度小于2
if len(alist) < 2:
#将列表返回
return alist
#设置num 是切片分割成left 和 right 的分割点 ,num是列表的长度整除2
num = len(alist)//2
#对左边函数不断的进行递归赋值给列表left
left = MergeSort(alist[:num])
# 对右边边函数不断的进行递归赋值给列表right
right = MergeSort(alist[num:])
#对left和right进行归并
return Merge(left , right)
#2 创建归并的函数
def Merge(left , right):
#设置两个指针 指向数据的起始位置 (索引)
l = 0
r = 0
#创建一个列表(空间) 用来容纳 left 和right
result =[]
#当某一边的指针超过了列表的长度就结束
while l < len(left) and r < len(right):
#如果左边的列表数据小于右边的列表中的数据 ,就将较小的值添加到result列表中,并且指针要右边移动
if left[l] <= right[r]:
result.append(left[l])
l += 1
else:
result.append(right[r])
r += 1
#while循环结束就代表了有一方的指针大于等于列表的长度将没有添加完成的数据直接添加到列表的末尾
result += left[l:]
result += right[r:]
return result
alist = [8,4,5,7,1,3,6,2]
print(MergeSort(alist))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment