-
Notifications
You must be signed in to change notification settings - Fork 13
/
p412.py
472 lines (397 loc) · 11.6 KB
/
p412.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# 实现一个累加
import time
import timeit
def sumofN(n):
start = time.time()
theSum = 0
for i in range(1,n+1):
theSum = theSum+i
end = time.time()
return theSum,end-start
def sumofN2(n):
start = time.time()
theSum = 0;
for i in range(1,n+1):
theSum = theSum+i
end = time.time()
return theSum,end-start
def sumofN3(n):
start = time.time()
end = time.time()
return (n*(n+1))/2,end-start
# give a test the running time
# for i in range(5):
# print("sumofN :Sum is %d required %10.7f seconds "%sumofN(10000000))
# for i in range(5):
# print("sumofN2 :Sum is %d required %10.7f seconds "%sumofN2(10000000))
# for i in range(5):
# print("sumofN3 :Sum is %d required %10.7f seconds "%sumofN3(10000000))
# 以下实现了一个两个字符串判定是否是乱序
def anagramSolution4(s1,s2):
c1=[0]*26
c2=[0]*26
for i in range(len(s1)):
pos = ord(s1[i])-ord('a')
c1[pos] = c1[pos]+1
for i in range(len(s2)):
pos = ord(s2[i])-ord('a')
c2[pos]=c2[pos]+1
j=0
stillOK = True
while j<26 and stillOK:
if c1[j] == c2[j]:
j=j+1
else:
stillOK=False
return stillOK
# print(anagramSolution4('apple','pleap'))
#
# 关于列表的一些操作
def test1():
l =[]
for i in range(1000):
l = l+[i]
def test2():
l=[]
for i in range(1000):
l.append(i)
def test3():
l = [i for i in range(1000)]
def test4():
l = list(range(1000))
# t1 = Timer("test1()","from __main__ import test1")
# print("concat ",t1.timeit(number=1000),"milliseconds")
#
# t1 = time("test2()","from __main__ import test1")
# print("append ",t1.timeit(number=1000),"milliseconds")
#
# t1 = time("test3()","from __main__ import test1")
# print("comprehension ",t1.timeit(number=1000),"milliseconds")
#
# t1 = time("test4()","from __main__ import test1")
# print("list range ",t1.timeit(number=1000),"milliseconds")
# popzero = timeit.Timer("x.pop(0)","from __main__ import x")
# popend = timeit.Timer("x.pop(0)","from __main__ import x")
#
# x = list(range(2000000))
# popzero.timeit(number=1000)
#
# x = list(range(2000000))
# popzero.timeit(number=1000)
# import timeit
# import random
#
# for i in range(10000,1000001,20000):
# t = timeit.Timer("random.randrange(%d) in x"%i,"from __main__ import random,x")
# x = list(range(i))
# list_time = t.timeit(number=1000)
# x = {j:None for j in range(i)}
# d_time = t.timeit(number=1000)
# print("%d,%10.3f,%10.3f"%(i,list_time,d_time))
# x = {j:None for j in range(10000)}
# y = [i for i in range(10000)]
# z = list(range(10000))
# print(x)
# print(y)
# print(z)
# 一些关于栈的知识
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
# s = Stack()
# print(s.isEmpty())
# s.push(4)
# s.push('dog')
# print(s.peek())
# s.push(True)
# print(s.size())
# print(s.isEmpty())
# s.push(8.4)
# print(s.pop())
# print(s.pop())
# print(s.size())
#
# 一个简单的括号检测函数,使用栈来实现
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
index =index+1
if balanced and s.isEmpty():
return True
else:
return False
# print(parChecker('((()))'))
# print(parChecker('(()'))
def parChecker1(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
if not matches(top,symbol):
balanced = False
index = index+1
index =index+1
if balanced and s.isEmpty():
return True
else:
return False
def matches(open,close):
opens = "([{"
closers = ")]}"
return opens.index(open) == closers.index(close)
# print(parChecker1('{{([][])}()}'))
# print(parChecker1('[]'))
# 二进制字符串
def divideBy2(decNumber):
remstack = Stack()
while decNumber > 0:
rem = decNumber % 2
remstack.push(rem)
decNumber = decNumber // 2
binString = ""
while not remstack.isEmpty():
binString = binString + str(remstack.pop())
return binString
# print(divideBy2(42))
# 定义一个通用的进制转换
def baseConvert(decNumbert,base):
digits = "0123456789ABCDEF"
remstack = Stack()
while decNumbert > 0:
rem = decNumbert % base
remstack.push(rem)
decNumbert = decNumbert // base
newString = ""
while not remstack.isEmpty():
newString = newString + digits[remstack.pop()]
return newString
# print(baseConvert(135, 2))
# print(baseConvert(13, 16))
# 以下编写关于队列的一些知识
class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
# s = Queue()
# print(s.isEmpty())
# s.enqueue('dog')
# s.enqueue(84)
# print(s.size())
# s.dequeue()
# print(s.size())
# 模拟一个队列的操作
def hotPotato(namelist,num):
simqueue = Queue()
for name in namelist:
simqueue.enqueue(name)
while simqueue.size() > 1:
for i in range(num):
simqueue.enqueue(simqueue.dequeue())
print(simqueue.dequeue())
# 显示每一次删除的是谁
return simqueue.dequeue()
# print(hotPotato(["Bill","David","Susan","Jack","Kent","Brad"],3))
# 演示一个打印序列
class Printer:
def __init__(self,ppm):
self.pagerate = ppm
self.currentTask = None
self.timeRemaining = 0
def tick(self):
if self.currentTask != None:
self.timeRemaining = self.timeRemaining - 1
if self.timeRemaining <= 0:
self.currentTask = None
def busy(self):
if self.currentTask != None:
return True
else:
return False
def startNext(self,newtask):
self.currentTask = newtask
self.timeRemaining = newtask.getPages()*60/self.pagerate
import random
class Task:
def __init__(self,time):
self.timestamp = time
self.pages = random.randrange(1,21)
def getStamp(self):
return self.timestamp
def getPages(self):
return self.pages
def waitTime(self,currenttime):
return currenttime - self.timestamp
def simulation(numSeconds,pagesPerMinute):
labprinter = Printer(pagesPerMinute)
printQueue = Queue()
waitingtimes = []
for curentSecond in range(numSeconds):
if newPrintTask():
task =Task(curentSecond)
printQueue.enqueue(task)
if (not labprinter.busy()) and (not printQueue.isEmpty()):
nexttask = printQueue.dequeue()
waitingtimes.append(nexttask.waitTime(curentSecond))
labprinter.startNext(nexttask)
labprinter.tick()
averageWait = sum(waitingtimes)/len(waitingtimes)
print("Average wait %6.2f secs %3d tasks remaining. "%(averageWait,printQueue.size()))
def newPrintTask():
num = random.randrange(1,181)
if num ==180:
return True
else:
return False
# for i in range(10):
# simulation(3600,10)
# 双端队列的实现
class Deque:
def __init__(self):
self.items = []
def isEmpty(self,item):
self.items.append(item)
def addRear(self,item):
self.items.insert(0,item)
def removeFront(self):
return self.items.pop()
def removeReare(self):
return self.items.pop(0)
def size(self):
return len(self.items)
# 利用双端队列检查回文
def palchecker(aString):
chardeque = Deque()
for ch in aString:
chardeque.addRear(ch)
stillEqual = True
while chardeque.size() > 1 and stillEqual:
first = chardeque.removeFront()
last = chardeque.removeReare()
if first != last:
stillEqual = False
return stillEqual
# print(palchecker("lsdkjfskf"))
# print(palchecker("raar"))
# 定义一个列表
class Node:
def __init__(self, data):
self.data = data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, new_data):
self.data = new_data
def set_next(self, new_next):
self.next = new_next
# temp = Node(93)
# print(temp.get_data())
class UnorderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.set_next(self.head)
def size(self):
current = self.head
count = 0
while current != None:
count = count+1
current = current.get_next()
return count
def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
# my_list = UnorderedList()
# print(my_list.is_empty())
class OrderedList:
def __init__(self):
self.head = None
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not stop:
if current.get_data() == item:
founf = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found
def add(self,item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next
temp = Node(item)
if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)