Commit f7390e87 by yangpengflag

âcommit homework day2 named 山脉数组的峰顶索引

parents
'''
实现冒泡排序
传入参数:nums 需要排序的数组
传入参数:isAsc 排序模式,升级还是降序,默认升序
'''
def bubbleSort(nums,isAsc):
for i in range(len(A)-1): # 外循环每一次把有序的数增加一个
for j in range(len(A)-i-1): # 内循环每次把无序部分中的最大值放到最上面
if(isAsc): # 是否升序
if(A[j] > A[j+1]):
A[j], A[j+1] = A[j+1], A[j]
else: # 降序
if (A[j] < A[j + 1]):
A[j], A[j + 1] = A[j + 1], A[j]
A=[3,4,2,22,7,8]
bubbleSort(A,isAsc=True)
print(A)
bubbleSort(A,isAsc=False)
print(A)
def peakIndexInMountainArray(self, A):
lenth = len(A)
for x in range(lenth):
if(A[x]>A[x+1]):
print(x)
return x
else:
continue
A= [0,1,0]
B = [0,2,3,0]
peakIndexInMountainArray(A,A)
peakIndexInMountainArray(B,B)
def peakIndexInMountainArray1(self, A):
idx = A.index(max(A))
print(idx)
return idx
A= [0,1,0]
B = [0,2,3,0]
peakIndexInMountainArray1(A,A)
peakIndexInMountainArray1(B,B)
def peakIndexInMountainArray2(self, A):
a = max(A)
for i in range(len(A)):
if A[i] == a:
print(i)
return i
A= [0,1,0]
B = [0,2,3,0]
peakIndexInMountainArray2(A,A)
peakIndexInMountainArray2(B,B)
# 这种写法好像有问题,待解决
def peakIndexInMountainArray3(self, A):
for i in range(len(A)):
while(A[i] < A[i+1]):
i+=1
print(i)
return i
A= [0,1,0]
B = [0,2,3,0]
peakIndexInMountainArray3(A,A)
peakIndexInMountainArray3(B,B)
def question_and_answer():
while True:
a = input("")
if (("贪心" or "贪心学院") and "做什么"in a):
print("贪心学院是一家高端重视售后服务的在线教育培训机构")
if "bye" in a:
print("拜拜,回聊!")
break
question_and_answer()
\ 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 to comment