Skip to content

Commit 527d427

Browse files
committed
提交第五章作业
1 parent 426173a commit 527d427

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

Diff for: Homework/mona/lz_episode_05/LRU.png

142 KB
Loading

Diff for: Homework/mona/lz_episode_05/LRU.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import time
2+
import pickle # 将数据序列化
3+
import hashlib # 得到序列化数据后的字符串
4+
import functools # 保留原函数的属性
5+
6+
7+
class Node: # 根据LRU的特性制作一个链表
8+
def __init__(self, key, value, time, pre=None, nxt=None):
9+
self.key = key
10+
self.value = value
11+
self.pre = pre
12+
self.nxt = nxt
13+
self.time = time
14+
15+
16+
def judge_time(entryTime, expire_date):
17+
d = time.time() - entryTime
18+
return d > expire_date
19+
20+
21+
def hash_key(funcName, args, kwargs):
22+
key = pickle.dumps((funcName, args, kwargs))
23+
return hashlib.md5(key).hexdigest()
24+
25+
26+
class Cache:
27+
def __init__(self, expire_date=3):
28+
self.head = Node(None, None, None)
29+
self.tail = Node(None, None, None, pre=self.head)
30+
self.head.nxt = self.tail
31+
self.expire_date = 3
32+
33+
def __call__(self, func):
34+
@functools.wraps(func)
35+
def wrap(*args, **kwargs):
36+
key = hash_key(func.__name__, args, kwargs) # 得到该状态的hash值来判断是否计算过
37+
38+
t = self.head.nxt
39+
while t is not self.tail: # 判断并输出
40+
if t.key == key:
41+
if not judge_time(t.time, self.expire_date): # 如果在链表中,即把该节点放在最前面
42+
print('Hey! You have just calculated it!', end=' --> ')
43+
t.nxt.pre = t.pre
44+
t.pre.nxt = t.nxt
45+
self.head.nxt.pre = t
46+
t.nxt = self.head.nxt
47+
self.head.nxt = t
48+
t.pre = self.head
49+
return t.value
50+
else: # 超时了就把它置为没有指针指向它的节点,等python自动回收空间
51+
t.pre.nxt = t.nxt
52+
t.nxt.pre = t.pre
53+
break
54+
else:
55+
t = t.nxt
56+
57+
result = func(*args, **kwargs)
58+
newNode = Node(key, result, time.time(), self.head, self.head.nxt)
59+
self.head.nxt = newNode
60+
newNode.nxt.pre = newNode
61+
return result
62+
63+
return wrap
64+
65+
@Cache()
66+
def add(x, y):
67+
time.sleep(2)
68+
return x+y
69+
70+
print(add(1,2))
71+
72+
print(add(1,2))
73+
74+
print(add(1,3))
75+
76+
print(add(1,3))

Diff for: Homework/mona/lz_episode_05/base64.png

108 KB
Loading

Diff for: Homework/mona/lz_episode_05/base64.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import base64
2+
import math
3+
4+
b64 = {
5+
'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,
6+
'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,'a':26,'b':27,
7+
'c':28,'d':29,'e':30,'f':31,'g':32,'h':33,'i':34,'j':35,'k':36,'l':37,'m':38,'n':39,'o':40,'p':41,
8+
'q':42,'r':43,'s':44,'t':45,'u':46,'v':47,'w':48,'x':49,'y':50,'z':51,'0':52,'1':53,'2':54,'3':55,
9+
'4':56,'5':57,'6':58,'7':59,'8':60,'9':61,'+':62,'/':63
10+
}
11+
12+
b = [] # 存放解码后的二进制数
13+
14+
15+
def transform(num):
16+
l = [5, 4, 3, 2, 1, 0]
17+
for x in range(6):
18+
if num >= pow(2, l[x]):
19+
b.append(1)
20+
num -= pow(2, l[x])
21+
else:
22+
b.append(0)
23+
24+
25+
def printChar(lst):
26+
l = [7, 6, 5, 4, 3, 2, 1, 0]
27+
num_Ascall = 0
28+
for x in range(8):
29+
if lst[x] == 1:
30+
num_Ascall += pow(2, l[x])
31+
32+
ch = chr(num_Ascall)
33+
print(ch, end='')
34+
35+
ori_s = 'Man'
36+
print('The original string is:{}'.format(ori_s))
37+
encodestr = base64.b64encode(ori_s.encode())
38+
print('The base64 string is:{}'.format(encodestr))
39+
s = encodestr.decode()
40+
41+
for i in range(len(s)):
42+
num = 0
43+
if s[i] is not '=':
44+
num = b64[s[i]]
45+
transform(num)
46+
47+
lst = [] # 存放八位二进制
48+
lst.append(b[0])
49+
print('The result of the decoding is:',end = '')
50+
51+
for x in range(1, len(b)): # 每8位输出一个字符
52+
if x % 8 == 0:
53+
printChar(lst)
54+
lst.clear()
55+
lst.append(b[x])
56+
57+
printChar(lst)

0 commit comments

Comments
 (0)