當前位置: 妍妍網 > 碼農

Python 10個面試題(2)

2024-02-29碼農

1. 題目:編寫一個函式,接受一個字串,返回字串中出現次數最多的字元及其出現次數。

def most_common_char(s): char_count= {}forcharin s:ifcharin char_count: char_count[char] += 1else: 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 nelse:return fibonacci(n-1) + fibonacci(n-2)n = 10result = 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] += 1else: word_count[word] = 1return word_countsentence = "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:returnFalsefor i in range(2, int(num**0.5) + 1):if num % i == 0:returnFalsereturnTruedefprime_numbers(n): primes = []for i in range(2, n):if is_prime(i): primes.append(i)return primesn = 20result = prime_numbers(n)print(f"Prime numbers less than {n}: {result}")

5. 題目:實作一個簡單的猜詞遊戲,程式隨機選取一個單詞,玩家透過猜測字母來猜出完整單詞。

import randomwords = ['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] = guessprint(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 trianglen = 5result = 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 / num2else: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.")