请继续查看以下第91到100题的代码示例:
def calculate_sum(nums):
return sum(nums)
number_list = [1, 2, 3, 4, 5]
sum_of_numbers = calculate_sum(number_list)
print("列表中所有元素的和:", sum_of_numbers)
def replace_spaces(string, replacement):
return string.replace(" ", replacement)
text = input("请输入一个字符串:")
replacement_char = input("请输入替换空格的字符:")
new_text = replace_spaces(text, replacement_char)
print("替换空格后的字符串:", new_text)
def is_valid_number(s):
try:
float(s)
return True
except ValueError:
return False
string = input("请输入一个字符串:")
if is_valid_number(string):
print(string, "是有效的数字表示")
else:
print(string, "不是有效的数字表示")
from collections import Counter
def find_duplicates(lst):
counter = Counter(lst)
duplicates = [num for num, count in counter.items() if count > 1]
return duplicates
number_list = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7]
duplicate_numbers = find_duplicates(number_list)
print("列表中的重复元素:", duplicate_numbers)
from collections import Counter
def count_characters(string):
counter = Counter(string)
return counter
text = input("请输入一个字符串:")
character_counts = count_characters(text)
print("每个字符出现的次数:", character_counts)
def is_perfect_square(num):
if num < 0:
return False
square_root = int(num ** 0.5)
return square_root * square_root == num
number = int(input("请输入一个整数:"))
if is_perfect_square(number):
print(number, "是完全平方数")
else:
print(number, "不是完全平方数")
def reverse_list(lst):
return lst[::-1]
number_list = [1, 2, 3, 4, 5]
reversed_list = reverse_list(number_list)
print("倒序排列后的列表:", reversed_list)
def is_valid_palindrome(string):
string = string.lower() # 忽略大小写
clean_string = ''.join(filter(str.isalnum, string)) # 去除非字母数字字符
return clean_string == clean_string[::-1]
text = input("请输入一个字符串:")
if is_valid_palindrome(text):
print(text, "是有效的回文字符串")
else:
print(text, "不是有效的回文字符串")
def calculate_even_sum(nums):
even_nums = [num for num in nums if num % 2 == 0]
return sum(even_nums)
number_list = [1, 2, 3, 4, 5, 6]
even_sum = calculate_even_sum(number_list)
print("列表中所有偶数的和:", even_sum)
def reverse_string(string):
return string[::-1]
text = input("请输入一个字符串:")
reversed_text = reverse_string(text)
print("逆序输出的字符串:", reversed_text)
这是第91到100题的代码示例。如果您还有其他问题,请继续提问!