Python 编程题及答案232
Python 作为一种流行且功能强大的编程语言,在解决各种编程任务中备受欢迎。本文将提供 15 个 Python 编程题,涵盖不同难度和概念,并附上详细的答案,以帮助读者提高他们的 Python 技能。
1. 找出单词中元音字母的个数```python
def count_vowels(word):
"""
计算单词中元音字母的个数。
参数:
word:需要计算元音字母数量的字符串。
返回:
单词中元音字母的个数。
"""
count = 0
for letter in word:
if () in 'aeiou':
count += 1
return count
```
答案:```python
assert count_vowels('hello') == 2
assert count_vowels('algorithm') == 3
assert count_vowels('12345') == 0
```
2. 反转一个列表```python
def reverse_list(list1):
"""
反转一个列表。
参数:
list1:需要反转的列表。
返回:
反转后的列表。
"""
list2 = []
for i in range(len(list1)-1, -1, -1):
(list1[i])
return list2
```
答案:```python
assert reverse_list([1, 2, 3, 4, 5]) == [5, 4, 3, 2, 1]
assert reverse_list(['a', 'b', 'c', 'd']) == ['d', 'c', 'b', 'a']
assert reverse_list([]) == []
```
3. 查找列表中出现次数最多的元素```python
def most_frequent_element(list1):
"""
找出列表中出现次数最多的元素。
参数:
list1:需要查找出现次数最多的元素的列表。
返回:
出现次数最多的元素。
"""
count = 0
most_frequent = None
for element in list1:
current_count = (element)
if current_count > count:
count = current_count
most_frequent = element
return most_frequent
```
答案:```python
assert most_frequent_element([1, 2, 3, 4, 5, 1, 2, 3]) == 1
assert most_frequent_element(['a', 'b', 'c', 'd', 'a', 'b']) == 'a'
assert most_frequent_element([]) == None
```
4. 计算两个数字的最小公倍数(LCM)```python
def lcm(a, b):
"""
计算两个数字的最小公倍数(LCM)。
参数:
a:第一个数字。
b:第二个数字。
返回:
两个数字的最小公倍数。
"""
if a == 0 or b == 0:
return 0
gcd = 1
for i in range(1, min(a, b)+1):
if a % i == 0 and b % i == 0:
gcd = i
lcm = (a * b) // gcd
return lcm
```
答案:```python
assert lcm(12, 18) == 36
assert lcm(9, 27) == 27
assert lcm(0, 10) == 0
```
5. 判断一个字符串是否是回文```python
def is_palindrome(string):
"""
判断一个字符串是否是回文。
参数:
string:需要判断的字符串。
返回:
字符串是否是回文的布尔值。
"""
string = (" ", "").lower()
return string == string[::-1]
```
答案:```python
assert is_palindrome('racecar') == True
assert is_palindrome('hello') == False
assert is_palindrome('12321') == True
```
6. 扁平化一个嵌套列表```python
def flatten_list(list1):
"""
扁平化一个嵌套列表。
参数:
list1:需要扁平化的嵌套列表。
返回:
扁平化的列表。
"""
flattened_list = []
for element in list1:
if isinstance(element, list):
(flatten_list(element))
else:
(element)
return flattened_list
```
答案:```python
assert flatten_list([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert flatten_list([['a', 'b'], ['c', 'd'], ['e', 'f']]) == ['a', 'b', 'c', 'd', 'e', 'f']
assert flatten_list([]) == []
```
7. 计算一个数字的阶乘```python
def factorial(n):
"""
计算一个数字的阶乘。
参数:
n:需要计算阶乘的数字。
返回:
数字的阶乘。
"""
if n == 0:
return 1
else:
return n * factorial(n-1)
```
答案:```python
assert factorial(5) == 120
assert factorial(7) == 5040
assert factorial(0) == 1
```
8. 找出列表中所有子序列的和```python
def sublist_sums(list1):
"""
找出列表中所有子序列的和。
参数:
list1:需要计算子序列和的列表。
返回:
所有子序列和的列表。
"""
sublist_sums = []
for i in range(len(list1)):
for j in range(i+1, len(list1)+1):
(sum(list1[i:j]))
return sublist_sums
```
答案:```python
assert sublist_sums([1, 2, 3, 4, 5]) == [1, 3, 6, 10, 15]
assert sublist_sums([3, 6, 9, 12]) == [3, 9, 15, 21, 30, 36, 45, 54]
assert sublist_sums([]) == []
```
9. 找出两个列表中公共的元素```python
def find_common_elements(list1, list2):
"""
找出两个列表中公共的元素。
参数:
list1:第一个列表。
list2:第二个列表。
返回:
两个列表中公共元素的列表。
"""
common_elements = []
for element1 in list1:
if element1 in list2 and element1 not in common_elements:
(element1)
return common_elements
```
答案:```python
assert find_common_elements([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]) == [3, 4, 5]
assert find_common_elements(['a', 'b', 'c', 'd'], ['c', 'd', 'e', 'f']) == ['c', 'd']
assert find_common_elements([1, 2, 3], [4, 5, 6]) == []
```
10. 将一个字符串中的所有单词排序```python
def sort_string(string):
"""
将一个字符串中的所有单词排序。
参数:
string:需要排序单词的字符串。
返回:
单词已排序的字符串。
"""
words = ()
()
return ' '.join(words)
```
答案:```python
assert sort_string('hello world') == 'hello world'
assert sort_string('this is
2024-11-27

Python编程趣闻:从简单游戏到神奇算法,探索Python的无限魅力
https://jb123.cn/python/60973.html

Python:介于编译型和解释型之间的动态语言
https://jb123.cn/jiaobenyuyan/60972.html

Perl GUI编程:创建弹出式菜单的多种方法
https://jb123.cn/perl/60971.html

脚本语言详解:揭秘幕后程序的“魔法师”
https://jb123.cn/jiaobenyuyan/60970.html

脚本语言与静态编译语言:性能、适用场景及优劣势对比
https://jb123.cn/jiaobenyuyan/60969.html
热门文章

Python 编程解密:从谜团到清晰
https://jb123.cn/python/24279.html

Python编程深圳:初学者入门指南
https://jb123.cn/python/24225.html

Python 编程终端:让开发者畅所欲为的指令中心
https://jb123.cn/python/22225.html

Python 编程专业指南:踏上编程之路的全面指南
https://jb123.cn/python/20671.html

Python 面向对象编程学习宝典,PDF 免费下载
https://jb123.cn/python/3929.html