Python 进阶(3) | 值得练习的30道Python练手题(附单元测试)
1. 设计
准备了30道值得练习的 Python 题目。在阅读了前面两篇 blog 的基础上, 我们对 VSCode 执行 Python 的单元测试已经有所了解, 这一篇是进一步练习。 30道题目都是入门水平, 但是我们套用到单元测试中进行练习。
我提供了 test_main.py
文件, 你需要的是自行在 answer.py
文件中,实现每个 exN()
函数, N 从 1 到 30.
2. 题目
test_main.py 内容如下:
import answer as ans
# 已知一个字符串为 “hello_world_yoyo”,如何得到一个队列 [“hello”,”world”,”yoyo”] ?
def test_ex1():
s = "hello_world_yoyo"
res = ans.ex1(s)
assert res == ["hello", "world", "yoyo"]
# 有个列表 [“hello”, “world”, “yoyo”],如何把列表里面的字符串联起来,得到字符串 “hello_world_yoyo”?
def test_ex2():
s = ["hello", "world", "yoyo"]
res = ans.ex2(s)
assert res == "hello_world_yoyo"
# 把字符串 s 中的每个空格替换成”%20”,输入:s = “We are happy.”,输出:“We%20are%20happy.”。
def test_ex3():
s = "We are happy."
res = ans.ex3(s)
assert res == "We%20are%20happy."
# Python 如何打印 99 乘法表?
def test_ex4():
res = ans.ex4()
expected = """
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
""".strip()
assert res == expected
# 从下标 0 开始索引,找出单词 “welcome” 在字符串“Hello, welcome to my world.” 中
# 出现的位置,找不到返回 -1。
def test_ex5():
s = "Hello, welcome to my world."
target = "welcome"
res = ans.ex5(s, target)
assert res == 7
# 统计字符串“Hello, welcome to my world.” 中字母 w 出现的次数。
def test_ex6():
s = "Hello, welcome to my world."
res = ans.ex6(s)
assert res == 2
# 输入一个字符串 str,输出第 m 个只出现过 n 次的字符
# 如在字符串 gbgkkdehh 中,找出第 2 个只出现 1 次的字符,输出结果:d
def test_ex7():
s = "gbgkkdehh"
res = ans.ex7(s, 2, 1)
assert res == 'd'
res = ans.ex7(s, 1, 2)
assert res == 'g'
res = ans.ex7(s, 1, 1)
assert res == 'b'
# 判断字符串 a = “welcome to my world” 是否包含单词 b = “world”,
# 包含返回 True,不包含返回 False。
def test_ex8():
s = "welcome to my world"
b = "world"
res = ans.ex8(s, b)
assert res == True
# 从 0 开始计数,输出指定字符串 A = “hello” 在字符串 B = “hi how are you hello world, hello yoyo!”中
# 第一次出现的位置,如果 B 中不包含 A,则输出 -1。
def test_ex9():
s = "hi how are you hello world, hello yoyo!"
A = "hello"
res = ans.ex9(s, A)
assert res == 15
# 从 0 开始计数,输出指定字符串 A = “hello”在字符串 B = “hi how are you hello world, hello yoyo!”中
# 最后出现的位置,如果 B 中不包含 A,则输出 -1。
def test_ex10():
s = "hi how are you hello world, hello yoyo!"
A = "hello"
res = ans.ex10(s, A)
assert res == -11 + len(s)
# 给定一个数 a,判断一个数字是否为奇数或偶数。
def test_ex11():
s = 42
res = ans.ex11(s)
assert res == 'even'
s = 233
res = ans.ex11(s)
assert res == 'odd'
# 输入一个姓名,判断是否姓王。
def test_ex12():
s = "Lihong Wang"
res = ans.ex12(s)
assert res == True
s = "Junjie Lin"
res = ans.ex12(s)
assert res == False
# 如何判断一个字符串是不是纯数字组成?
def test_ex13():
s = "3.14"
res = ans.ex13(s)
assert res == True
s = "3.14s"
res = ans.ex13(s)
assert res == False
s = "000"
res = ans.ex13(s)
assert res == True
s = "133w3"
res = ans.ex13(s)
assert res == False
s = "-42"
res = ans.ex13(s)
assert res == False
# 将字符串 a = “This is string example….wow!” 全部转成大写,
# 字符串 b = “Welcome To My World” 全部转成小写。
def test_ex14():
a = "This is string example...wow!"
b = "Welcome To My World"
res_a, res_b = ans.ex14(a, b)
assert res_a == "THIS IS STRING EXAMPLE...WOW!"
assert res_b == "welcome to my world"
# 将字符串 a = “ welcome to my world ”首尾空格去掉
def test_ex15():
a = " welcome to my world "
res = ans.ex15(a)
assert res == "welcome to my world"
# 将字符串 s = “ajldjlajfdljfddd”,去重并从小到大排序输出”adfjl”。
def test_ex16():
s = "ajldjlajfdljfddd"
res = ans.ex16(s)
assert res == "adfjl"
# 打印出如下图案(菱形)
def test_ex17():
res = ans.ex17(7)
expected = """
*
***
*****
*******
*****
***
*
""".strip('\n')
assert expected == res
res = ans.ex17(9)
expected = """
*
***
*****
*******
*********
*******
*****
***
*
""".strip('\n')
assert expected == res
# 给一个不多于 5 位的正整数(如 a = 12346),求它是几位数和逆序打印出各位数字。
def test_ex18():
a = 12345
cnt, rev = ans.ex18(a)
assert cnt == 5
assert rev == 54321
b = 120
cnt, rev = ans.ex18(b)
assert cnt == 3
assert rev == 21
# 如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
# 例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数。那么如何求 1000 以内的水仙花数(3 位数)。
def test_ex19():
res = ans.ex19()
print(res)
assert res == [153, 370, 371, 407]
# 求 1+2+3…+n 相加的和。
def test_ex20():
res = ans.ex20(100)
assert res == 5050
res = ans.ex20(1)
assert res == 1
# 计算 1-2+3-4+5-…-n 的值。
def test_ex21():
res = ans.ex21(100)
assert res == -50
# 已知 a 的值为“hello”,b 的值为“world”,
# 如何交换 a 和 b 的值,得到 a 的值为“world”,b 的值为”hello”?
def test_ex23():
a = "hello"
b = "world"
res_a, res_b = ans.ex23(a, b)
assert res_a == "world"
assert res_b == "hello"
# 如何判断一个数组是对称数组?
def test_ex24():
x = [1, 'a', 0, '2', 0, 'a', 1]
res = ans.ex24(x)
assert res == True
x = [1, 2, 0, 2, 1]
res = ans.ex24(x)
assert res == True
x = [1, 2, 3, 3, 2, 1]
res = ans.ex24(x)
assert res == True
x = [1, 2, 3, 4]
res = ans.ex24(x)
assert res == False
# 如果有一个列表 a = [1,3,5,7,11],
# 那么如何让它反转成 [11,7,5,3,1],并且取到奇数位值的数字 [1,5,11]?
def test_ex25():
a = [1, 3, 5, 7, 11]
rev, odds = ans.ex25(a)
assert rev == [11, 7, 5, 3, 1]
assert odds == [1, 5, 11]
# 对列表 a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8] 中的数字从小到大排序。
def test_ex26():
a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8]
res = ans.ex26(a)
assert res == [1, 1, 6, 6, 7, 8, 8, 8, 8, 9, 11]
# 找出列表 L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88] 中最大值和最小值。
def test_ex27():
L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
min_val, max_val = ans.ex27(L1)
assert min_val == 1
assert max_val == 88
# 找出列表 a = [“hello”, “world”, “yoyo”, “congratulations”] 中单词最长的一个。
def test_ex28():
a = ["hello", "world", "yoyo", "congratulations"]
res = ans.ex28(a)
assert res == "congratulations"
# 取出列表 L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88] 中最大的三个值。
def test_ex29():
L1 = [1, 2, 3, 11, 2, 5, 3, 2, 5, 33, 88]
res = ans.ex29(L1)
assert res == [11, 33, 88]
# 把列表 a = [1, -6, 2, -5, 9, 4, 20, -3] 中的数字求绝对值。
def test_ex30():
a = [1, -6, 2, -5, 9, 4, 20, -3]
res = ans.ex30(a)
assert res == [1, 6, 2, 5, 9, 4, 20, 3]
3. 答案
def ex1(s : str):
return s.split('_')
def ex2(s : list):
return '_'.join(s)
def ex3(s : str):
return s.replace(' ', '%20')
def ex4():
s = ''
for i in range(1, 10):
if i > 1:
s += '\n'
for j in range(1, i+1):
if j > 1:
s += ' '
s += f'{j}x{i}={i*j}'
return s
def ex5(s : list, target : str):
return s.find(target)
def ex6(s : str):
cnt = 0
for c in s:
if c == 'w':
cnt += 1
return cnt
def ex7(s : str, m: int, n: int):
record = []
total_len = len(s)
for c in s:
count = s.count(c, 0, total_len)
if count == n:
record.append(c)
return record[m-1]
def ex8(s: str, b : str):
return b in s
def ex9(s : str, A : str):
return s.find(A)
def ex10(s : str, A : str):
return s.rfind(A)
def ex11(s : int):
if s % 2 == 0:
return 'even'
return 'odd'
def ex12(s : int):
items = s.split(' ')
firstname = items[-1]
return 'Wang' == firstname
# 利用 Python 提供的类型转行,将用户输入的数据转换成浮点数类型,
# 如果转换抛异常,则判断数字不是纯数字组成。
def ex13(s : str):
try:
v = float(s)
return v >= 0
except ValueError:
return False
def ex14(a : str, b : str) -> (str, str):
return a.upper(), b.lower()
def ex15(a: str) -> str:
return a.strip()
def ex16(a: str) -> str:
b = set(a)
return ''.join(sorted(b))
def ex17(n : int) -> str:
s = []
half = n // 2
for i in range(half):
t = ' ' * (half - i) + (2 * i + 1) * '*'
s.append(t)
s.append(n * '*')
for j in range(half):
i = half - 1 - j
t = ' ' * (half - i) + (2 * i + 1) * '*'
s.append(t)
return '\n'.join(s)
def ex18(a: int) -> (int, int):
t = a
cnt = 0
rev = 0
while t > 0:
digit = t % 10
t = t // 10
rev = rev * 10 + digit
cnt += 1
return cnt, rev
def ex19() -> list:
res = []
for i in range(100, 1000):
a = i // 100
c = i % 10
b = (i // 10) % 10
if a**3 + b**3 + c**3 == i:
res.append(i)
return res
def ex20(n : int) -> int:
sum = (1+n) * n / 2
return sum
def ex21(n : int) -> int:
sign = 1
res = 0
for i in range(1, n+1):
j = sign * i
res = res + j
sign *= -1
return res
def ex23(a : str, b : str) -> (str, str):
return b, a
def ex24(a : list):
# a[::] 表示 a从起始到结束的切片
# a[::-1] 表示从结束到起始,也就是翻转的链表
return a == a[::-1]
def ex25(a : list):
rev = a[::-1]
odds = a[0::2]
return rev, odds
def ex26(a : list):
return sorted(a)
def ex27(a : list):
return min(a), max(a)
def ex28(a : list):
max_len = 0
res = None
for item in a:
if len(item) > max_len:
max_len = len(item)
res = item
return res
def ex29(a : list):
b = sorted(a)
return b[-3:]
def ex30(a : list):
b = [abs(_) for _ in a]
return b