没有合适的资源?快使用搜索试试~ 我知道了~
用Python实现冒泡排序算法。 def bs(a): # a = name of list b=len(a)-1 # minus 1 because we always compare 2 adjacent values for x in range(b): for y in range(b-x): if a[y]>a[y+1]: a[y],a[y+1]=a[y+1],a[y] return a a=[32,5,3,6,7,54,87] bs(a) 输出结果为: [3, 5, 6, 7, 32, 54, 87]
资源推荐
资源详情
资源评论
1.请写出冒泡排序。
#冒泡排序:n*n
def bubbleSort(array):
maxindex = len(array)-1
maxValue = array[maxindex]
k=0
while maxindex:
for i in range(1,maxindex):
if array[i-1]>array[i]:
temp = array[i]
array[i] = array[i-1]
array[i-1] = temp
k+=1
maxindex -=1
print(k)
return array
2.1~9999 数列中数字 3 出现的次数。用递推方法解出。
def count_digit(number):
return len(str(number))
def countThree(digit):
if not isinstance(digit,int):
raise TypeError('number is not int')
# digit = len(str(number))
if(digit <=0):
return 0
if(digit ==1):
return 1
return 10*countThree(digit-1) + 10 **(digit-1)
print(countThree(count_digit(9999)))
3.从一个数组中找出前 4 个最大的数,用最优解。
#快速排序:最快的 n*logN
def qiuckSort(list):
if len(list)<2:
return list
mid = list[0]
left = [i for i in list[1:] if i <= mid]
right = [i for i in list[1:] if i > mid]
finallyList = qiuckSort(left)+[mid] + qiuckSort(right)
return finallyList
array = [3, 0, 1, 832,23,45, 5, 5, 6,46, 9, 56, 897]
print(qiuckSort(array)[-4:])
4.写一段程序,删除字符串 a 中包含的字符串 b,举例 输入 a = "asdw",b = "sd" 返回 字符串
“aw”,并且测试这个程序。
def delBString(a,b):
if not isinstance(a,str):
raise TypeError("a is not str")
if not isinstance(b,str):
raise TypeError("b is not str")
if len(a) < len(b):
raise Exception('a length must large to b length')
result = []
flag = False
i=0
la = len(a)
lb = len(b)
while i <la:
j = 0
while j < lb:
if i+j < la and a[i+j] == b[j]:
j += 1
else :
j += 1
flag = False
break
flag = True
if flag:
i += lb
else:
result.append(a[i])
i += 1
return "".join(result)
测试用例:
class TestdelInnerStringFunctions():
def setUp(self):
pass
def tearDown(self):
pass
def test_nomorl1(self):
assert delBString('asdqwe','we') == 'asdq'
def test_nomorl2(self):
assert delBString('asdqwe','0') == 'asdqwe'
def test_nomorl3(self):
assert delBString('测试 asdqwe','we') == '测试 asdq'
def test_nomorl4(self):
assert delBString('测试 asdqwe','测试') == 'asdqwe'
def test_nomorl5(self):
assert delBString('asdqwe','') == 'asdqwe'
def test_nomorl6(self):
with pytest.raises(TypeError):
delBString('', 0)
def test_nomorl7(self):
with pytest.raises(TypeError):
delBString(0, 'as')
def test_nomorl8(self):
with pytest.raises(TypeError):
delBString(True)
def test_nomorl9(self):
with pytest.raises(Exception) as excinfo:
delBString('acd','acde')
assert "a length must large to b length" in str(excinfo.value)
assert excinfo.type == Exception
5.写一个方法,把字符串转为数字,比如 str="1234",变成 int 1234。并且测试这个程序。
def StrToInt(a):
res ,mult,flag = 0,1,1
if not isinstance(a,str):
raise TypeError("a is not str")
if a[0] =='-' or a[0] == '+':
if a[0] == '-':
flag = -1
a = a[1:]
for i in range(len(a)-1,-1,-1):
if '9' >=a[i] >= '0':
剩余12页未读,继续阅读
资源评论
菜鸟学识
- 粉丝: 3141
- 资源: 109
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- python-leetcode题解之156-Binary-Tree-Upside-Down.py
- python-leetcode题解之155-Min-Stack.py
- python-leetcode题解之154-Find-Minimum-in-Rotated-Sorted-Array-II.py
- python-leetcode题解之153-Find-Minimum-in-Rotated-Sorted-Array.py
- python-leetcode题解之152-Maximum-Product-Subarray.py
- python-leetcode题解之151-Reverse-Words-in-a-String.py
- python-leetcode题解之150-Evaluate-Reverse-Polish-Notation.py
- python-leetcode题解之149-Max-Points-on-a-Line.py
- GoogleCybersecurityTimeline_Vertical_RGB.pdf
- SemEval2014数据集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功