dhclient error while loading shared libraries libdns-export.so.1102
环境:
环境:
工作需要检查相关excel数据信息, 查询一个表格中的指定位置编号,检查相关多个个相关表格中指定位置编号是否相同。 环境 windows10 python3.7 + openpyxl + xlrd 测试数据 目录结构 1 2 3 4 5 6 7 8 9 10 C:. │ checklist.py │ reference.xlsx │ └─newdir ├─dir1 │ data1.xls │ └─dir2 data2.xls 文件内容 参考表格 reference.xlsx A CODE-12345 表格1 data1.xls A CODE-12345 表格2 data2.xlsx A CODE-67890 代码 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 from openpyxl import load_workbook from openpyxl import Workbook import os import xlrd ## 获取待检查清单列表 def get_file(root_path,all_files=[]): files = os.listdir(root_path) for file in files: if not os.path.isdir(root_path + '/' + file): # 检查文件名包含"功能点", 以 "xls", "xlsx"为后缀的文件 if (file.endswith('.xls') or file.endswith('.xlsx')) \ and 'XXXX' in file: all_files.append(root_path + '/' + file) else: # 如果是目录,目录后增加'/'递归查找符合条件的文件 get_file((root_path+'/'+file),all_files) return all_files ## 获取参考文件指定位置编号 def get_request_no(root_path): files = os.listdir(root_path) for file in files: if not os.path.isdir(root_path + '/' + file): if file.endswith('.xls') or file.endswith('.xlsx') \ and 'YYYY' in file: print (file) ## only xlsx wb_in = load_workbook(file) ws_in = wb_in.active ## 查找第2行,第5列的编号 request_no = ws_in.cell(row=2, column=5) print (request_no.value + '\n') ##break return request_no.value; ## 遍历参考列表文件记录与参考文件表中需求编号不同的文件名 def check_file(files, request_no): result="" for file in files: ## 由于openpyxl, 不支持xls,这里使用的是xlrd库 wb_in = xlrd.open_workbook(file) ws_in = wb_in.sheets()[0] ## 编号是从0开始计数, 第8行, 第2列中的数据 file_request_no = ws_in.cell(rowx=7, colx=1) if str(file_request_no.value) != str(request_no): s = file_request_no.value + file +'\n' result += s return result def write_log(result): file=open("result.txt", "w") file.write(result) file.close() path = r'.\\ZZZZ' list=get_file(path) path = r'.' no=get_request_no(path) result=check_file(list, no) write_log(result) 运行结果 1 2 3 > python check.py > type result.txt CODE-67890.\\newdir/dir2/data2.xls 参考 Openpyxl Doc xlrd- API Reference
题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. Example : 1 2 3 4 5 6 7 8 9 Input: root = [5,1,5,5,5,null,5] 5 / \ 1 5 / \ \ 5 5 5 Output: 4 实现: 创建判断相同值子树方法isUnivalSubtrees, 判断其左、右节点与父节点是否相同,判定为Uni-value subtree(比较子节点与父节点val值时要先判断下对应left, right是否为空), 分别判断左子树和右子树的是否为Uni-value subtree, 如果是计数器累加。 isUnivalSubtrees 时间 复杂度O(n) 嵌入 countUnivalSubtrees后 O(n^2) 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 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param root: the given tree * @return: the number of uni-value subtrees. */ int countUnivalSubtrees(TreeNode * root) { int total = 0; if (root == nullptr){ return 0; } total = countUnivalSubtrees(root->left) + countUnivalSubtrees(root->right); if (isUnivalSubtrees(root)){ total +=1; } return total; } private: bool isUnivalSubtrees(TreeNode * root){ if (root == nullptr) { return true; } if (root->left!=nullptr && root->left->val != root->val) { return false; } if (root->right!=nullptr && root->right->val != root->val){ return false; } if (isUnivalSubtrees(root->left) && isUnivalSubtrees(root->right)){ return true; } return false; } }; 实现: 判断是否为Uni-value subtree的同时累计符合条件的子树数量,一起返回。 O(N) 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 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param root: the given tree * @return: the number of uni-value subtrees. */ int countUnivalSubtrees(TreeNode * root) { bool isUnival = true; return helperCount(root, isUnival); } int helperCount(TreeNode *root, bool& isUnival) { if (root == nullptr) { isUnival = true; return 0; } bool isLeftunival = true; bool isRightunival = true; int leftCount = 0; int rightCount = 0; leftCount = helperCount(root->left, isLeftunival); rightCount = helperCount(root->right, isRightunival); if (!isLeftunival || !isRightunival){ isUnival = false; } if (root->left !=nullptr && root->left->val != root->val ){ isUnival = false; } if (root->right !=nullptr && root->right->val != root->val ){ isUnival = false; } if (isUnival){ return leftCount+rightCount+1; } else { return leftCount+rightCount; } } }; 实现: 使用C++ pair ,判断Uni-value subtree和累计值放到一起返回。 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 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param root: the given tree * @return: the number of uni-value subtrees. */ int countUnivalSubtrees(TreeNode * root) { pair<int, bool> re = helperCount(root); return re.first; } pair<int,bool> helperCount(TreeNode *root) { pair<int, bool> res = {0, true}; if (root == nullptr) { return res; } pair<int, bool> left, right; left = helperCount(root->left); right = helperCount(root->right); if (!left.second || !right.second){ res.second = false; } if (root->left !=nullptr && root->left->val != root->val ){ res.second = false; } if (root->right !=nullptr && root->right->val != root->val ){ res.second = false; } if (res.second){ res = {left.first+right.first+1, true}; } else { res = {left.first+right.first, false}; } return res; } }; 参考及引用: https://www.youtube.com/watch?v=7HgsS8bRvjo
这篇文章Don’t solve problems if you want to be a great manager 是出自medium
极客时间 26 | 内核态内存映射:如何找到正确的会议室? 一道课后问题
文件时间信息一般包含,文件创建时间,文件修改时间,文件访问时间。 Unix/Linux 文件修改时间有两个一个是Modify Time 文件内容修改/ Change Time:文件权限状态/权限, 根据不同的文件系统,文件时间信息属性会有一些差别。Unix/Linux在挂载文件系统的时候会设置相关文件时间属性相关参数。
工作需要修改excel文件创建内容时间(excel属性非文件属性)和上次修改时间, 用python写了一个脚本处理了一下。 环境: windows10 python3.7 + openpyxl 1. 使用openpyxl修改excel属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import openpyxl from datetime import datetime fh = openpyxl.load_workbook("results.xlsx") obj = fh.properties #To get old properties print( obj ) # print old properties fh.properties.created = datetime(2000,1,1, 8,30,11) fh.properties.modified = datetime(2000,2,1, 8,32,19) ##similarly you can set other fields ## new_obj = fh.properties #Now get new properties print ( new_obj ) # print new properties fh.save("results2.xlsx") 2.使用os.utime方法修改文件属性的访问时间,修改时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import os, sys,time from datetime import datetime # Showing stat information of file stinfo = os.stat('results.xlsx') print (stinfo) # Using os.stat to recieve atime and mtime of file print ("access time of results.xlsx: %s" %stinfo.st_atime) print ("modified time of results.xlsx: %s" %stinfo.st_mtime) # Modifying atime and mtime t = datetime(2008, 1, 1, 12,12, 12) atime = time.mktime(t.timetuple()) t = datetime(2009, 1, 1, 12,12, 12) mtime = time.mktime(t.timetuple()) os.utime("results.xlsx",(atime, mtime)) print ("done!!") 3.使用pywin32-SetFileTime方法修改文件属性的创建日期, 访问时间,修改时间 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 from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING from pywintypes import Time import time from datetime import datetime def modifyFileTime(filePath, createTime, modifyTime, accessTime): try: fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes = Time(createTime) accessTimes = Time(modifyTime) modifyTimes = Time(accessTime) print (createTimes, accessTimes, modifyTimes) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return 0 except: return 1 if __name__ == '__main__': t = datetime (2019, 12,13,21,51,2) cTime = time.mktime(t.timetuple()) t = datetime (2019, 2,2,0,1,3) mTime = time.mktime(t.timetuple()) t = datetime (2019, 2,2,0,1,4) aTime = time.mktime(t.timetuple()) fName = r"results.xlsx" r = modifyFileTime(fName, cTime, mTime, aTime) if r == 0: print('修改完成') elif r == 1: print('修改失败') 参考及引用 Openpyxl Doc python 修改文件的创建时间、修改时间、访问时间 pythondoc-os.utime In Python, how do you convert a datetime object to seconds? python ImportError: No module named win32file
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given 1 2 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 1 2 3 4 5 3 / \ 9 20 / \ 15 7 实现: 递归方式,根据二叉树中序遍历和前续遍历,创建二叉树。中序遍历: 左-根-右,前续遍历: 根-左-右, 所以可以通过前续遍历的第一个节点找到根节点,并生成根节点, 因为二叉树没有重复节点, 就 可以在中序遍历数据中找到根节点,再分成左右两个子树,递归执行下去,遍历完所有节点。 通过preorder获取根节点, 通过inorder生成左右子树,查找节点在中序遍历数据组里位置,java可以通过HashMap实现。 不使用map情况下 时间复杂度 O(N^2) , 使用map查找O(N) 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 /** java * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { Map<Integer, Integer> treemap = new HashMap <>(); for (int i=0; i<inorder.length; i++){ treemap.put(inorder[i], i); } AtomicInteger rootindex = new AtomicInteger(0); return helper(preorder, inorder, 0, inorder.length -1, rootindex, treemap); } public TreeNode helper(int[] preorder, int[] inoder, int left, int right, AtomicInteger index, Map<Integer, Integer> treemap) { if (left > right){ return null; } //new node, set root int val = preorder[index.intValue()]; TreeNode node = new TreeNode(val); index //search inorder int root = treemap.get(val); //left substree node.left = helper(preorder, inoder, left, root-1, index, treemap); //right substree node.right = helper(preorder, inoder, root+1, right, index, treemap); return node ; } } 参考及引用: techiedelight-construct-binary-tree-from-inorder-preorder-traversal
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4] ...
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given 1 2 inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 1 2 3 4 5 3 / \ 9 20 / \ 15 7 实现: 递归方式,根据二叉树中序遍历和后续遍历,创建二叉树。中序遍历: 左-根-右, 后续遍历: 左-右-根, 所以可以通过后续遍历的最后一个节点找到根节点,并生成根节点, 因为二叉树没有重复节点, 就 可以在中序遍历数据中找到根节点,再分成左右两个子树,递归执行下去,遍历完所有节点。 通过postorder获取根节点, 通过inorder生成左右子树,查找节点在中序遍历数据组里位置,可以通过unordered_map实现。 不使用map情况下 时间复杂度 O(N^2) , 使用map查找O(N) 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 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { unordered_map <int, int> treemap; for (int i=0; i<inorder.size();i++) { treemap[inorder[i]] = i; } int index = inorder.size()-1; return helper(inorder, postorder, 0, inorder.size()-1, &index, treemap); } TreeNode *helper(vector<int> & inorder, vector<int>& postorder, int left, int right, int *index , unordered_map<int, int>&treemap){ //check if (left > right){ return NULL; } // create root node /* destory postorder vector int val = postorder.back(); TreeNode *node = new TreeNode(val); postorder.pop_back(); */ int val = postorder[*index]; TreeNode *node = new TreeNode(val); (*index)--; // search inorder by array /* int root = -1; for (int i=left ; i <=right; i ++){ if (val == inorder[i]){ root = i; break; } } if (root == -1){ return node; }*/ // search inorder by map int root = treemap[val]; // create left & right node node->right = helper(inorder, postorder, root+1, right, index, treemap); node->left = helper(inorder, postorder, left, root-1, index, treemap); return node; } };