python学习笔记_week28
import heapq
import random
heap = []
data = list(range(10000))
random.shuffle(data)
# for num in data:
# heapq.heappush(heap, num)
# for i in range(len(heap)):
# print(heapq.heappop(heap))
print(heapq.nsmallest(10, data))
class Node(object):
def __init__(self, item=None):
self.item = item
self.next = None
head = Node()
head.next = Node(20)
head.next.next = Node(30)
def traversal(head):
curNode = head # 临时用指针
while curNode is not None:
print(curNode.item)
curNode = curNode.next
traversal(head)
maze = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,0,0,1,1,0,0,1],
[1,0,1,1,1,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,1],
[1,0,1,0,0,0,1,0,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,1,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1]
]
dirs = [lambda x, y: (x + 1, y),
lambda x, y: (x - 1, y),
lambda x, y: (x, y - 1),
lambda x, y: (x, y + 1)] def mpath(x1, y1, x2, y2):
stack = []
stack.append((x1, y1))
while len(stack) > 0:
curNode = stack[-1]
if curNode[0] == x2 and curNode[1] == y2:
#到达终点
for p in stack:
print(p)
return True
for dir in dirs:
nextNode = dir(curNode[0], curNode[1])
if maze[nextNode[0]][nextNode[1]] == 0:
#找到了下一个
stack.append(nextNode)
maze[nextNode[0]][nextNode[1]] = -1 # 标记为已经走过,防止死循环
break
else:#四个方向都没找到
maze[curNode[0]][curNode[1]] = -1 # 死路一条,下次别走了
stack.pop() #回溯
print("没有路")
return False
mpath(1,1,8,8)
from collections import deque
mg = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,0,0,1,1,0,0,1],
[1,0,1,1,1,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,1],
[1,0,1,0,0,0,1,0,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,1,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1]
]
dirs = [lambda x, y: (x + 1, y),
lambda x, y: (x - 1, y),
lambda x, y: (x, y - 1),
lambda x, y: (x, y + 1)] def print_p(path):
curNode = path[-1]
realpath = []
print('迷宫路径为:')
while curNode[2] != -1:
realpath.append(curNode[0:2])
curNode = path[curNode[2]]
realpath.append(curNode[0:2])
realpath.reverse()
print(realpath)
def mgpath(x1, y1, x2, y2):
queue = deque()
path = []
queue.append((x1, y1, -1))
while len(queue) > 0:
curNode = queue.popleft()
path.append(curNode)
if curNode[0] == x2 and curNode[1] == y2:
#到达终点
print_p(path)
return True
for dir in dirs:
nextNode = dir(curNode[0], curNode[1])
if mg[nextNode[0]][nextNode[1]] == 0: # 找到下一个方块
queue.append((*nextNode, len(path) - 1))
mg[nextNode[0]][nextNode[1]] = -1 # 标记为已经走过
return False
mgpath(1,1,8,8)
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
print(queue.popleft())
import time
import random
def cal_time(func):
def wrapper(*args, **kwargs):
t1 = time.time()
result = func(*args, **kwargs)
t2 = time.time()
print("%s running time: %s secs." % (func.__name__, t2 - t1))
return result
return wrapper
@cal_time
def bin_search(data_set, val):
low = 0
high = len(data_set) - 1
while low <= high:
mid = (low+high)//2
if data_set[mid] == val:
return mid
elif data_set[mid] < val:
low = mid + 1
else:
high = mid - 1
return
def binary_search(dataset, find_num):
if len(dataset) > 1:
mid = int(len(dataset) / 2)
if dataset[mid] == find_num:
#print("Find it")
return dataset[mid]
elif dataset[mid] > find_num:
return binary_search(dataset[0:mid], find_num)
else:
return binary_search(dataset[mid + 1:], find_num)
else:
if dataset[0] == find_num:
#print("Find it")
return dataset[0]
else:
pass
#print("Cannot find it.")
@cal_time
def binary_search_alex(data_set, val):
return binary_search(data_set, val)
def random_list(n):
result = []
ids = list(range(1001,1001+n))
a1 = ['zhao','qian','sun','li']
a2 = ['li','hao','','']
a3 = ['qiang','guo']
for i in range(n):
age = random.randint(18,60)
id = ids[i]
name = random.choice(a1)+random.choice(a2)+random.choice(a3)
data = list(range(100000))
print(bin_search(data, 90))
print(binary_search_alex(data, 90))
import random
import time
import copy
import sys
def cal_time(func):
def wrapper(*args, **kwargs):
t1 = time.time()
result = func(*args, **kwargs)
t2 = time.time()
print("%s running time: %s secs." % (func.__name__, t2 - t1))
return result
return wrapper
@cal_time
def bubble_sort(li):
for i in range(len(li) - 1):
for j in range(len(li) - i - 1):
if li[j] > li[j+1]:
li[j], li[j+1] = li[j+1], li[j]
@cal_time
def bubble_sort_1(li):
for i in range(len(li) - 1):
exchange = False
for j in range(len(li) - i - 1):
if li[j] > li[j+1]:
li[j], li[j+1] = li[j+1], li[j]
exchange = True
if not exchange:
break
def select_sort(li):
for i in range(len(li) - 1):
min_loc = i
for j in range(i+1,len(li)):
if li[j] < li[min_loc]:
min_loc = j
li[i], li[min_loc] = li[min_loc], li[i]
def insert_sort(li):
for i in range(1, len(li)):
tmp = li[i]
j = i - 1
while j >= 0 and li[j] > tmp:
li[j+1]=li[j]
j = j - 1
li[j + 1] = tmp
def quick_sort_x(data, left, right):
if left < right:
mid = partition(data, left, right)
quick_sort_x(data, left, mid - 1)
quick_sort_x(data, mid + 1, right)
def partition(data, left, right):
tmp = data[left]
while left < right:
while left < right and data[right] >= tmp:
right -= 1
data[left] = data[right]
while left < right and data[left] <= tmp:
left += 1
data[right] = data[left]
data[left] = tmp
return left
@cal_time
def quick_sort(data):
return quick_sort_x(data, 0, len(data) - 1)
@cal_time
def sys_sort(data):
return data.sort()
def sift(data, low, high):
i = low
j = 2 * i + 1
tmp = data[i]
while j <= high: #孩子在堆里
if j + 1 <= high and data[j] < data[j+1]: #如果有右孩子且比左孩子大
j += 1 #j指向右孩子
if data[j] > tmp: #孩子比最高领导大
data[i] = data[j] #孩子填到父亲的空位上
i = j #孩子成为新父亲
j = 2 * i +1 #新孩子
else:
break
data[i] = tmp #最高领导放到父亲位置
@cal_time
def heap_sort(data):
n = len(data)
for i in range(n // 2 - 1, -1, -1):
sift(data, i, n - 1)
#堆建好了
for i in range(n-1, -1, -1): #i指向堆的最后
data[0], data[i] = data[i], data[0] #领导退休,刁民上位
sift(data, 0, i - 1) #调整出新领导
# def heap_sort(data):
# n = len(data)
# for i in range(n // 2 - 1, -1, -1):
# sift(data, i, n - 1)
# #堆建好了
# li = []
# for i in range(n-1, -1, -1): #i指向堆的最后
# li.append(data[0])
# data[i] = data[0]
# sift(data,0,i-1)
def merge(li, low, mid, high):
i = low
j = mid + 1
ltmp = []
while i <= mid and j <= high:
if li[i] < li[j]:
ltmp.append(li[i])
i += 1
else:
ltmp.append(li[j])
j += 1
while i <= mid:
ltmp.append(li[i])
i += 1
while j <= high:
ltmp.append(li[j])
j += 1
li[low:high+1] = ltmp
def _mergesort(li, low, high):
if low < high:
mid = (low + high) // 2
_mergesort(li,low, mid)
_mergesort(li, mid+1, high)
merge(li, low, mid, high)
@cal_time
def mergesort(li):
_mergesort(li, 0, len(li) - 1)
@cal_time
def insert_sort(li):
for i in range(1, len(li)):
tmp = li[i]
j = i - 1
while j >= 0 and li[j] > tmp:
li[j+1]=li[j]
j = j - 1
li[j + 1] = tmp
@cal_time
def shell_sort(li):
gap = int(len(li) // 2)
while gap >= 1:
for i in range(gap, len(li)):
tmp = li[i]
j = i - gap
while j >= 0 and tmp < li[j]:
li[j + gap] = li[j]
j -= gap
li[i - gap] = tmp
gap = gap // 2
@cal_time
def count_sort(li, max_num):
count = [0 for i in range(max_num + 1)]
for num in li:
count[num] += 1
i = 0
for num,m in enumerate(count):
for j in range(m):
li[i] = num
i += 1
@cal_time
def insert_sort(li):
for i in range(1, len(li)):
tmp = li[i]
j = i - 1
while j >= 0 and li[j] > tmp:
li[j+1]=li[j]
j = j - 1
li[j + 1] = tmp
# def topk(li, k):
# ltmp = li[0:k + 1]
# insert_sort(ltmp)
# for i in range(k, len(li)):
# ltmp[k]=li[i]
# tmp = ltmp[k]
# j = k - 1
# while j >= 0 and ltmp[j] > tmp:
# li[j + 1] = ltmp[j]
# j = j - 1
# li [j + 1] = ltmp
# return ltmp
def topn(li, n):
heap = li[0:n]
for i in range(n // 2 - 1, -1, -1):
sift(heap, i, n - 1)
#遍历
for i in range(n, len(li)):
if li[i] > heap[0]:
heap[0] = li[i]
sift(heap, 0, n - 1)
for i in range(n - 1, -1, -1): # i指向堆的最后
heap[0], heap[i] = heap[i], heap[0] # 领导退休,刁民上位
sift(heap, 0, i - 1) # 调整出新领导
return heap
# sys.setrecursionlimit(100000)
# #data = list(range(100000, 0, -1))
# data = []
# for i in range(100000):
# data.append(random.randint(0,100))
# # data.sort()
# #random.shuffle(data)
# data1 = copy.deepcopy(data)
# data2 = copy.deepcopy(data)
# data3 = copy.deepcopy(data)
# #
# # bubble_sort(data1)
# #quick_sort(data2)
# count_sort(data1, 100)
# # sys_sort(data3)
# #mergesort(data3)
# sys_sort(data3)
# li=[1,4,5,6,2,3,7,8,9]
# merge(li, 0, 3, 8)
# print(li)
li = list(range(10000))
random.shuffle(li)
print(topn(li, 10))
def cheak_kuohao(s):
stack = []
for char in s:
if char in {'(','[', '{'}:
stack.append(char)
elif char == ')':
if len(stack)>0 and stack[-1]=='(':
stack.pop()
else:
return False
elif char == ']':
if len(stack) > 0 and stack[-1] == '[':
stack.pop()
else:
return False
elif char == '}':
if len(stack)>0 and stack[-1]=='{':
stack.pop()
else:
return False
if len(stack) == 0:
return True
else:
return False
print(cheak_kuohao('()[]{{[]}}'))
def bin_search(data_set, val):
low = 0
high = len(data_set) - 1
while low <= high:
mid = (low+high)//2
if data_set[mid] == val:
left = mid
right = mid
while left >= 0 and data_set[left] == val:
left -= 1
while right < len(data_set) and data_set[right] == val:
right += 1
return (left + 1, right - 1)
elif data_set[mid] < val:
low = mid + 1
else:
high = mid - 1
return (-1, -1)
def bin_search(data_set, val):
low = 0
high = len(data_set) - 1
while low <= high:
mid = (low+high)//2
if data_set[mid] == val:
left = mid
right = mid
while left >= 0 and data_set[left] == val:
left -= 1
while right < len(data_set) and data_set[right] == val:
right += 1
return (left + 1, right - 1)
elif data_set[mid] < val:
low = mid + 1
else:
high = mid - 1
return
li = [1,2,3,3,3,4,4,5]
print(bin_search(li, 5))
import copy
li = [1, 5, 4, 2]
target = 3
max_num = 100
def func1():
for i in range(len(li)):
for j in range(i+1, len(li)):
if li[i] + li[j] == target:
return (i,j)
def bin_search(data_set, val, low, high):
while low <= high:
mid = (low+high)//2
if data_set[mid] == val:
return mid
elif data_set[mid] < val:
low = mid + 1
else:
high = mid - 1
return
def func2():
li2 = copy.deepcopy(li)
li2.sort()
for i in range(len(li2)):
a = i
b = bin_search(li2, target - li2[a], i+1, len(li2)-1)
if b:
return (li.index(li2[a]),li.index(li2[b]))
def func3():
a = [None for i in range(max_num+1)]
for i in range(len(li)):
a[li[i]] = i
if a[target-li[i]] != None:
return (a[li[i]], a[target-li[i]])
print(func3())
data_dict = {}
for i in range(len(data_list)):
if data_list[i] in data_dict:
print(data_dict[data_list[i]], i)
else:
data_dict[13 - data_list[i]] = i
import random
def insert(li, i):
tmp = li[i]
j = i - 1
while j >= 0 and li[j] > tmp:
li[j + 1] = li[j]
j = j - 1
li[j + 1] = tmp
def insert_sort(li):
for i in range(1, len(li)):
insert(li, i)
def topk(li, k):
top = li[0:k + 1]
insert_sort(top)
for i in range(k+1, len(li)):
top[k] = li[i]
insert(top, k)
return top[:-1]
def sift(data, low, high):
i = low
j = 2 * i + 1
tmp = data[i]
while j <= high: #孩子在堆里
if j + 1 <= high and data[j] < data[j+1]: #如果有右孩子且比左孩子大
j += 1 #j指向右孩子
if data[j] > tmp: #孩子比最高领导大
data[i] = data[j] #孩子填到父亲的空位上
i = j #孩子成为新父亲
j = 2 * i +1 #新孩子
else:
break
data[i] = tmp #最高领导放到父亲位置
def topn(li, n):
heap = li[0:n]
for i in range(n // 2 - 1, -1, -1):
sift(heap, i, n - 1)
#遍历
for i in range(n, len(li)):
if li[i] < heap[0]:
heap[0] = li[i]
sift(heap, 0, n - 1)
for i in range(n - 1, -1, -1): # i指向堆的最后
heap[0], heap[i] = heap[i], heap[0] # 领导退休,刁民上位
sift(heap, 0, i - 1) # 调整出新领导
return heap
data = list(range(10000))
random.shuffle(data)
print(topn(data, 10))
PPT
python学习笔记_week28的更多相关文章
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- Pythoner | 你像从前一样的Python学习笔记
Pythoner | 你像从前一样的Python学习笔记 Pythoner
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
随机推荐
- inode 与black 特点与简介
inodo为索引节点, 存放文件属性的信息(实际文件位置与容量信息 /black文件属性) 作用在格式化文件系统时候产生 创建一个文件就占用一个inode 数字相同为硬链接 black为实际数据/内容 ...
- ant通配符
ANT通配符有三种: 通配符 说明 ? 匹配任何单字符 * 匹配0或者任意数量的字符 ** 匹配0或者更多的目录 例子: URL路径 说明 /app/*.x 匹配(Matches)所有在app路径下的 ...
- gzip 所使用压缩算法的基本原理(选摘)
摘自:http://blog.csdn.net/ghevinn/article/details/45747465 gzip 所使用压缩算法的基本原理 gzip 对于要压缩的文件,首先使用LZ77算法 ...
- video自动填充满父级元素
想要video能自动填充慢父div的大小,只要给video标签加上style="width= 100%; height=100%; object-fit: fill"即可. obj ...
- Spell checker using hash table
Problem description Given a text file, show the spell errors from it. (https://www.andrew.cmu.edu/c ...
- ubuntu16.04x下搜狗输入法无法输入中文
使用如下命令: cd ~/,config find . -name sogou* 找到sogou-qimpanel ,sudo rm -r ./sogou-qimpanel删除 find . -nam ...
- [ 随手记 1 ] C/C++宏,普通函数,内联函数
函数定义 C 语言中的函数定义的一般形式如下: return_type function_name( parameter list ) { body of the function } 在 C 语言中 ...
- 几个特殊的IP地址
1)私有地址 IP地址在全世界范围内唯一,看到这句话你可能有这样的疑问,像192.168.0.1这样的地址在许多地方都能看到,并不唯一,这是为何?Internet管理委员会规定如下地址段为私有 ...
- js barcode 打印
新建 html <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset=&quo ...
- [CSL 的字符串][栈,模拟]
链接:https://ac.nowcoder.com/acm/contest/551/D来源:牛客网题目描述 CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助. 给定一个字 ...