ARTS-week02

Algorithms

本周算法题:leetcode02
注意进位问题。

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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *res = new ListNode(-1);
ListNode *cur = res;
int carry = 0;
while (l1 || l2) {
int n1 = l1 ? l1->val : 0;
int n2 = l2 ? l2->val : 0;
int sum = n1 + n2 + carry;
carry = sum / 10;
cur->next = new ListNode(sum % 10);
cur = cur->next;
if (l1) l1 = l1->next;
if (l2) l2 = l2->next;
}
if (carry) cur->next = new ListNode(1);
return res->next;
}
};

Review

本周阅读英文文章:Choosing A Master Password
在密码强度上,应该选用一个主密码,根据不同的服务去按特定方式对主密码进行修改,这样每个网站的密码不一致,当某一网站发生泄露时,可以保证其他帐户的安全。

目前网站注册数量过多,最好将密码进行备份,存储为加密文件或者使用密码管理器(如LastPass,1Password,Dashlane,KeePassX等)。个人认为除了用户自己加强这方面的意识以外,各服务商也应该提供二次验证,备用码等方案。

密码熵:

In its simplest definition, password entropy is a measure of how unpredictable (or random) a password is. It is traditionally expressed in ‘bits’, where a bit is a binary digit representing two possible states (on or off, true or false, 1 or 0). For example, password with 10-bit entropy would be one that is equally likely to be one of any 1,024 possibilities (1,024 = 2¹⁰ = 2x2x2x2x2x2x2x2x2x2).

Technique

在学习Pytorch官方教程中Data Loading and Processing Tutorial中遇到BrokenPipeError: [Errno 32] Broken pipe
这个错误,反复确认代码没有问题后,开始搜索原因,疑似PyTorch的多线程库在Windows下工作还不正常,最后将num_workers的值由4改为0,工作正常。

环境:Windows10 64 Anaconda3 pytorch-cpu

解决该问题的链接:
https://github.com/pytorch/pytorch/issues/2341

https://zhuanlan.zhihu.com/p/26871672

https://discuss.pytorch.org/t/brokenpipeerror-errno-32-broken-pipe-when-i-run-cifar10-tutorial-py/6224/4?u=karmus89

Share

PythonCookBook
在迭代操作或者其他操作的时候,怎样只保留最后有限几个元素的历史记录?
保留有限历史记录使用collections.deque。比如,下面的代码在多行上面做简单的文本匹配, 并返回匹配所在行的最后N行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from collections import deque

def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in line:
yield line, previous_lines
previous_lines.append(line)

# Example use on a file
if __name__ == '__main__':
with open(r'../../cookbook/somefile.txt') as f:
for line, prevlines in search(f, 'python', 5):
for pline in prevlines:
print(pline, end='')
print(line, end='')
print('-' * 20)
0%