20个python代码大全,学python必刷的题目
20个python代码大全
关注公众号,获取更多学习资料,动态消息更新!
题目1:字符串处理
题目描述: 编写一个函数is_balanced_parentheses,该函数接收一个字符串作为参数,并判断该字符串中的括号是否平衡。括号包括圆括号 ()、方括号 [] 和花括号 {}。
示例:
input: "()[]{}"
output: True
input: "([)]"
output: False
代码框架:
def is_balanced_parentheses(s):
# Your code here
pass
result = is_balanced_parentheses("()[]{}")
print(result) # 应该输出 True
题目2:列表操作
题目描述: 编写一个函数remove_duplicates_preserve_order,该函数接收一个列表作为参数,并返回一个新的列表,其中去除了所有重复的元素,同时保持原有的顺序。
示例:
input: [1, 2, 2, 3, 4, 4, 5]
output: [1, 2, 3, 4, 5]
代码框架:
def remove_duplicates_preserve_order(lst):
# Your code here
pass
result = remove_duplicates_preserve_order([1, 2, 2, 3, 4, 4, 5])
print(result) # 应该输出 [1, 2, 3, 4, 5]
题目3:字典操作
题目描述: 编写一个函数get_max_value_key,该函数接收一个字典作为参数,并返回字典中值最大的键。如果有多个键具有相同的最大值,则返回最先出现的那个键。
示例:
input: {"a": 1, "b": 2, "c": 3}
output: "c"
代码框架:
def get_max_value_key(d):
# Your code here
pass
result = get_max_value_key({"a": 1, "b": 2, "c": 3})
print(result) # 应该输出 "c"
题目4:数据结构应用
题目描述: 编写一个函数find_unique_number,该函数接收一个整数列表作为参数,并返回列表中唯一的一个不重复的数。假设列表中只有一个数是不重复的。
示例:
input: [1, 1, 2, 2, 3]
output: 3
代码框架:
def find_unique_number(lst):
# Your code here
pass
result = find_unique_number([1, 1, 2, 2, 3])
print(result) # 应该输出 3
题目5:文件处理
题目描述: 编写一个函数get_file_extension,该函数接收一个文件路径作为参数,并返回文件的扩展名。如果文件没有扩展名,则返回空字符串。
示例:
input: "/path/to/file.txt"
output: "txt"
input: "/path/to/file"
output: ""
代码框架:
def get_file_extension(file_path):
# Your code here
pass
result = get_file_extension("/path/to/file.txt")
print(result) # 应该输出 "txt"
题目6:字符串处理
题目描述: 编写一个函数count_vowels,该函数接收一个字符串作为参数,并返回该字符串中元音字母的数量。元音字母包括 a, e, i, o, u(忽略大小写)。
示例:
input: "Hello World"
output: 3
代码框架:
def count_vowels(s):
# Your code here
pass
result = count_vowels("Hello World")
print(result) # 应该输出 3
题目7:列表操作
题目描述: 编写一个函数find_median,该函数接收一个整数列表作为参数,并返回列表中所有元素的中位数。如果列表长度为偶数,则返回中间两个数的平均值。
示例:
input: [1, 3, 5]
output: 3
input: [1, 2, 3, 4]
output: 2.5
代码框架:
def find_median(lst):
# Your code here
pass
result = find_median([1, 2, 3, 4])
print(result) # 应该输出 2.5
题目8:字典操作
题目描述: 编写一个函数get_frequency_dict,该函数接收一个字符串作为参数,并返回一个字典,字典的键是字符串中的字符,值是该字符出现的次数。
示例:
input: "hello"
output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
代码框架:
def get_frequency_dict(s):
# Your code here
pass
result = get_frequency_dict("hello")
print(result) # 应该输出 {'h': 1, 'e': 1, 'l': 2, 'o': 1}
题目9:数据结构应用
题目描述: 编写一个函数is_subsequence,该函数接收两个字符串作为参数,并判断第一个字符串是否是第二个字符串的子序列。子序列是指第一个字符串中的字符按照顺序出现在第二个字符串中,但不需要连续。
示例:
input: "abc", "ahbgdc"
output: True
input: "axc", "ahbgdc"
output: False
代码框架:
def is_subsequence(s, t):
# Your code here
pass
result = is_subsequence("abc", "ahbgdc")
print(result) # 应该输出 True
题目10:文件处理
题目描述: 编写一个函数rename_files,该函数接收一个文件夹路径和一个前缀作为参数,并将该文件夹中所有的文件重命名为前缀加上原始文件名。例如,文件file1.txt将被重命名为prefix_file1.txt。
示例: 假设文件夹images中有文件image1.jpg,调用rename_files("images", "pic_")后,image1.jpg将被重命名为pic_image1.jpg。
代码框架:
import os
def rename_files(folder_path, prefix):
# Your code here
pass
folder_path = "images"
prefix = "pic_"
rename_files(folder_path, prefix)
题目描述: 编写一个函数capitalize_words,该函数接收一个字符串作为参数,并返回一个新的字符串,其中每个单词的首字母都被大写。
示例:
input: "hello world"
output: "Hello World"
代码框架:
def capitalize_words(s):
# Your code here
pass
result = capitalize_words("hello world")
print(result) # 应该输出 "Hello World"
题目12:列表操作
题目描述: 编写一个函数transpose_matrix,该函数接收一个二维列表(矩阵)作为参数,并返回该矩阵的转置。即原矩阵的行变为新矩阵的列。
示例:
input: [[1, 2, 3], [4, 5, 6]]
output: [[1, 4], [2, 5], [3, 6]]
代码框架:
def transpose_matrix(matrix):
# Your code here
pass
result = transpose_matrix([[1, 2, 3], [4, 5, 6]])
print(result) # 应该输出 [[1, 4], [2, 5], [3, 6]]
题目13:字典操作
题目描述: 编写一个函数combine_dicts,该函数接收多个字典作为参数,并返回一个新的字典,其中包含了所有输入字典的键值对。如果存在重复的键,则最后一个字典中的值将被保留。
示例:
input: [{"a": 1, "b": 2}, {"b": 3, "c": 4}]
output: {"a": 1, "b": 3, "c": 4}
代码框架:
def combine_dicts(*dicts):
# Your code here
pass
result = combine_dicts({"a": 1, "b": 2}, {"b": 3, "c": 4})
print(result) # 应该输出 {"a": 1, "b": 3, "c": 4}
题目14:数据结构应用
题目描述: 编写一个函数find_first_non_repeating_char,该函数接收一个字符串作为参数,并返回字符串中第一个不重复的字符。如果所有字符都重复,则返回None。
示例:
input: "leetcode"
output: "l"
input: "loveleetcode"
output: "v"
代码框架:
def find_first_non_repeating_char(s):
# Your code here
pass
result = find_first_non_repeating_char("loveleetcode")
print(result) # 应该输出 "v"
题目15:文件处理
题目描述: 编写一个函数count_lines_in_files,该函数接收一个文件夹路径作为参数,并返回一个字典,其中的键是文件名,值是对应的文件中的行数。
示例: 假设文件夹data中有两个文件file1.txt和file2.txt,分别含有5行和3行,调用count_lines_in_files("data")应该返回{"file1.txt": 5, "file2.txt": 3}。
代码框架:
import os
def count_lines_in_files(folder_path):
# Your code here
pass
folder_path = "data"
line_counts = count_lines_in_files(folder_path)
print(line_counts) # 应该输出 {"file1.txt": 5, "file2.txt": 3}
题目16:字符串处理
题目描述: 编写一个函数reverse_sentence,该函数接收一个句子作为参数,并返回一个新的字符串,其中句子中的单词顺序被反转,但单词内部的字符顺序不变。
示例:
input: "I love Python programming"
output: "programming Python love I"
代码框架:
def reverse_sentence(sentence):
# Your code here
pass
result = reverse_sentence("I love Python programming")
print(result) # 应该输出 "programming Python love I"
题目17:列表操作
题目描述: 编写一个函数reverse_odd_indices,该函数接收一个列表作为参数,并返回一个新的列表,其中奇数索引上的元素被反转,偶数索引上的元素保持不变。
示例:
input: [1, 2, 3, 4, 5]
output: [1, 4, 3, 2, 5]
代码框架:
def reverse_odd_indices(lst):
# Your code here
pass
result = reverse_odd_indices([1, 2, 3, 4, 5])
print(result) # 应该输出 [1, 4, 3, 2, 5]
题目18:字典操作
题目描述: 编写一个函数filter_dict_by_value,该函数接收一个字典和一个阈值作为参数,并返回一个新的字典,其中只包含那些值大于等于给定阈值的键值对。
示例:
input: {"a": 1, "b": 2, "c": 3}, threshold=2
output: {"b": 2, "c": 3}
代码框架:
def filter_dict_by_value(d, threshold):
# Your code here
pass
result = filter_dict_by_value({"a": 1, "b": 2, "c": 3}, threshold=2)
print(result) # 应该输出 {"b": 2, "c": 3}
题目19:数据结构应用
题目描述: 编写一个函数find_missing_numbers,该函数接收一个整数列表作为参数,并返回一个新的列表,包含从1到列表中最大数之间的所有缺失的整数。
示例:
input: [2, 3, 7, 4]
output: [1, 5, 6]
代码框架:
def find_missing_numbers(lst):
# Your code here
pass
result = find_missing_numbers([2, 3, 7, 4])
print(result) # 应该输出 [1, 5, 6]
题目20:文件处理
题目描述: 编写一个函数move_files,该函数接收一个文件夹路径和一个目标文件夹路径作为参数,并将源文件夹中的所有文件移动到目标文件夹中。
示例: 假设文件夹source中有文件file1.txt和file2.txt,调用move_files("source", "target")后,source文件夹将为空,而target文件夹将包含file1.txt和file2.txt。
代码框架:
python
浅色版本
import os
import shutil
def move_files(source_folder, target_folder):
# Your code here
pass
source_folder = "source"
target_folder = "target"
move_files(source_folder, target_folder)
关于Python技术储备
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
全套Python学习资料分享:
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,还有环境配置的教程,给大家节省了很多时间。

三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频全套
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。


五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。



最后
如果你也想自学Python,可以关注我。我会把踩过的坑分享给你,让你不要踩坑,提高学习速度,还整理出了一套系统的学习路线,这套资料涵盖了诸多学习内容:开发工具,基础视频教程,项目实战源码,51本电子书籍,100道练习题等。相信可以帮助大家在最短的时间内,能达到事半功倍效果,用来复习也是非常不错的。

————————————————
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)