Python 100天-從新手到大師學習筆記番外篇:演算法入門系列課程1 — 周而復始

算法概述

nums = []
for i in range(100000):
nums.append(i)
nums.reverse()
nums = []
for i in range(100000):
nums.insert(0, i)
a, b = 0, 1
for num in range(1, 101):
a, b = b, a + b
print(f'{num}: {a}')
def fib(num):
if num in (1, 2):
return 1
return fib(num - 1) + fib(num - 2)


for num in range(1, 101):
print(f'{num}: {fib(num)}')
def fib(num, temp={}):
if num in (1, 2):
return 1
elif num not in temp:
temp[num] = fib(num - 1) + fib(num - 2)
return temp[num]
from functools import lru_cache


@lru_cache()
def fib(num):
if num in (1, 2):
return 1
return fib(num - 1) + fib(num - 2)

如何评价算法的好坏?

O符号的意义?

Image for post
Image for post

穷举法

import re

import PyPDF2

with open('Python_Tricks_encrypted.pdf', 'rb') as pdf_file_stream:
reader = PyPDF2.PdfFileReader(pdf_file_stream)
with open('dictionary.txt', 'r') as txt_file_stream:
file_iter = iter(lambda: txt_file_stream.readline(), '')
for word in file_iter:
word = re.sub(r'\s', '', word)
if reader.decrypt(word):
print(word)
break

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store