ARTS-week34

Algorithms

Binary Tree Paths
思路: 递归

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if (!root) {
return {};
}
if (!root->left && !root->right) {
return {to_string(root->val)};
}

vector<string> left = binaryTreePaths(root->left);
vector<string> right = binaryTreePaths(root->right);

left.insert(left.end(), right.begin(), right.end());
for (auto &i : left) {
i = to_string(root->val) + "->" + i;
}
return left;
}
};

Add Digits

1
2
3
4
5
6
7
8
9
class Solution {
public:
int addDigits(int num) {
while(num > 9) {
num = num / 10 + num % 10;
}
return num;
}
};

看到一个一行搞定的答案:

1
2
3
4
5
6
class Solution {
public:
int addDigits(int num) {
return (num == 0) ? 0 : (num - 1) % 9 + 1;
}
};

Review

本周阅读英文文章 Building a Music Recommendation Engine with Probabilistic Matrix Factorization in PyTorch

Technique

使用joblib提供基于磁盘的简单缓存,并且支持非常大的numpy数组,首先安装joblib库:

1
pip install joblib

使用装饰器Memory.cache来存储函数结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> from joblib import Memory
>>> memory = Memory(cachedir="./save")
>>>
>>> @memory.cache
... def sum_m(a, b):
... return a + b
...
>>> print(sum_m(1, 2))
________________________________________________________________________________
[Memory] Calling __main__--home-ubuntu-test-<stdin>.sum_m...
sum_m(1, 2)
____________________________________________________________sum_m - 0.0 s, 0.0 min
3

看一下save的目录结构:

1
2
3
4
5
6
7
8
9
10
11
$ tree -h
.
└── [4.0K] joblib
└── [4.0K] __main__--home-ubuntu-test-<stdin>
└── [4.0K] sum_m
├── [4.0K] 0f2b8227db955581b11071a6cf8988a3
│   ├── [ 70] metadata.json
│   └── [ 5] output.pkl
└── [ 36] func_code.py

4 directories, 3 files

当在类中使用时,只需要:

1
2
3
4
5
6
7
class Foo(object):

def __init__(self, args):
self.method = memory.cache(self.method)

def method(self, ...):
pass

Share

使用iperf3测试网络带宽

1
2
3
4
5
$ git clone https://github.com/esnet/iperf.git
$ cd iperf/
$ ./configure
$ make
$ make install

iperf必须安装在链路的两端: 服务器端和客户端,在安装好之后启动服务端:

1
$ iperf3 -s

然后启动客户端连接服务器,生成吞吐量统计:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ iperf3 -c 192.168.1.2
Connecting to host 192.168.1.2, port 5201
[ 5] local 192.168.1.6 port 38590 connected to 192.168.1.2 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 112 KBytes 915 Kbits/sec 3 23.4 KBytes
[ 5] 1.00-2.00 sec 109 KBytes 892 Kbits/sec 0 26.2 KBytes
[ 5] 2.00-3.00 sec 103 KBytes 847 Kbits/sec 3 26.2 KBytes
[ 5] 3.00-4.00 sec 172 KBytes 1.41 Mbits/sec 1 29.0 KBytes
[ 5] 4.00-5.00 sec 127 KBytes 1.04 Mbits/sec 0 31.7 KBytes
[ 5] 5.00-6.00 sec 138 KBytes 1.13 Mbits/sec 0 35.9 KBytes
[ 5] 6.00-7.00 sec 222 KBytes 1.82 Mbits/sec 0 44.1 KBytes
[ 5] 7.00-8.00 sec 101 KBytes 824 Kbits/sec 1 1.38 KBytes
[ 5] 8.00-9.00 sec 171 KBytes 1.40 Mbits/sec 28 27.6 KBytes
[ 5] 9.00-10.00 sec 127 KBytes 1.04 Mbits/sec 1 31.7 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Retr
[ 5] 0.00-10.00 sec 1.35 MBytes 1.13 Mbits/sec 37 sender
[ 5] 0.00-10.24 sec 1.23 MBytes 1.01 Mbits/sec receiver

iperf Done.

还有很多好玩的选项,组合使用很强大。

0%