0% found this document useful (0 votes)
439 views6 pages

Python期末考试编程题

Uploaded by

安子
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
439 views6 pages

Python期末考试编程题

Uploaded by

安子
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. 编写函数,使其被执行后输出”Hello world!


def MyHello():

例如执行:MyHello() 后输出”Hello world!”

代码
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 11:56:53 2022

@author: 86159
"""

def MyHello():
print("Hello world!")

MyHello()

运行过程和结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit
(AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
Hello world!

2. 编写函数,接收一个正整数 n,按如下方法进行求值:比如 n=234,通过执行函数计算


234 + 4 + 3 + 2 的值并将结果作为函数的返回值。
函数头为:
def CulNum(n): #n 为整数

返回值为:
return res

例如执行:CulNum(234)后返回结果为 243

代码
def CulNum(n):
res = 0
# 循环遍历 n 的每一位数字
for digit in str(n):
res += int(digit)
return res 运行过程和结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

for digit in str(n):


res += int(digit)
return res 运行过程和结果

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
9
6

3. 编写函数,接收一个运算法则和两个整数,该法则为加、减、乘、除(注意验证除数的
非零)、求余(注意验证除数的非零)中的一种,返回这两个数在该运算法则下的结果。
函数头为:
def MyCul(op, m, n): #op 为字符串格式,m,n 为整数

返回值为:
return res # 即 m op n 的结果。

例如执行:MyCul(‘+’, 3, 4)后返回结果为 7;MyCul(‘-’, 3, 4)后返回结果为-1;


MyCul(‘*’, 3, 4)后返回结果为 12;MyCul(‘/’, 3, 4)后返回结果为 0.75
代码
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 11:56:53 2022

@author: 86159
"""
def MyCul(op, m, n):

if op == "+":
return m + n

elif op == "-":
return m - n
elif op == "*":
return m * n
elif op == "/":
if n == 0:
return "Error: Division by zero"
return m / n
elif op == "%":
if n == 0:
return "Error: Division by zero"
return m % n
else:
return "Error: Invalid operator"
print(MyCul('+', 3, 4))
print(MyCul('-', 3, 4))
print(MyCul('*', 3, 4))
print(MyCul('/', 3, 4))
print(MyCul('%', 3, 4))

运行过程和结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
7
-1
12
0.75
3

4. 编写函数,接受一个字符串,统计该字符串中出现的字符种类以及次数。要求返回结果
为字典。
函数头为:
def StatisNum(s): #s 为被统计的字符串
返回值为:
return res #res 为字典类型

例如执行: StatisNum('aabbbcccdddd11222,,,****')
返回结果为{'a':2, 'b':3, 'c':3, 'd':4, '1':2, '2':3, ',':3, '*':4}
代码
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 11:56:53 2022

@author: 86159
"""

def StatisNum(s):
res = {}
for c in s:
if c not in res:
res[c] = 1
else:
res[c] += 1
return res
print(StatisNum('aabbbcccdddd11222,,,****'))

运行过程和结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
{'a': 2, 'b': 3, 'c': 3, 'd': 4, '1': 2, '2': 3, ',': 3, '*': 4}

5. 编写函数,在(1, 1000)范围内生成一个包含 30 个随机整数的列表,然后对其中奇数下标


的元素进行升序排序,偶数下标的元素不变。(提示:使用 random 中的 randint 以及切
片)
函数头为:
def MyList( ):

返回值为:
return res #res 为排序后的列表

例如执行: MyList()后,返回的结果为[312, 58, 883, 101, 697, 271, 361, 292, 542, 305,
347, 338, 907, 424, 360, 433, 73, 506, 104, 613, 234, 734, 544, 808, 425, 851, 168, 858, 907, 996]

代码
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 11:56:53 2022

@author: 86159
"""

import random

def MyList():
res = [random.randint(1, 1000) for _ in range(30)]
odd_elements = res[1::2]
odd_elements = sorted(odd_elements)
res[1::2] = odd_elements
return res
print(MyList())
运行过程和结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
[481, 156, 661, 374, 87, 441, 607, 453, 763, 456, 362, 511, 494, 533, 320, 634, 46, 653, 850, 706,
861, 833, 801, 836, 379, 909, 50, 932, 457, 974]

6. 编写函数,接收一个整数 n。首先判断一个数是否为素数,如果是素数直接返回 True,


否则(即是合数)就将该合数进行因数分解。建议每个功能分别定义一个函数。(20 分)
函数头为:
def func(n): #总函数
def IsPrime(n): #判断是否为素数的子函数
def Factor(n): #因式分解子的函数

返回值为:
return res #如果 n 为素数 res 为 True;如果 n 为合数 res 为 n 的因式分解结果。

代码
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 11:56:53 2022
@author: 86159
"""

def func(n):
if IsPrime(n):
return True
else:
return Factor(n)

def IsPrime(n):
if n == 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True

def Factor(n):
factors = []
i=2
while n > 1:
while n % i == 0:
factors.append(i)
n = n // i
i += 1
return factors

print(func(2))
print(func(16))

运行结果
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.

runfile('C:/Users/86159/Desktop/untitled0.py', wdir='C:/Users/86159/Desktop')
True
[2, 2, 2, 2]

You might also like