數據結構和算法html
算法:解決問題的方法和步驟python
評價算法的好壞:漸近時間複雜度和漸近空間複雜度。git
漸近時間複雜度的大O標記:程序員
排序算法(選擇、冒泡和歸併)和查找算法(順序和折半)github
def select_sort(origin_items, comp=lambda x, y: x < y): """簡單選擇排序""" items = origin_items[:] for i in range(len(items) - 1): min_index = i for j in range(i + 1, len(items)): if comp(items[j], items[min_index]): min_index = j items[i], items[min_index] = items[min_index], items[i] return items
def bubble_sort(origin_items, comp=lambda x, y: x > y): """高質量冒泡排序(攪拌排序)""" items = origin_items[:] for i in range(len(items) - 1): swapped = False for j in range(i, len(items) - 1 - i): if comp(items[j], items[j + 1]): items[j], items[j + 1] = items[j + 1], items[j] swapped = True if swapped: swapped = False for j in range(len(items) - 2 - i, i, -1): if comp(items[j - 1], items[j]): items[j], items[j - 1] = items[j - 1], items[j] swapped = True if not swapped: break return items
def merge_sort(items, comp=lambda x, y: x <= y): """歸併排序(分治法)""" if len(items) < 2: return items[:] mid = len(items) // 2 left = merge_sort(items[:mid], comp) right = merge_sort(items[mid:], comp) return merge(left, right, comp) def merge(items1, items2, comp): """合併(將兩個有序的列表合併成一個有序的列表)""" items = [] index, index2 = 0, 0 while index1 < len(items1) and index2 < len(items2): if comp(items1[index1], items2[index2]): items.append(items1[index1]) index1 += 1 else: items.append(items2[index2]) index2 += 1 items += items1[index1:] items += items2[index2:] return items
def seq_search(items, key): """順序查找""" for index, item in enumerate(items): if item == key: return index return -1
def bin_search(items, key): """折半查找""" start, end = 0, len(items) - 1 while start <= end: mid = (start + end) // 2 if key > items[mid]: start = mid + 1 elif key < items[mid]: end = mid - 1 else: return mid return -1
使用生成式(推導式)語法面試
prices = {
'AAPL': 191.88, 'GOOG': 1186.96, 'IBM': 149.24, 'ORCL': 48.44, 'ACN': 166.89, 'FB': 208.09, 'SYMC': 21.29 } # 用股票價格大於100元的股票構造一個新的字典 prices2 = {key: value for key, value in prices.items() if value > 100} print(prices2)
說明:生成式(推導式)能夠用來生成列表、集合和字典。正則表達式
嵌套的列表算法
names = ['關羽', '張飛', '趙雲', '馬超', '黃忠'] courses = ['語文', '數學', '英語'] # 錄入五個學生三門課程的成績 # 錯誤 - 參考http://pythontutor.com/visualize.html#mode=edit # scores = [[None] * len(courses)] * len(names) scores = [[None] * len(courses) for _ in range(len(names))] for row, name in enumerate(names): for col, course in enumerate(courses): scores[row][col] = float(input(f'請輸入{name}的{course}成績: ')) print(scores)
Python Tutor - VISUALIZE CODE AND GET LIVE HELP編程
heapq、itertools等的用法後端
"""
從列表中找出最大的或最小的N個元素 堆結構(大根堆/小根堆) """ import heapq list1 = [34, 25, 12, 99, 87, 63, 58, 78, 88, 92] list2 = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] print(heapq.nlargest(3, list1)) print(heapq.nsmallest(3, list1)) print(heapq.nlargest(2, list2, key=lambda x: x['price'])) print(heapq.nlargest(2, list2, key=lambda x: x['shares']))
"""
迭代工具 - 排列 / 組合 / 笛卡爾積 """ import itertools itertools.permutations('ABCD') itertools.combinations('ABCDE', 3) itertools.product('ABCD', '123')
collections模塊下的工具類
"""
找出序列中出現次數最多的元素 """ from collections import Counter words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] counter = Counter(words) print(counter.most_common(3))
經常使用算法:
窮舉法例子:百錢百雞和五人分魚。
# 公雞5元一隻 母雞3元一隻 小雞1元三隻 # 用100元買100只雞 問公雞/母雞/小雞各多少隻 for x in range(20): for y in range(33): z = 100 - x - y if 5 * x + 3 * y + z // 3 == 100 and z % 3 == 0: print(x, y, z) # A、B、C、D、E五人在某天夜裏合夥捕魚 最後疲憊不堪各自睡覺 # 次日A第一個醒來 他將魚分爲5份 扔掉多餘的1條 拿走本身的一份 # B第二個醒來 也將魚分爲5份 扔掉多餘的1條 拿走本身的一份 # 而後C、D、E依次醒來也按一樣的方式分魚 問他們至少捕了多少條魚 fish = 1 while True: total = fish enough = True for _ in range(5): if (total - 1) % 5 == 0: total = (total - 1) // 5 * 4 else: enough = False break if enough: print(fish) break fish += 1
貪婪法例子:假設小偷有一個揹包,最多能裝20公斤贓物,他闖入一戶人家,發現以下表所示的物品。很顯然,他不能把全部物品都裝進揹包,因此必須肯定拿走哪些物品,留下哪些物品。
名稱 | 價格(美圓) | 重量(kg) |
---|---|---|
電腦 | 200 | 20 |
收音機 | 20 | 4 |
鍾 | 175 | 10 |
花瓶 | 50 | 2 |
書 | 10 | 1 |
油畫 | 90 | 9 |
"""
貪婪法:在對問題求解時,老是作出在當前看來是最好的選擇,不追求最優解,快速找到滿意解。 輸入: 20 6 電腦 200 20 收音機 20 4 鍾 175 10 花瓶 50 2 書 10 1 油畫 90 9 """ class Thing(object): """物品""" def __init__(self, name, price, weight): self.name = name self.price = price self.weight = weight @property def value(self): """價格重量比""" return self.price / self.weight def input_thing(): """輸入物品信息""" name_str, price_str, weight_str = input().split() return name_str, int(price_str), int(weight_str) def main(): """主函數""" max_weight, num_of_things = map(int, input().split()) all_things = [] for _ in range(num_of_things): all_things.append(Thing(*input_thing())) all_things.sort(key=lambda x: x.value, reverse=True) total_weight = 0 total_price = 0 for thing in all_things: if total_weight + thing.weight <= max_weight: print(f'小偷拿走了{thing.name}') total_weight += thing.weight total_price += thing.price print(f'總價值: {total_price}美圓') if __name__ == '__main__': main()
分治法例子:快速排序。
"""
快速排序 - 選擇樞軸對元素進行劃分,左邊都比樞軸小右邊都比樞軸大 """ def quick_sort(origin_items, comp=lambda x, y: x <= y): items = origin_items[:] _quick_sort(items, 0, len(items) - 1, comp) return items def _quick_sort(items, start, end, comp): if start < end: pos = _partition(items, start, end, comp) _quick_sort(items, start, pos - 1, comp) _quick_sort(items, pos + 1, end, comp) def _partition(items, start, end, comp): pivot = items[end] i = start - 1 for j in range(start, end): if comp(items[j], pivot): i += 1 items[i], items[j] = items[j], items[i] items[i + 1], items[end] = items[end], items[i + 1] return i + 1
回溯法例子:騎士巡邏。
"""
遞歸回溯法:叫稱爲試探法,按選優條件向前搜索,當搜索到某一步,發現原先選擇並不優或達不到目標時,就退回一步從新選擇,比較經典的問題包括騎士巡邏、八皇后和迷宮尋路等。 """ import sys import time SIZE = 5 total = 0 def print_board(board): for row in board: for col in row: print(str(col).center(4), end='') print() def patrol(board, row, col, step=1): if row >= 0 and row < SIZE and \ col >= 0 and col < SIZE and \ board[row][col] == 0: board[row][col] = step if step == SIZE * SIZE: global total total += 1 print(f'第{total}種走法: ') print_board(board) patrol(board, row - 2, col - 1, step + 1) patrol(board, row - 1, col - 2, step + 1) patrol(board, row + 1, col - 2, step + 1) patrol(board, row + 2, col - 1, step + 1) patrol(board, row + 2, col + 1, step + 1) patrol(board, row + 1, col + 2, step + 1) patrol(board, row - 1, col + 2, step + 1) patrol(board, row - 2, col + 1, step + 1) board[row][col] = 0 def main(): board = [[0] * SIZE for _ in range(SIZE)] patrol(board, SIZE - 1, SIZE - 1) if __name__ == '__main__': main()
動態規劃例子1:斐波拉切數列。(不使用動態規劃將會是幾何級數複雜度)
"""
動態規劃 - 適用於有重疊子問題和最優子結構性質的問題 使用動態規劃方法所耗時間每每遠少於樸素解法(用空間換取時間) """ def fib(num, temp={}): """用遞歸計算Fibonacci數""" if num in (1, 2): return 1 try: return temp[num] except KeyError: temp[num] = fib(num - 1) + fib(num - 2) return temp[num]
動態規劃例子2:子列表元素之和的最大值。(使用動態規劃能夠避免二重循環)
說明:子列表指的是列表中索引(下標)連續的元素構成的列表;列表中的元素是int類型,可能包含正整數、0、負整數;程序輸入列表中的元素,輸出子列表元素求和的最大值,例如:
輸入:1 -2 3 5 -3 2
輸出:8
輸入:0 -2 3 5 -1 2
輸出:9
輸入:-9 -2 -3 -5 -3
輸出:-2
def main(): items = list(map(int, input().split())) size = len(items) overall, partial = {}, {} overall[size - 1] = partial[size - 1] = items[size - 1] for i in range(size - 2, -1, -1): partial[i] = max(items[i], partial[i + 1] + items[i]) overall[i] = max(partial[i], overall[i + 1]) print(overall[0]) if __name__ == '__main__': main()
函數的使用方式
將函數視爲「一等公民」
高階函數的用法(filter
、map
以及它們的替代品)
items1 = list(map(lambda x: x ** 2, filter(lambda x: x % 2, range(1, 10)))) items2 = [x ** 2 for x in range(1, 10) if x % 2]
位置參數、可變參數、關鍵字參數、命名關鍵字參數
參數的元信息(代碼可讀性問題)
匿名函數和內聯函數的用法(lambda
函數)
閉包和做用域問題
Python搜索變量的LEGB順序(Local --> Embedded --> Global --> Built-in)
global
和nonlocal
關鍵字的做用
global
:聲明或定義全局變量(要麼直接使用現有的全局做用域的變量,要麼定義一個變量放到全局做用域)。
nonlocal
:聲明使用嵌套做用域的變量(嵌套做用域必須存在該變量,不然報錯)。
裝飾器函數(使用裝飾器和取消裝飾器)
例子:輸出函數執行時間的裝飾器。
def record_time(func): """自定義裝飾函數的裝飾器""" @wraps(func) def wrapper(*args, **kwargs): start = time() result = func(*args, **kwargs) print(f'{func.__name__}: {time() - start}秒') return result return wrapper
若是裝飾器不但願跟print
函數耦合,能夠編寫帶參數的裝飾器。
from functools import wraps from time import time def record(output): """自定義帶參數的裝飾器""" def decorate(func): @wraps(func) def wrapper(*args, **kwargs): start = time() result = func(*args, **kwargs) output(func.__name__, time() - start) return result return wrapper return decorate
from functools import wraps from time import time class Record(): """自定義裝飾器類(經過__call__魔術方法使得對象能夠當成函數調用)""" def __init__(self, output): self.output = output def __call__(self, func): @wraps(func) def wrapper(*args, **kwargs): start = time() result = func(*args, **kwargs) self.output(func.__name__, time() - start) return result return wrapper
說明:因爲對帶裝飾功能的函數添加了@wraps裝飾器,能夠經過
func.__wrapped__
方式得到被裝飾以前的函數或類來取消裝飾器的做用。
例子:用裝飾器來實現單例模式。
from functools import wraps def singleton(cls): """裝飾類的裝飾器""" instances = {} @wraps(cls) def wrapper(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return wrapper @singleton class President(): """總統(單例類)""" pass
說明:上面的代碼中用到了閉包(closure),不知道你是否已經意識到了。尚未一個小問題就是,上面的代碼並無實現線程安全的單例,若是要實現線程安全的單例應該怎麼作呢?
from functools import wraps def singleton(cls): """線程安全的單例裝飾器""" instances = {} locker = Lock() @wraps(cls) def wrapper(*args, **kwargs): if cls not in instances: with locker: if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return wrapper
面向對象相關知識
三大支柱:封裝、繼承、多態
例子:工資結算系統。
"""
月薪結算系統 - 部門經理每個月15000 程序員每小時200 銷售員1800底薪加銷售額5%提成 """ from abc import ABCMeta, abstractmethod class Employee(metaclass=ABCMeta): """員工(抽象類)""" def __init__(self, name): self.name = name @abstractmethod def get_salary(self): """結算月薪(抽象方法)""" pass class Manager(Employee): """部門經理""" def get_salary(self): return 15000.0 class Programmer(Employee): """程序員""" def __init__(self, name, working_hour=0): self.working_hour = working_hour super().__init__(name) def get_salary(self): return 200.0 * self.working_hour class Salesman(Employee): """銷售員""" def __init__(self, name, sales=0.0): self.sales = sales super().__init__(name) def get_salary(self): return 1800.0 + self.sales * 0.05 class EmployeeFactory(): """建立員工的工廠(工廠模式 - 經過工廠實現對象使用者和對象之間的解耦合)""" @staticmethod def create(emp_type, *args, **kwargs): """建立員工""" emp_type = emp_type.upper() emp = None if emp_type == 'M': emp = Manager(*args, **kwargs) elif emp_type == 'P': emp = Programmer(*args, **kwargs) elif emp_type == 'S': emp = Salesman(*args, **kwargs) return emp def main(): """主函數""" emps = [ EmployeeFactory.create('M', '曹操'), EmployeeFactory.create('P', '荀彧', 120), EmployeeFactory.create('P', '郭嘉', 85), EmployeeFactory.create('S', '典韋', 123000), ] for emp in emps: print('%s: %.2f元' % (emp.name, emp.get_salary())) if __name__ == '__main__': main()
類與類之間的關係
例子:撲克遊戲。
"""
經驗:符號常量老是優於字面常量,枚舉類型是定義符號常量的最佳選擇 """ from enum import Enum, unique import random @unique class Suite(Enum): """花色""" SPADE, HEART, CLUB, DIAMOND = range(4) def __lt__(self, other): return self.value < other.value class Card(): """牌""" def __init__(self, suite, face): """初始化方法""" self.suite = suite self.face = face def show(self): """顯示牌面""" suites = ['♠️', '♥️', '♣️', '♦️'] faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] return f'{suites[self.suite.value]} {faces[self.face]}' def __str__(self): return self.show() def __repr__(self): return self.show() class Poker(): """撲克""" def __init__(self): self.index = 0 self.cards = [Card(suite, face) for suite in Suite for face in range(1, 14)] def shuffle(self): """洗牌(隨機亂序)""" random.shuffle(self.cards) self.index = 0 def deal(self): """發牌""" card = self.cards[self.index] self.index += 1 return card @property def has_more(self): return self.index < len(self.cards) class Player(): """玩家""" def __init__(self, name): self.name = name self.cards = [] def get_one(self, card): """摸一張牌""" self.cards.append(card) def sort(self, comp=lambda card: (card.suite, card.face)): """整理手上的牌""" self.cards.sort(key=comp) def main(): """主函數""" poker = Poker() poker.shuffle() players = [Player('東邪'), Player('西毒'), Player('南帝'), Player('北丐')] while poker.has_more: for player in players: player.get_one(poker.deal()) for player in players: player.sort() print(player.name, end=': ')