1. 下面程序的运行结果是 5 4 5 8
def scope():
n=4
m=5
print(m,n,end=' ')
n=5
t=8
scope()
print(n,t)
2. 下面程序的运行结果是 1 6
l=[1]
def fun():
l.append(6)
print(*l)
fun()
3. 下面程序的运行结果是 20 20
a=10
def func():
global a
a=20
print(a,end=' ')
func()
print(a)
4. 下面程序的运行结果是 8
b, c=2, 4
def g_func(d):
global a
a=d*c
g_func(b)
print(a)
5.下面程序运行结果是 a a a good good good good good
def nprintf(message,n):
for i in range(n):
print(message,end=" ")
nprintf("a",3)
nprintf(n=5,message="good")
6.下面程序的输出是什么?(字符串不要加引号) o
lst=[(1,"one"),(2,"two"),(3,"three"),(4,"four")]
lst.sort(key=lambda x:x[1])
print(lst[3][1][2])
7. 在Python中,函数是对象,可以像其他数据对象一样使用。下面程序的输出是 11 22 33
def func1():
print("11",end=" ")
def func2():
print("22",end=" ")
def func3():
print("33")
funclist=[func1,func2,func3]
for func in funclist:
func()
8. 下面程序的输出是 41
f = lambda p:p+5
t = lambda p:p*3
x=7
x=f(x)
x=t(x)
x=f(x)
print(x)
9. 下面程序的运行结果是 19
import math
def factors(x):
y=int(math.sqrt(x))
for i in range(2,y+1):
if (x%i ==0):
factors(x//i)
break
else:
print(x,end=' ')
return
factors(38)
10. 下面程序是冒泡排序的实现,请填空(答案中不要有空格) len(List)-1
def bubble(List):
for j in range(________,0,-1):
for i in range(0,j):
if List[i]>List[i+1]:List[i],List[i+1]=List[i+1],List[i] return List
testlist = [49, 38, 65, 97, 76, 13, 27, 49]
print( bubble(testlist))
11. 下面程序是选择排序的实现,请填空(答案中不要有空格) mi
def selSort(nums):
n = len(nums)
for bottom in range(n-1):
mi = bottom
for i in range(_________, n):
if nums[i] < nums[mi]:
mi = i
nums[bottom], nums[mi] = nums[mi], nums[bottom]
return nums
numbers = [49, 38, 65, 97, 76, 13, 27, 49]
print(selSort(numbers))
12.
def perm(choice,selected=[]):
if len(choice)==1:
print("".join(selected+choice))
else:
for i in range(len(choice)):
t=choice[i]
choice.remove(t)
selected.append(t)
perm(choice,selected)
choice.insert(i,t)
selected.pop()
first=["1","2","3"]
perm(first,selected=[])
上面程序的第四行输出是 231
13. 下面程序的运行结果是 7
def basic_lis(seq):
l=[1]*len(seq)
for cur ,val in enumerate(seq): #enumerate返回元素的"索引和值"
for pre in range(cur):
if seq[pre]<val:
l[cur]=max(l[cur],1+l[pre])
return max(l)
L=[49, 64, 17, 100, 86, 66, 78, 68, 87, 96, 19, 99, 35]
print(basic_lis(L))
14. 下面程序的运行结果是 12
def ins_sort_rec(seq, i):
if i == 0: return
ins_sort_rec(seq, i - 1)
j = i
while j > 0 and seq[j - 1] > seq[j]:
seq[j - 1], seq[j] = seq[j], seq[j - 1]
j -= 1
seq = [3,-6,79,45,8,12,6,8]
ins_sort_rec(seq, len(seq)-1)
print(seq[5])
15. 程序求素数个数。输出是 25
# Use generator to generate a stream of primes
def gen_primes():
n = 2
while True:
for x in range(2, n):
if n % x == 0:
break
else:
yield n
n += 1
lst=[]
for p in gen_primes():
if p>100:
break
else:
lst.append((p))
print(len(lst))
16. 下面程序的执行结果是 5 25
def deco(fun):
def wrapper(*args,**kwargs):
x=fun(*args,**kwargs)
return x
return wrapper
def add(a,b):
print(a+b,end=" ")
return a+b
add=deco(add)
print(add(1,4)*5)
17. 要想得到[0,100]
范围内的随机数,random.randint(0,n)
里的n应该是: 100