python练习之变位词
from timeit import Timer
def anagram_solution(s1, s2):
c1 = [0] * 26
c2 = [0] * 26
for i in range(len(s1)):
pos = ord(s1[i]) - ord('a')
c1[pos] += 1
for j in range(len(s2)):
pos = ord(s2[j]) - ord('a')
c2[pos] += 1
for i in range(26):
if c1[i] == c2[i]:
continue
else:
return False
return True
t1 = Timer("anagram_solution('heldfglo', 'ellsdfgboh')", "from __main__ import anagram_solution")
print(t1.timeit(10000))
python练习之用数组实现栈
class Stack:
def __init__(self):
self.list = []
def pop(self):
return self.list.pop()
def push(self, value):
self.list.append(value)
def peek(self):
return self.list[len(self.list)-1]
def size(self):
return len(self.list)
def isEmpty(self):
return self.list == []
s = Stack()
s.push(8)
s.push(4)
s.push('a')
s.push(9)
print(s.size())
print(s.peek())
print(s.pop())
print(s.pop())
print(s.pop())
print(s.size())
python练习之用数组实现队列
class Queue:
def __init__(self):
self.list = []
def enqueue(self, item):
self.list.insert(0, item)
def dequeue(self):
return self.list.pop()
def isEmpty(self):
return self.list == []
def size(self):
return len(self.list)
q = Queue()
q.enqueue('k')
q.enqueue('a')
q.enqueue('l')
q.enqueue('e')
print(q.isEmpty())
print(q.size())
print(q.dequeue())
print(q.dequeue())
print(q.size())