没出现过的数字
python
import random
n=int(input(""))
nums=[]
for i in range(n):
nums.append(random.randint(1,n))
print(nums)
lst=[]
for i in range(1,n):
if i not in nums:
lst.append(i)
print(lst)
替换空格
方法一(replace函数)
python
str="Hellow world"
print(str.replace(" ","%"))
方法二(手搓函数)
python
str="Hellow world"
def func(s):
str=""
for i in s:
if i !=" ":
str+=i
else:
str+="%"
return str
print(func(str))
快乐数
很巧妙的方法通过change()函数
python
def change(x):
sum=0
while x>0:
j=x%10
sum+=j*j
x=x//10
return sum
def happynum(n):
while n>9:#当两位数的时候进行转换
n=change(n)
if n==1:
return True
else:
return False
print(happynum(18))
立方根
math库里的pow函数
python
import math
n=int(input())
res=math.pow(n,1/3)
print(res)
最长公共前缀
先排序,拿最小的去比较
python
strs=["abca","abc","abca","abc","abcc","ab","abcccd"]
strs.sort()
s=""
m=len(strs[1])
for i in strs:
if i[0:m]!=strs[1][0:m]:
m-=1
print(strs[1][0:m])