The following is a code example for questions 61 to 70:
- Write a program that determines whether a string is a palindrome (same for both forward and reverse spellings).
def is_palindrome(string):
return string == string[::-1]
string = input("请输入一个字符串:")
if is_palindrome(string):
print("是回文串")
else:
print("不是回文串")
- Write a program to find the most frequent element in a list.
from collections import Counter
num_list = [1, 2, 3, 4, 2, 2, 3, 4, 4, 4]
counter = Counter(num_list)
most_common = counter.most_common(1)
print("出现频率最高的元素是:", most_common[0][0])
- Write a program to find the common elements in two lists.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_elements = list(set(list1) & set(list2))
print("两个列表中的公共元素为:", common_elements)
- Write a program that calculates the average of all odd numbers in a list.
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = [num for num in num_list if num % 2 != 0]
average = sum(odd_numbers) / len(odd_numbers)
print("所有奇数的平均值为:", average)
- Write a program to determine whether a year is a leap year.
def is_leap_year(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
year = int(input("请输入一个年份:"))
if is_leap_year(year):
print(year, "是闰年")
else:
print(year, "不是闰年")
- Write a program to remove all elements from a list of integers.
num_list = [1, 2, 3, 4, 2, 2, 3, 4, 4, 4]
unique_numbers = list(set(num_list))
print("去重后的列表为:", unique_numbers)
- Write a program to determine whether a string is a valid bracket sequence (that is, the opening and closing of the brackets match correctly).
def is_valid_parentheses(s):
stack = []
mapping = {
')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping.values():
stack.append(char)
elif char in mapping.keys():
if not stack or mapping[char] != stack.pop():
return False
return not stack
string = input("请输入一个括号序列:")
if is_valid_parentheses(string):
print("是有效的括号序列")
else:
print("不是有效的括号序列")
- Write a program to find the kth largest element in a list.
def find_kth_largest(lst, k):
sorted_list = sorted(lst, reverse=True)
return sorted_list[k-1]
num_list = [1, 5, 2, 8, 4, 9, 3]
k = 3
kth_largest = find_kth_largest(num_list, k)
print("列表中第", k, "大的元素为:", kth_largest)
- Write a program to determine whether a number is prime (divisible only by 1 and itself).
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
number = int(input("请输入一个整数:"))
if is_prime(number):
print(number, "是质数")
else:
print(number, "不是质数")
- Write a program that counts the length of each word in a string and outputs the longest and shortest word length.
string = input("请输入一个字符串:")
words = string.split()
word_lengths = [len(word) for word in words]
max_length = max(word_lengths)
min_length = min(word_lengths)
print("最长单词长度:", max_length)
print("最短单词长度:", min_length)
Here is the code sample for questions 61 to 70. If you have any other questions, please keep asking!