直接选择排序:
直接选择排序(Straight Select Sorting) 也是一种简单的排序方法,它的基本思想是:第一次从R[0]R[n-1]中选取最小值,与R[0]交换,第二次从R[1]R[n-1]中选取最小值,与R[1]交换,…,第i次从R[i-1]R[n-1]中选取最小值,与R[i-1]交换,…,第n-1次从R[n-2]R[n-1]中选取最小值,与R[n-2]交换,总共通过n-1次,得到一个按排序码从小到大排列的有序序列。
def selectSort(numList):
length=len(numList)
for i in range(length-1):
for j in range(i+1,length):
if numList[j] < numList[i]:
numList[i],numList[j]=numList[j],numList[i]
return numList
print(selectSort([3,2,1,5,8,7,6,99,33]))
冒泡排序算法的原理如下:
def bubbleSort(nums):
for i in range(len(nums) - 1): # 这个循环负责设置冒泡排序进行的次数
for j in range(len(nums) - i - 1): # j为列表下标
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
return nums
print(bubbleSort([45, 32, 8, 33, 12, 22, 19, 97]))
def count_str_char(my_str):
count=[0,0,0,0]
for index in range(len(my_str)):
if my_str[index].isdigit():
count[0]+=1
elif my_str[index].isspace():
count[2]+=1
elif my_str[index].isalpha():
count[1]+=1
else :
count[3]+=1
print("包含"+str(count[0])+"个数字,"+str(count[1])+"个英文字母,"+str(count[2])+"个空白字符,"+str(count[3])+"个其他字符")
count_str_char("asd fgt 12131 **** &")
结果:
包含5个数字,6个英文字母,4个空白字符,5个其他字符
def fn(n):
sum=0
for i in range(1,n+1):
sum += i * i * i
return sum
print(fn(11))
def fn(n):
sum=1
for i in range(1,n+1):
sum *= i
return sum
print(fn(14))
def distinct(list):
copiedList=[]
for index in list:
if index in list and index not in copiedList:
copiedList.append(index)
return copiedList
list=[1,1,2,2,3,3,4,4,5,5,9,8,9]
print(distinct(list))
import random
def createTuple(n):
tupleCreated=[]
for i in range(n):
numberGet=random.randint(0,100)
while numberGet in tupleCreated:
numberGet=random.randint(0,100)
tupleCreated.append(numberGet)
return tuple(tupleCreated)
res1=createTuple(101)
print('长度:'+str(len(res1))+'--->'+str(res1))
结果:
长度:101--->(54, 4, 81, 47, 59, 49, 29, 38, 32, 51, 65, 22, 66, 50, 56, 48, 19, 69, 64, 40, 42, 20, 35, 1, 23, 46, 17, 84, 18, 11, 90, 2, 43, 34, 45, 86, 94, 26, 82, 79, 61, 80, 24, 31, 39, 83, 15, 27, 41, 44, 71, 63, 7, 91, 88, 77, 36, 52, 37, 3, 55, 87, 62, 92, 97, 85, 75, 25, 68, 76, 10, 95, 57, 98, 9, 53, 5, 33, 58, 74, 21, 8, 60, 13, 14, 67, 78, 100, 6, 99, 0, 73, 93, 12, 96, 16, 89, 30, 72, 28, 70)
import random
def getRandomUpper(n):
if n > 26:
return "error"
str="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
tupleCreated=[]
for i in range(n):
charGet=str[random.randint(0,len(str)-1)]
while charGet in tupleCreated:
charGet=str[random.randint(0,len(str)-1)]
tupleCreated.append(charGet)
return tuple(tupleCreated)
print(getRandomUpper(10))
例如,当参数为3时,先输出:
1 2 3
4 5 6
7 8 9
后输出:
1 4 7
2 5 8
3 6 9
def printSquareMatrix(n):
def dir1():
for i in range(1,n * n + 1):
if i < 10 and i % n != 0:
print(i,end=" ")
elif i % n != 0:
print(i,end=" ")
else:
print(i)
def dir2():
for i in range(1,n+1):
for j in range(i,i+(n*n-n)+1,n):
if j < 10:
print(j,end=" ")
else:
print(j,end=" ")
print()
print("转置前:")
dir1()
print("转置后:")
dir2()
printSquareMatrix(7)
结果:
转置前:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
转置后:
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
7 14 21 28 35 42 49