Commit 0cbdaa3f by bonnieyan

提交第10次作业

parent eeaac984
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$USER_HOME$/1-homework-yanjun" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/1-homework-yanjun.iml" filepath="$PROJECT_DIR$/.idea/1-homework-yanjun.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
'''
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
'''
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 == None and l2 == None:
return None
if l1 == None:
return l2
if l2 == None:
return l1
if l1.val <= l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
def print_node(self, node):
res_list = []
while node:
res_list.append(str(node.val))
node = node.next
print('->'.join(res_list))
if __name__ == "__main__":
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(4)
node1.next = node2
node2.next = node3
l1 = node1
node_1 = ListNode(1)
node_2 = ListNode(3)
node_3 = ListNode(4)
node_1.next = node_2
node_2.next = node_3
l2 = node_1
cal = Solution()
res = cal.mergeTwoLists(l1, l2)
cal.print_node(res)
\ No newline at end of file
'''
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) -- 将元素 x 推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
'''
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.min = None
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if self.min == None or self.min > x:
self.min = x
def pop(self):
"""
:rtype: void
"""
popItem = self.stack.pop()
if len(self.stack) == 0:
self.min = None
return popItem
if popItem == self.min:
self.min = self.stack[0]
for i in self.stack:
if i < self.min:
self.min = i
return popItem
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.min
if __name__ == "__main__":
minStack = MinStack()
minStack.push(-2)
minStack.push(0)
minStack.push(-3)
print(minStack.getMin())
minStack.pop()
print(minStack.top())
print(minStack.getMin())
import xlwt
import time
import xlrd
from xlutils.copy import copy
import logging
import multiprocessing
logger =logging.getLogger()
logger.setLevel("DEBUG")
#流处理,控制输出到控制台
stream_handle = logging.StreamHandler()
#设置文件handler
file_handler = logging.FileHandler("my.log", mode='a', encoding="utf8")
logger.addHandler(file_handler)
logger.addHandler(stream_handle)
#格式化
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
stream_handle.setFormatter(formatter)
class Worker():
def __init__(self, worker_num, worker_name, worker_message, position_salary, start_time, end_time):
self.worker_num = worker_num
self.worker_name = worker_name
self.worker_message = worker_message
self.position_salary = position_salary
self.start_time = start_time
self.end_time = end_time
class Finace():
def write_excel(list_all):
book = xlwt.Workbook()
sheet = book.add_sheet('员工工资计算')
row0 = ['员工编号', '工资结算时间', '员工姓名', '员工基本信息', '岗位', '工资', '说明', '工资总计']
#写表头
for i in range(len(row0)):
sheet.write(0, i, row0[i])
row_num = 1
for c in list_all:
col_num = 0
for s in c:
sheet.write(row_num, col_num, s)
col_num += 1
row_num += 1
book.save('员工工资计算.xls')
def write_excel_append(path, listall):
index = len(listall) # 获取需要写入数据的行数
workbook = xlrd.open_workbook(path) # 打开工作簿
worksheet = workbook.sheet_by_name('员工工资计算')
rows_old = worksheet.nrows # 获取表格中已存在的数据的行数
new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象
new_worksheet = new_workbook.get_sheet(0) # 获取转化后工作簿中的第一个表格
for i in range(0, index):
for j in range(0, len(listall[i])):
new_worksheet.write(i + rows_old, j, listall[i][j]) # 追加写入数据,注意是从i+rows_old行开始写入
new_workbook.save(path) # 保存工作簿
logger.info("xls格式表格【追加】写入数据成功!")
def calc_salary(worker):
list_all = []
list_posi = []
list_sa_base =[]
start_sec = time.mktime(time.strptime(worker.start_time, '%Y-%m-%d'))
end_sec = time.mktime(time.strptime(worker.end_time, '%Y-%m-%d'))
worker_time = (int((end_sec - start_sec)/(24*60*60))) // 7
logger.info(worker_time)
for l in worker.position_salary:
for key in l.keys():
posi = key
list_posi.append(posi)
for value in l.values():
sa_base = int(value) * worker_time
list_sa_base.append(sa_base)
logger.info(list_posi)
logger.info(list_sa_base)
s_total = 0
for i in range(len(list_posi)):
list_content = []
list_content.append(worker.worker_num)
list_content.append('2019-1')
list_content.append(worker.worker_name)
list_content.append(worker.worker_message)
list_content.append(list_posi[i])
list_content.append(list_sa_base[i])
list_content.append('工资结算4周')
s_total += list_sa_base[i]
list_content.append(s_total)
list_all.append(list_content)
logger.info(list_all)
return list_all
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
list_work = []
# res = []
worker = Worker(1, '小A', '住在回龙观', [{"程序员": 2000}, {"扫地": 1000}], '2019-01-01', '2019-01-31')
worker1 = Worker(1, '小B', '居住在天通苑', [{"程序员": 2000}], '2019-01-01', '2019-01-31')
worker2 = Worker(1, '小c', '居住在西二旗', [{"程序员": 2000}], '2019-01-01', '2019-01-31')
worker3 = Worker(1, '小D', '居住在天通苑', [{"程序员": 2000}], '2019-01-01', '2019-01-31')
list_work.append(worker)
list_work.append(worker1)
list_work.append(worker2)
list_work.append(worker3)
for work in list_work:
res = pool.apply_async(Finace.calc_salary(work))
print(res.get())
# res.append(res)
pool.close()
pool.join()
#
# for r in res:
# Finace.write_excel(r)
2019-02-11 23:12:33,964 - INFO - 4
2019-02-11 23:12:33,965 - INFO - ['程序员', '扫地']
2019-02-11 23:12:33,965 - INFO - [8000, 4000]
2019-02-11 23:12:33,965 - INFO - [[1, '2019-1', '小A', '住在回龙观', '程序员', 8000, '工资结算4周', 8000], [1, '2019-1', '小A', '住在回龙观', '扫地', 4000, '工资结算4周', 12000]]
2019-02-11 23:13:48,822 - INFO - 4
2019-02-11 23:13:48,822 - INFO - ['程序员', '扫地']
2019-02-11 23:13:48,822 - INFO - [8000, 4000]
2019-02-11 23:13:48,822 - INFO - [[1, '2019-1', '小A', '住在回龙观', '程序员', 8000, '工资结算4周', 8000], [1, '2019-1', '小A', '住在回龙观', '扫地', 4000, '工资结算4周', 12000]]
2019-02-11 23:13:48,822 - INFO - 4
2019-02-11 23:13:48,822 - INFO - ['程序员']
2019-02-11 23:13:48,822 - INFO - [8000]
2019-02-11 23:13:48,822 - INFO - [[1, '2019-1', '小B', '居住在天通苑', '程序员', 8000, '工资结算4周', 8000]]
2019-02-11 23:13:48,823 - INFO - 4
2019-02-11 23:13:48,823 - INFO - ['程序员']
2019-02-11 23:13:48,823 - INFO - [8000]
2019-02-11 23:13:48,823 - INFO - [[1, '2019-1', '小c', '居住在西二旗', '程序员', 8000, '工资结算4周', 8000]]
2019-02-11 23:13:48,823 - INFO - 4
2019-02-11 23:13:48,823 - INFO - ['程序员']
2019-02-11 23:13:48,823 - INFO - [8000]
2019-02-11 23:13:48,824 - INFO - [[1, '2019-1', '小D', '居住在天通苑', '程序员', 8000, '工资结算4周', 8000]]
2019-02-11 23:14:09,274 - INFO - 4
2019-02-11 23:14:09,274 - INFO - ['程序员', '扫地']
2019-02-11 23:14:09,274 - INFO - [8000, 4000]
2019-02-11 23:14:09,274 - INFO - [[1, '2019-1', '小A', '住在回龙观', '程序员', 8000, '工资结算4周', 8000], [1, '2019-1', '小A', '住在回龙观', '扫地', 4000, '工资结算4周', 12000]]
2019-02-11 23:16:38,525 - INFO - 4
2019-02-11 23:16:38,526 - INFO - ['程序员', '扫地']
2019-02-11 23:16:38,526 - INFO - [8000, 4000]
2019-02-11 23:16:38,526 - INFO - [[1, '2019-1', '小A', '住在回龙观', '程序员', 8000, '工资结算4周', 8000], [1, '2019-1', '小A', '住在回龙观', '扫地', 4000, '工资结算4周', 12000]]
'''
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。
你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
示例:
输入: 4
输出: false
解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛;
因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。
'''
def nim_game(n):
if n % 4 == 0:
return False
else:
return True
print(nim_game(4))
print(nim_game(7))
\ No newline at end of file
'''
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
'''
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def lowestCommonAncestor(self, root, p, q):
minn = min(p.val, q.val)
maxn = max(p.val, q.val)
if root is None:
return None
if minn <= root.val <= maxn:
return root
else:
l = self.lowestCommonAncestor(root.left, p, q)
r = self.lowestCommonAncestor(root.right, p, q)
if l:
return l
if r:
return r
a = Tree = TreeNode(6)
b = Tree.left = TreeNode(2)
c = Tree.right = TreeNode(8)
d = b.left = TreeNode(0)
e = b.right = TreeNode(4)
f = c.left = TreeNode(7)
e = c.right = TreeNode(9)
f = e.right = TreeNode(3)
g = e.right = TreeNode(5)
s = Solution()
print(s.lowestCommonAncestor(a, b, c).val)
\ No newline at end of file
'''
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
示例 2:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
'''
class Solution(object):
def maxProfit(self, prices):
small = prices[0]
res = 0
for price in prices:
small = min(small, price)
if res < price - small:
res = price - small
return res
s = Solution()
print(s.maxProfit([7, 1, 5, 3, 6, 4]))
print(s.maxProfit([7, 6, 4, 3, 1]))
\ No newline at end of file
# for i in range(5):
# print(i)
#
# for j in range(0, 5):
# print(j)
import re
s = "phone number : 010-8877665 0431-98989498 0832-3821251"
reg = "0\d{2}-d{7}|0\d{3}-\d{7}|0\d{3}-\d{8}"
print(re.findall(reg,s))
#捕捉与不捕捉7665
ip = "1234.543.123.12 255.255.133.255 0.0.144.0 192.168.11.1"
reg1 = "(?:\d{1,3}\.){3}\d{1,3})"
print(re.findall(reg1, ip))
\ No newline at end of file
'''
如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵。
给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True。
示例 1:
输入:
matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
]
输出: True
解释:
在上述矩阵中, 其对角线为:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。
各条对角线上的所有元素均相同, 因此答案是True。
'''
class Matrix:
def topu_matrix(self, matrix):
......
......@@ -115,3 +115,5 @@
2019-01-27 14:23:04,655 - INFO - [8000]
2019-01-27 14:23:04,655 - INFO - [[1, '2019-1', '小D', '居住在天通苑', '程序员', 8000, '工资结算4周', 8000]]
2019-01-27 14:23:04,656 - INFO - xls格式表格【追加】写入数据成功!
2019-02-11 21:47:06,721 - INFO - 4->5->1->9->13
2019-02-11 21:47:06,723 - INFO - 4->1->9->13
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment