引言
编程是一门实践性极强的技术,理论知识固然重要,但实战经验的积累同样不可或缺。通过解决大量的实战练习题,可以帮助程序员加深对编程基础的理解,提高问题解决能力。本文将为您提供500道实战练习题的挑战指南,帮助您从基础到进阶,全面提升编程技能。
第一部分:基础算法题(100题)
1. 打印数字1到9
for i in range(1, 10):
print(i)
2. 求和1到100
total = 0
for i in range(1, 101):
total += i
print(total)
3. 判断质数
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# 示例
print(is_prime(7))
4. 冒泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
# 示例
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print(arr)
第二部分:数据结构题(100题)
5. 链表反转
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def reverse_list(head):
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev
# 示例
# 创建链表:1 -> 2 -> 3 -> 4 -> 5
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
head.next.next.next.next = ListNode(5)
# 反转链表
reversed_head = reverse_list(head)
# 打印反转后的链表
while reversed_head:
print(reversed_head.val)
reversed_head = reversed_head.next
6. 二叉树遍历
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def inorder_traversal(root):
if root:
inorder_traversal(root.left)
print(root.val)
inorder_traversal(root.right)
# 示例
# 创建二叉树:1 -> 2 -> 4 / \ 3 -> 5
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(4)
root.right.right = TreeNode(5)
# 中序遍历
inorder_traversal(root)
第三部分:设计题(100题)
7. 单例模式
class Singleton:
_instance = None
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
# 示例
singleton1 = Singleton.get_instance()
singleton2 = Singleton.get_instance()
print(singleton1 is singleton2) # 输出:True
8. 货币兑换
def coin_change(coins, amount):
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
# 示例
coins = [1, 2, 5]
amount = 11
print(coin_change(coins, amount)) # 输出:3
第四部分:系统设计题(100题)
9. 缓存系统
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = {}
self.order = []
def get(self, key):
if key not in self.cache:
return -1
else:
self.order.remove(key)
self.order.append(key)
return self.cache[key]
def put(self, key, value):
if key in self.cache:
self.order.remove(key)
else:
if len(self.order) == self.capacity:
oldest_key = self.order.pop(0)
del self.cache[oldest_key]
self.cache[key] = value
self.order.append(key)
# 示例
lru_cache = LRUCache(2)
lru_cache.put(1, 1)
lru_cache.put(2, 2)
print(lru_cache.get(1)) # 输出:1
lru_cache.put(3, 3) # 删除键1
print(lru_cache.get(2)) # 输出:2
print(lru_cache.get(3)) # 输出:3
10. 文件上传系统
import threading
class FileUploadSystem:
def __init__(self):
self.lock = threading.Lock()
self.file_queue = []
def upload_file(self, file_path):
with self.lock:
self.file_queue.append(file_path)
print(f"上传文件:{file_path}")
def process_files(self):
while True:
if self.file_queue:
file_path = self.file_queue.pop(0)
print(f"处理文件:{file_path}")
# 示例
file_upload_system = FileUploadSystem()
file_upload_system.upload_file("file1.txt")
file_upload_system.upload_file("file2.txt")
结语
通过以上500道实战练习题的挑战,相信您能够在编程基础方面取得显著的进步。不断练习,不断反思,才能在编程的道路上越走越远。祝您学习愉快!
