2022-06-30
2022-06-30 本文已影响0人
木马音响积木
la=[]
for i in range(1,10):
for j in range(1,6):
print(str(i*j).center(6),end='')
la.append(i*j)
print()
la.sort()
print(la)
def gui(la):
def he(la, a, b, c, d):
t, m= [],a
while a < b and c < d:
if la[a] <= la[c]:
t.append(la[a])
a += 1
else:
t.append(la[c])
c += 1
la[m:d] = t+la[a:b]+la[c:d]
#start
n, step = len(la), 1
while step < n:
i = f = 0
while i < n:
start, stop = i, i + step
if stop > n:
stop = n
if f:
he(la, a, b, start, stop)
f = 0
else:
f, a, b = 1, start, stop
i += step
step += step
x = [11, 3, 2, 6, 4, 5, 8, 7, 9]
gui(x)
print(x)
'''from random import randint
la=[1,2,3,4]
import itertools
for a,b in itertools.pairwise(la):
print(a,b)
i=0
he=0
while he<=1234567:
i+=1
he+=i
print(i,he)
la=(i for i in range(1234567))
print(type(la))
for i,j in enumerate(itertools.accumulate(la)):
if j>1234567:
print(j,i)
break
print(1571*1572/2)
e=10000
a=1
def cc(a):
return a*(a+1)//2
x=0
while a<e:
m=a+e >>1
x+=1
print(x,m)
if cc(m)<=1234567:
a=m+1
else:
e=m
print(e,a)
print(1571/13)
'''
# 敢去华为 面试,你小子还有点胆量
# 我说一下我的思路
'''
1,第一是特判断 , 长度不够3,杀;
正好 3 ,和 判断后,输出
2,从大到小排序
3,检查 ,如果钱三强 加起来 小于等于100 ,答案得到
如果后三名,加起来 ,大于100,,返回 输入数据错误
4,说明能找到值
先定义 big 就是最后的返回值,答案
last = la[-1] +la[-2] #备用,后期优化用,
r=right=size-1
5,第一层 遍历, 下标,从 0 到 size -2(不可用边界)
for i in range(size -2):
6,这里面要在用双指针之前,先 进行特判
如果 a, first3 = la[i] +la[i+1] +la[i+2] ==100:return 100
b, first3 < 100:
以i 开始的,最大已经找到了,i 可以不用循环了,
因为,再后面的无论怎么选,都不会大于first3 ,,返回 它和big 中大的那个
c, 此时 first3 >100:
hou = la[i] + la[-2] +la[-1]
1) if hou >100: 这个i 是 废了 , continue
2) if hou ==100 : return 100
3) 没法投机了
干 , 两边夹逼
left =i+1
r=right
代码略
class Solution:
def gan(self,la):
size=len(la)
if size<=2:return '输入数据有误'
if size==3:
t=la[0]+la[1]+la[2]
if t>100:
return '输入数据有误'
else:
return t
#2 排序啦
la.sort(reverse =True)
#备用 变量区
last = la[-1] +la[-2]
r=right=size-1
t = la[0]+la[1]+la[2]
if t<=100:return t
hou = la[-3] + la[-2] +la[-1]
if hou >100:
return '输入数据有误'
big =-float('inf')
print('--------70--------')
#5 遍历 i
for i in range(size-2 ):
first3 = la[i] +la[i+1] +la[i+2]
if first3 ==100:
print(75,'行', la[i] ,la[i+1] ,la[i+2])
return 100
if first3<100:
#尝试刷新 big
if first3 >big:
#此时不用找了,这是最后一个满足条件的,最后一个i ,big 就不更新了,
#print(96)
print('83行', la[i] ,la[i+1] ,la[i+2])
return first3
else:
print(999999999999)
return big
#此时 first3 >100:
hou = la[i] + la[-2] +la[-1]
#hou =la[i] +last
#print(la[i] )
if hou >100:
continue
elif hou ==100:
print('93===',la[i] , la[-2] ,la[-1])
return 100
# 双指针夹逼
left = i+1
r= right
cha = 100-la[i]
while left <r:
#print(left,r,100)
v = la[left] +la[r]
if v ==cha:
print('107=====',la[left] ,la[r],la[i])
return 100
elif v >cha:
left +=1
else:
c=v+la[i]
if c>big:
big=c
#print(120)
print('116===',la[left] ,la[r],la[i])
r-=1
return big
cc=Solution()
#x=[i for i in range(90,2,-5)]
x=[]
for i in range(30):
w= randint(-3000,2000)
x.append(w)
x=[50,45,32,31,30,3,2,1]
#x.sort(reverse=True)
#print(x)
print(cc.gan(x))
for i in range(100000):
cat = randint(1,7)
zhua=0
#print(cat)
for a in (2,3,4,5,6,6,5,4,3,2):
#print(cat,999)
if a==cat:
print(cat,'done')
zhua=1
break
suiji =randint(0,1)
if suiji==0:suiji=-1
cat+=suiji
if cat ==0:
cat=2
elif cat ==8:
cat=6
if zhua==0:
print('cat is a ghost')
def gan(args):
if args==1:
return 1
return gan(args-1)*2 +2
print(gan(8))
print(gan(2))
la= [1, 2, 4, 7, 11, 6,8,9,34,45,56,67,78,79,99,12,13,14,23,24]
def swap(a,b):la[a],la[b]= la[b],la[a]
def pao(la):
n=len(la)
for i in range(n):
for j in range(n-1-i):
if la[j] >la[j+1]:
swap(j,j+1)
pao(la)
print(la)
def select(la):
n =len(la)
for i in range(n-1):
index =i
for j in range(i+1,n):
if la[j] <la[index]:
index =j
swap(i,index)
select(la)
print(la)
def insert(la):
n=len(la)
for i in range(1,n):
j=i
while j and la[j-1] > la[j]:
swap(j, j-1)
j-=1
insert(la)
print(la)
def gui(la,a,e):
if a<e:
m=a+e>>1
gui(la,a,m)
gui(la,m+1,e)
he(la,a,m,e)
def he(la,a,m,e):
k=0
i=a
j=m+1
t=[0]*(e-a+1)
while i<=m and j<=e:
if la[i]<=la[j]:
t[k]=la[i]
k+=1
i+=1
else:
t[k]=la[j]
k+=1
j+=1
if i<=m:
t[k:]=la[i:m+1]
if j<=e:
t[k:]=la[j:e+1]
la[a:e+1]=t
gui(la,0,len(la)-1)
print(la)
def dui(la):
def adj(la,pos,r):
er=pos+pos+1
if er+1<r and la[er+1]>la[er]:
er+=1
if er<r and la[er] >la[pos]:
swap(er,pos)
adj(la,er,r)
r=len(la)
half=r//2
for i in range(half,-1,-1):
adj(la,i,r)
for i in range(r-1,0,-1):
swap(0,i)
adj(la,0,i)
dui(la)
print(la)
def fast(la,a,e):
if a<e:
t=la[e]
slow=a
for f in range(a,e):
if la[f]<t:
swap(slow,f)
slow+=1
swap(slow,e)
fast(la,a,slow-1)
fast(la, slow+1,e)
fast(la,0,len(la)-1)
print(la)
def sort(a):
left =0
high = r= len(a) - 1
def kk(a, left, high):
if left < high:
r = high
p = a[r]
zuo = left
while left < high:
while left < high and a[left] <= p:
left += 1
while left < high and a[high] >= p:
high -= 1
a[left], a[high] = a[high], a[left]
a[left], a[r] = a[r], a[left]
p = left
kk(a, zuo, p - 1)
kk(a, p + 1, r)
kk(a, 0, high)
x = [11, 3, 2, 6, 4, 5, 8, 7, 9]
import random
for jj in range(10000):
x=[]
for i in range(500):
x.append(random.randint(50,1450))
t=x[:]
sort(x)
w=sorted(t)
if w !=x:
print(x)
print(w)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
a=t=root
st=[]
if p.right:#需要返回右子树的最左节点
a=p.right
while a:
w,a=a,a.left
return w
while t.val != p.val:
st.append(t)
if t.val<p.val:t=t.right
else: t=t.left
#if st==[]:return #说明是根节点,没有右子树
#if not a.right:return
while st:
node =st.pop()
if node.val>p.val:
return node
return
'''
"""
w=len(la)
half,yu =divmod(w,2)
def suibian(la):
low =a=0
r=high=w-1
while 1:
'''
p=la[r]
while a<r:
while a<r and la[r]>=p:
r-=1
while a<r and la[a]<p:
a+=1
if a<r:
la[a],la[r] = la[r],la[a]
a+=1
r-=1
la[a],la[high] =la[high],la[a]'''
if a<r:
t=la[r]
slow= a
for i in range(a,r):
if la[i]<t:
la[slow],la[i] =la[i],la[slow]
slow+=1
la[slow],la[r]=la[r],la[slow]
a=slow
#check a
if a==half:
break
elif a>half:
high= r = a-1
a=low
else:
low = a = a+1
r = high
if yu:
pass
else:
pass
print(la,999)
#sum(la[a+1:]) -sum(la[:a+1])
print(suibian(la))
la.sort()
print(half)
print(sum(la[11:]) -sum(la[:11]))
"""
'''
la=[]
chaju=0
qian=hou=1
for i in range(1,100):
qian = qian +chaju
chaju+=1
hou = qian + chaju
chaju+=1
t=[qian,hou]
la.append(t)
qian=hou
if hou+chaju>=200:break
print(la)
c=[]
cha=0
p=1
for i in range(20):
p=p+cha
c.append(p)
cha+=1
print(c)
la=[]
t=0
for i in range(0,20,2):
la.append([c[i],c[i+1]])
w=sum(la[-1])
print(w-t)
t=w
print(la)
def dui(la):
def adj(la,pos,r):
er=pos+pos+1
if er+1<r and la[er+1]>la[er]:
er+=1
if er<r and la[er]>la[pos]:
la[pos],la[er] = la[er],la[pos]
adj(la,er,r)
k=len(la)
half=k//2 -1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1 ,0,-1):
la[0],la[i] = la[i],la[0]
adj(la,0,i)
la=[3,2,1,6,5,4,9,8,7]
dui(la)
print(la)
'''
"""
def dsort(la):
def adj(aa,pos,r):#大顶堆
#print(1,la)
er = pos+pos+1
if er +1 <r and aa[er] < aa[er+1]: #向右看齐
er +=1
if er <r and aa[pos] < aa[er]: #石头下山
aa[pos],aa[er]=aa[er],aa[pos]
adj(aa,er,r)
k=len(la)
half = k//2-1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,-1,-1):
la[0],la[i]=la[i],la[0]
adj(la,0,i)
la=[3,2,1,6,5,4,9,8,7]
dsort(la)
print(la)
#---------------------------------------------
la=[3,2,1,6,5,4,9,8,7]
def paopao(la):
k=len(la)
for i in range(k-1):
for j in range(k-1-i):
if la[j] >la[j+1]:
la[j],la[j+1]=la[j+1],la[j]
paopao(la)
print(la)
la=[3,2,1,6,5,4,9,8,7]
def insert(la):
k=len(la)
for i in range(1,k):
for j in range(i,0,-1):
if la[j]<la[j-1]:
la[j],la[j-1]=la[j-1],la[j]
else:break
insert(la)
print(la)
la=[3,2,1,6,5,4,9,8,7]
def se(la):
k=len(la)
for i in range(k-1):
index =i
for j in range(i+1,k):
if la[j]<la[index]:
index=j
la[i],la[index]=la[index],la[i]
se(la)
print(la)
def ss(la,left,r):
if left<r:
t=la[r]
slow=left
for i in range(left,r):
if la[i]<t:
la[slow],la[i] =la[i],la[slow]
slow+=1
la[slow],la[r]=la[r],la[slow]
ss(la,left,slow-1)
ss(la,slow+1,r)
la=[3,2,1,6,5,4,9,8,7]
ss(la,0,8)
print(la)
def ss(la,left,r):
if left<r:
t=la[left]
slow=left
for i in range(left+1,r+1):
if la[i]<t:
slow+=1
la[slow],la[i] =la[i],la[slow]
la[slow],la[left]=la[left],la[slow]
ss(la,left,slow-1)
ss(la,slow+1,r)
la=[3,2,1,6,5,4,9,8,7]
ss(la,0,8)
print(la)
la=[3,2,1,6,5,4,9,8,7]
def m(la,a,r):
if a<r: #看好了,先分后和
mid =a+r>>1
m(la,a,mid)
m(la,mid+1,r)
mm(la,a,mid,r)
def mm(la,a,mid,r): #合并
t=[0] *(r-a+1)
i,k=a,0
j=mid+1
while i<=mid and j<=r:
if la[i]<la[j]:
t[k]=la[i]
i+=1
else:
t[k]=la[j]
j+=1
k+=1
while i<=mid:
t[k]=la[i]
k+=1
i+=1
while j<=r:
t[k]=la[j]
j+=1
k+=1
la[a:r+1]=t[:k]
m(la,0,8)
print(la)
"""
'''
计算机程序的构造与解释(Python) CS61A
进阶
数据结构与算法 CS61B
分布式 MIT 6.824
MIT6.830(数据库)
MIT6.828(操作系统)
CS61c:计算机体系结构
1 eval 及其他内置函数 https://www.runoob.com/python/python-func-eval.html
2 format 格式化字符串 https://www.w3school.com.cn/python/python_string_formatting.asp https://www.cnblogs.com/JackLi07/p/9832137.html
3 字符串处理函数 https://www.w3school.com.cn/python/python_strings.asp
4 异常处理,try except https://www.w3school.com.cn/python/python_try_except.asp
5 列表 操作函数 https://www.w3school.com.cn/python/python_lists.asp
6 字典 操作函数 https://www.w3school.com.cn/python/python_dictionaries.asp
7 csv 格式读写
8 文件打开和关闭,读写
9 第三方库的安装 https://www.w3school.com.cn/python/python_pip.asp
10 turtle 库
11 random 库 https://www.cnblogs.com/cwp-bg/p/7468475.html
12 pyinstaller 必考 http://c.biancheng.net/view/2690.html '''
'''
def dsort(la):
def adj(la,pos,r):
er=pos+pos +1
if er+1<r and la[er+1] > la[er]: #找高个,向右看齐
er+=1
if er<r and la[er] > la[pos]:
la[er] ,la[pos]= la[pos], la[er] #上任,并告诉下属选人方法
adj(la,er,r)
k=len(la)
half = k//2 -1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,-1,-1):
la[0],la[i] =la[i],la[0]
adj(la,0,i)
la=[1,2,15,4,3,5,7,6,9,8,25,5,5,5]
dsort(la)
print(la)
def dui(la):
def adj(la,pos,k):
er=pos+pos+1
if er+1<k and la[er+1]>la[er]:
er+=1
if er<k and la[er]>la[pos]:
la[er],la[pos]=la[pos],la[er]
adj(la,er,k)
k=len(la)
half=k//2-1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,-1,-1):
la[i],la[0]=la[0],la[i]
adj(la,0,i)
la=[1,2,15,4,3,5,7,6,9,8,25,5,5,5]
dsort(la)
print(la)
def guibing(la,left,r):
if left <r:
m=left+r>>1
guibing(la,left,m)
guibing(la,m+1,r)
he(la,left,m,r)
def he(la,left,m,r):
t=[0]*(r-left+1)
k,i,j=0,left,m+1
while i<=m and j<=r:
if la[i]<=la[j]:
t[k]=la[i]
i+=1
k+=1
else:
t[k]=la[j]
j+=1
k+=1
if i<=m:
t[k:]=la[i:m+1]
elif j<=r:
t[k:]=la[j:r+1]
la[left:r+1]=t
"""
while i<=m:
t[k]=la[i]
k+=1
i+=1
while j<=r:
t[k]=la[j]
k+=1
j+=1
"""
#la[left:r+1]=t
la=[1,2,15,4,3,5,7,7,6,9,8,25,34,45,56,767,4,5,6,7]
guibing(la,0,19)
print(la)'''
"""
public class RandomIdGenerator implements IdGenerator { 实现接口
private static final Logger logger = LoggerFactory.getLogger(RandomIdGenerator.class);
@Override
public String generate() throws IdGenerationFailureException { 异常------334
String substrOfHostName = null;
try {
substrOfHostName = getLastFieldOfHostName();
} catch (UnknownHostException e) { 异常再次包装,隐藏下层细节
throw new IdGenerationFailureException("...", e); 异常------334
}
long currentTimeMillis = System.currentTimeMillis();
String randomString = generateRandomAlphameric(8);
String id = String.format("%s-%d-%s",
substrOfHostName, currentTimeMillis, randomString);
return id;
}
private String getLastFieldOfHostName() throws UnknownHostException{ --私有
String substrOfHostName = null;
String hostName = InetAddress.getLocalHost().getHostName();
if (hostName == null || hostName.isEmpty()) { --过滤--参数下传前判断
throw new UnknownHostException("...");
}
substrOfHostName = getLastSubstrSplittedByDot(hostName); 参数有保障了
return substrOfHostName;
}
@VisibleForTesting
protected String getLastSubstrSplittedByDot(String hostName) {
if (hostName == null || hostName.isEmpty()) { --入参判断
throw new IllegalArgumentException("...");
}
String[] tokens = hostName.split("\\.");
String substrOfHostName = tokens[tokens.length - 1];
return substrOfHostName;
}
@VisibleForTesting
protected String generateRandomAlphameric(int length) { --入参判断
if (length <= 0) {
throw new IllegalArgumentException("...");
}
char[] randomChars = new char[length];
int count = 0;
Random random = new Random();
while (count < length) {
int maxAscii = 'z';
int randomAscii = random.nextInt(maxAscii);
boolean isDigit= randomAscii >= '0' && randomAscii <= '9';
boolean isUppercase= randomAscii >= 'A' && randomAscii <= 'Z';
boolean isLowercase= randomAscii >= 'a' && randomAscii <= 'z';
if (isDigit|| isUppercase || isLowercase) {
randomChars[count] = (char) (randomAscii);
++count;
}
}
return new String(randomChars);
}
}
@VisibleForTesting
protected String generateRandomAlphameric(int length) { --入参判断
if (length <= 0) {
throw new IllegalArgumentException("...");
}
@VisibleForTesting
protected String generateRandomAlphameric(int length) throws IllegalArgumentException {
if (length <= 0) { --入参判断
throw new IllegalArgumentException("...");
}
提问:
抛出这个异常说明方法传入一个非法的或者不合适的参数。
举个例子:getUser(int username)方法,不允许传入空字符串或者null。但是有个调用的方法,没做检查,
传入了null或者空字符串,这时候getUser方法就应该要抛出IllegalArgumentException告诉调用者:hi!这个参数不能为empty或者null。
java.lang.IllegalArgumentException继承至RuntimeException,所以它是一个unchecked异常,它不需要在方法里加throws声明!
class Solution:
def helan_flag(self, nums) :
#三指针法,一个最右边,一个 slow ,fast ,缩写了
fast =c =s= slow=0
e=len(nums)-1
while c <= e:
if nums[c] ==0:
nums[c],nums[s] = nums[s],nums[c]
c += 1
s += 1
elif nums[c]==1:
c += 1
elif nums[c]==2:
nums[c],nums[e] = nums[e],nums[c]
e -= 1
"""
'''
#归并
def gui(la,a,r):
if a<r:
mid =a+r>>1
gui(la,a,mid)
gui(la,mid+1,r)
he(la,a,mid,r)
def he(la,a,mid,r):
i,j,k=a,mid+1,0
t=[0]*(r-a+1)
while i<= mid and j<=r:
if la[i]<=la[j]:
t[k],k,i=la[i],k+1,i+1
else:
t[k],k,j=la[j],k+1,j+1
if i<=mid:t[k:] =la[i:mid+1]
elif j<=r:t[k:] =la[j:r+1]
la[a:r+1] =t
la=[1,2,15,4,3,5,7,7,6,9,8,25,34,45,56,767,4,5,6,7]
gui(la,0,19)
print(la)
#快速
def qsort(la,a,r):
if a<r:
t=la[r]
slow=a
for i in range(a,r):
if la[i]<t:
la[i],la[slow]=la[slow],la[i]
slow+=1
la[slow],la[r] =t,la[slow]
qsort(la,a,slow -1)
qsort(la,slow +1,r)
la=[1,2,15,4,3,5,7,7,6,9,8,25,34,45,56,767,4,5,6,7]
qsort(la,0,19)
print(la)
#堆
def dui(la):
def adj(la,pos,r):
er=pos+pos+1
if er + 1 < r and la[er]<la[er+1]:
er+=1
if er<r and la[er] > la[pos]:
la[er],la[pos]= la[pos],la[er]
adj(la,er,r)
k=len(la)
half=k//2-1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,0,-1):
la[0],la[i] =la[i],la[0]
adj(la,0,i)
la=[1,2,15,4,3,5,7,7,6,9,8,25,34,45,56,76,7,4,5,6,799]
dui(la)
print(la)
def dsort(la):
def adj(la,pos,r):
er=pos+pos +1
if er+1<r and la[er+1] > la[er]: #找高个,向右看齐
er+=1
if er<r and la[er] > la[pos]:
la[er] ,la[pos]= la[pos], la[er] #上任,并告诉下属选人方法
adj(la,er,r)
k=len(la)
half = k//2 -1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,-1,-1):
la[0],la[i] =la[i],la[0]
adj(la,0,i)
def dui(la):
def adj(la,pos,k):
er=pos+pos+1
if er+1<k and la[er+1]>la[er]:
er+=1
if er<k and la[er]>la[pos]:
la[er],la[pos]=la[pos],la[er]
adj(la,er,k)
k=len(la)
half=k//2-1
for i in range(half,-1,-1):
adj(la,i,k)
for i in range(k-1,-1,-1):
la[i],la[0]=la[0],la[i]
adj(la,0,i)
la=[1,2,15,4,3,5,7,6,9,8,25,5,5,5]
dui(la)
print(la)
#冒泡
def pao(la):
k=len(la)
for i in range(k):
for j in range(k-1-i):
if la[j]>la[j+1]:
la[j],la[j+1]=la[j+1],la[j]
la=[1,2,15,4,3,5,7,6,9,8,25,5,5,5]
pao(la)
print(la)
#选择
def select(la):
k=len(la)
for i in range(k):
index=i
for j in range(i+1,k):
if la[j]<la[index]:
index=j
la[i],la[index]=la[index],la[i]
la=[1,2,15,4,3,5,7,6,9,8,25,5,5,5]
select(la)
print(la)
la=[3,2,1,6,5,4,9,8,7]
def se(la):
k=len(la)
for i in range(k-1):
index =i
for j in range(i+1,k):
if la[j]<la[index]:
index=j
la[i],la[index]=la[index],la[i]
#插入
la=[3,2,1,6,5,4,9,8,7]
def insert(la):
k=len(la)
for i in range(1,k):
for j in range(i,0,-1):
if la[j]<la[j-1]:
la[j],la[j-1]=la[j-1],la[j]
else:break
insert(la)
print(la)
def kuai(la,a,r):
if a<r:
t=la[r]
slow = a
for f in range(a,r):
if la[f] < t:
la[slow] ,la[f] = la[f],la[slow]
slow+=1
la[slow] ,la[r]= la[r],la[slow]
kuai(la, a, slow-1)
kuai(la ,slow+1, r)
la=[3,2,1,6,5,4,9,8,7,8,8,9,9,90]
kuai(la,0,13)
print(la)
def kuai(la,a,r):
if a<r:
t=la[a]
slow = a
for f in range(a+1,r+1):
if la[f] > t:
slow+=1
la[slow] ,la[f] = la[f],la[slow]
la[slow] ,la[a]= la[a],la[slow]
kuai(la, a, slow-1)
kuai(la ,slow+1, r)
la=[3,2,1,6,5,4,9,8,7,8,8,9,9,90]
kuai(la,0,13)
print(la)
'''