1. 题目:编写一个函数,接受一个字符串,返回字符串中出现次数最多的字符及其出现次数。
def most_common_char(s):
char_count= {}
forcharin s:
ifcharin char_count:
char_count[char] += 1
else:
char_count[char] = 1
max_char = max(char_count, key=char_count.get)
return max_char, char_count[max_char]
text = "hello, world!"
result_char, result_count = most_common_char(text)
print(f"The most common character is '{result_char}' with count: {result_count}")
2. 题目:实现一个简单的递归函数,计算斐波那契数列的第 n 项。
deffibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
n = 10
result = fibonacci(n)
print(f"The {n}th Fibonacci number is: {result}")
3. 题目:编写一个程序,接受用户输入的英文句子,统计并输出其中每个单词出现的次数。
defword_count(sentence):
words = sentence.split()
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
return word_count
sentence = "hello world hello python world"
result = word_count(sentence)
for word, count in result.items():
print(f"'{word}' appears {count} times")
4. 题目:编写一个函数,接受一个整数 n,输出所有小于 n 的质数。
defis_prime(num):
if num < 2:
returnFalse
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
returnFalse
returnTrue
defprime_numbers(n):
primes = []
for i in range(2, n):
if is_prime(i):
primes.append(i)
return primes
n = 20
result = prime_numbers(n)
print(f"Prime numbers less than {n}: {result}")
5. 题目:实现一个简单的猜词游戏,程序随机选取一个单词,玩家通过猜测字母来猜出完整单词。
import random
words = ['apple', 'banana', 'orange', 'grape', 'pear']
target_word = random.choice(words)
guessed_word = ['_'] * len(target_word)
while'_'in guessed_word:
print(' '.join(guessed_word))
guess = input("Guess a letter: ")
for i in range(len(target_word)):
if target_word[i] == guess:
guessed_word[i] = guess
print(f"Congratulations! The word is: {''.join(guessed_word)}")
6. 题目:编写一个函数,接受一个整数 n,返回一个 n 行的杨辉三角形的列表。
defgenerate_pascal_triangle(n):
triangle = []
for i in range(n):
row = [1] * (i+1)
for j in range(1, i):
row[j] = triangle[i-1][j-1] + triangle[i-1][j]
triangle.append(row)
return triangle
n = 5
result = generate_pascal_triangle(n)
for row in result:
print(row)
7. 题目:实现一个简单的计算器程序,能够接受用户输入的两个数以及操作符(加、减、乘、除),并输出计算结果。
def calculator(num1, num2, operator):
ifoperator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 != 0:
return num1 / num2
else:
return"Error: Division by zero"
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Enter the operator (+, -, *, /): ")
result = calculator(num1, num2, operator)
print(f"Result: {result}")
8. 题目:编写一个程序,接受用户输入的字符串,判断该字符串是否为回文串(正着读和倒着读都一样)。
defis_palindrome(s):
return s == s[::-1]
string = input("Enter a string: ")
if is_palindrome(string):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
9. 题目:实现一个简单的英汉词典程序,用户输入英文单词,程序输出对应的中文翻译。
dictionary = {
'apple': '苹果',
'banana': '香蕉',
'orange': '橙子',
'grape': '葡萄',
'pear': '梨'
}
word = input("Enter an English word: ")
translation = dictionary.get(word)
if translation:
print(f"The Chinese translation of '{word}' is: {translation}")
else:
print("Word not found in the dictionary.")