Python 二级编程题精选300


作为一名 Python 初学者,解决编程题是提升技能和巩固知识的有效途径。本篇文章精选了 8 道 Python 二级编程题,包含各种数据结构和算法,旨在帮助你深入理解 Python 的特性和语法。

1. 求取列表中最大值和最小值```python
def find_max_min(list1):
"""
求取列表中最大值和最小值
Args:
list1 (list): 输入列表
Returns:
tuple: 最大值和最小值
"""
max_value = max(list1)
min_value = min(list1)
return max_value, min_value

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
max_value, min_value = find_max_min(list1)
print("最大值:", max_value)
print("最小值:", min_value)
```

2. 查找列表中指定元素```python
def find_element(list1, element):
"""
查找列表中指定元素
Args:
list1 (list): 输入列表
element: 要查找的元素
Returns:
bool: 元素是否存在
"""
return element in list1

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
element = 5
result = find_element(list1, element)
print("元素是否存在:", result)
```

3. 计算两个列表的交集```python
def find_intersection(list1, list2):
"""
计算两个列表的交集
Args:
list1 (list): 第一个列表
list2 (list): 第二个列表
Returns:
list: 交集列表
"""
intersection = list(set(list1) & set(list2))
return intersection

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
list2 = [5, 7, 9, 11, 13, 15]
intersection = find_intersection(list1, list2)
print("交集:", intersection)
```

4. 判断回文串```python
def is_palindrome(string):
"""
判断回文串
Args:
string (str): 输入字符串
Returns:
bool: 字符串是否为回文串
"""
return string == string[::-1]

# 测试代码
string = "radar"
result = is_palindrome(string)
print("字符串是否是回文串:", result)
```

5. 堆排序```python
def heap_sort(list1):
"""
堆排序
Args:
list1 (list): 输入列表
Returns:
list: 排序后列表
"""
# 构建最大堆
for i in range(len(list1) // 2 - 1, -1, -1):
heapify(list1, i, len(list1))
# 排序
for i in range(len(list1) - 1, 0, -1):
list1[0], list1[i] = list1[i], list1[0]
heapify(list1, 0, i)
return list1

def heapify(list1, i, n):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and list1[left] > list1[largest]:
largest = left
if right < n and list1[right] > list1[largest]:
largest = right
if largest != i:
list1[i], list1[largest] = list1[largest], list1[i]
heapify(list1, largest, n)

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
sorted_list = heap_sort(list1)
print("排序后列表:", sorted_list)
```

6. 冒泡排序```python
def bubble_sort(list1):
"""
冒泡排序
Args:
list1 (list): 输入列表
Returns:
list: 排序后列表
"""
n = len(list1)
for i in range(n):
for j in range(0, n - i - 1):
if list1[j] > list1[j + 1]:
list1[j], list1[j + 1] = list1[j + 1], list1[j]
return list1

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
sorted_list = bubble_sort(list1)
print("排序后列表:", sorted_list)
```

7. 快排```python
def quick_sort(list1):
"""
快排
Args:
list1 (list): 输入列表
Returns:
list: 排序后列表
"""
if len(list1) pivot]
return quick_sort(left) + middle + quick_sort(right)

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9]
sorted_list = quick_sort(list1)
print("排序后列表:", sorted_list)
```

8. 查重```python
def remove_duplicates(list1):
"""
查重
Args:
list1 (list): 输入列表
Returns:
list: 去重后列表
"""
return list(set(list1))

# 测试代码
list1 = [10, 20, 4, 5, 6, 7, 8, 9, 10]
unique_list = remove_duplicates(list1)
print("去重后列表:", unique_list)
```

结语以上 8 道编程题涵盖了 Python 二级中的基础知识和常见算法。通过解决这些问题,可以帮助你巩固所学知识,提高编程能力。

2024-12-11


上一篇:Python 快速编程入门教程:初学者指南

下一篇:程序完数 6 - Python