ARTS-week01

Algorithms

第一周算法题:leetcode01

1
2
3
4
5
6
7
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

思路:最开始想到的是暴力穷举,复杂度会高一点,仔细想了一下,可以使用哈希表记录每个元素对应的下标,然后查找target - a[i]。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> summap;
int n = nums.size();
int i;
vector<int> res(2);
for (i = 0; i < n; ++i) {
if (summap.find(target - nums[i]) != summap.end()) {
res[0] = summap[target - nums[i]];
res[1] = i;
break;
} else {
summap[nums[i]] = i;
}
}
summap.clear();
return res;
}
};

Review

本周阅读英文文章:Making the Leap from Speech to Dialogue: The Challenge for Human to Machine Communication

spoken dialogue systems (SDS)语音对话系统

Right now these spoken dialogue systems (SDS) tends to be limited to a “command-based” approach, which can be seen with a number of recently introduced commercial implementations, like Apple’s Siri for the iOS, Amazon’s Echo/Alexa, and the social robot Jibo.

SDS最新发展由几种不同但相关的技术创建组成:
automatic speech recognition (ASR)自动语音识别
dialogue management (DM) 对话管理
text-to-speech (TTS) synthesis 文本到语音合成

Technique

OS:ubuntu16.04

生成一个密钥:

1
openssl genrsa -out test.key 1024

-out指定生成文件的。需要注意的是这个文件包含了公钥和密钥两部分,也就是说这个文件即可用来加密也可以用来解密。1024是生成密钥的长度。

从test.key中提取公钥:

1
openssl rsa -in test.key -pubout -out test_pub.key

-in指定输入文件
-out指定提取生成公钥的文件名

至此,我们手上就有了一个公钥,一个私钥(包含公钥)。现在可以将用公钥来加密文件了。

创建hello.txt文件,查看内容

1
2
3
$ hexdump -Cv hello.txt
00000000 31 32 33 34 35 36 0a |123456.|
00000007

利用此前生成公钥加密文件:

1
openssl rsautl -encrypt -in hello.txt -inkey test_pub.key -pubin -out hello.en

-in指定要加密的文件
-inkey指定密钥
-pubin表明是用纯公钥文件加密
-out为加密后的文件

查看加密文件内容:

1
2
3
4
5
6
7
8
9
10
$ hexdump -Cv hello.en
00000000 2a 03 1c 82 ca 7b d5 f5 14 4c 5a 3b 9f e3 31 2a |*....{...LZ;..1*|
00000010 62 f8 61 09 bc 54 79 72 07 55 9d 7e de a0 03 ab |b.a..Tyr.U.~....|
00000020 fe 37 cf 9a 1a c9 41 e5 6a df af 99 bf 3a 82 3f |.7....A.j....:.?|
00000030 e3 08 ae 85 1c e9 d7 4b da 5c b4 ff 73 7c b9 69 |.......K.\..s|.i|
00000040 6a 81 02 e4 ef 88 6d 6a f1 ac 46 19 c3 11 1d 5f |j.....mj..F...._|
00000050 fd d7 7a 3b c1 00 c2 12 6c e8 be 0e c1 17 cb 3d |..z;....l......=|
00000060 ef 9c 31 73 ea ba 90 63 57 88 64 2f a1 49 90 e0 |..1s...cW.d/.I..|
00000070 c1 67 88 45 15 1c a5 8c b3 cd 3c 04 a2 12 41 27 |.g.E......<...A'|
00000080

解密文件:

1
openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de

-in指定被加密的文件
-inkey指定私钥文件
-out为解密后的文件

查看解密文件内容:

1
2
3
$ hexdump -Cv hello.de
00000000 31 32 33 34 35 36 0a |123456.|
00000007

关于hexdump:

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
可用参数
[-bcCdovx] [-e format_string] [-f format_file] [-n length] [-s skip] file ...

参数含义
-b 单字节八进制显示,十六进制显示偏移量,每行显示16个字符,每字符用三位显示,不足补零,列间以空格分隔
-c 单字节字符显示,十六进制显示偏移量,每行显示16个字符,每字符三位显示,不足补空格,列间以空格分隔
-C 标准十六进制+ascii码显示,十六进制显示偏移量,每行16个字符,每字符两位显示,不足补0,结尾显示当前16位数据的ascii码值,以|框住
-d 双字节十进制显示,十六进制显示偏移量,每行8组(16字节)每组5位,不足补零,列间以空格分隔,以无符号10进制数值显示
-e 指定格式字符串,格式字符串包含在一对单引号中,格式字符串形如:'a/b "format1" "format2"'
-f 根据format file中的格式进行输出,忽略formatfile中空行及以#开始的行会
-n length 只显示length个字节的数据
-o 双字节八进制显示。十六进制显示偏移量,每行8组数据,每数据占两字节,6列,不足补零,以空格分隔
-s offset 跳过从开始的offset个字节,默认输入十进制,以0x或0X开始按16进制处理,否则如以0开始按八进制处理,如果以b/k/m结尾,则原数值乘以512/1024/1048576
-v 显示所有数据,如果不包含这一选项,对于同上一行完全相同的数据,hexdump会以*代替显示
-x 两位十六进制显示.十六进制显示偏移量,每行8组数据,每数据占两字节,4列,不足补零,以空格分隔

每个格式字符串由三部分组成,每个由空格分隔,第一个形如a/b,b表示对每b个输入字节应用format1格式,a表示对每a个输入字节应用format2格式,一般a>b,且b只能为1,2,4,另外a可以省略,省略则a=1。format1和format2中可以使用类似printf的格式字符串,如:
%02d:两位十进制
%03x:三位十六进制
%02o:两位八进制
%c:单个字符等

还有一些特殊的用法:
%_ad:标记下一个输出字节的序号,用十进制表示
%_ax:标记下一个输出字节的序号,用十六进制表示
%_ao:标记下一个输出字节的序号,用八进制表示
%_p:对不能以常规字符显示的用.代替
同一行如果要显示多个格式字符串,则可以跟多个-e选项

Share

《PythonCookBook》解压序列赋值给多个变量
任何的序列或者是可迭代对象可以通过一个简单的赋值语句解压并赋值给多个变量。 唯一的前提就是变量的数量必须跟序列元素的数量是一样的。如果变量个数和序列元素的个数不匹配,会产生一个异常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]
>>> name, shares, price, date = data
>>> name
'ACME'
>>> date
(2012, 12, 21)
>>> name, shares, price, (year, mon, day) = data
>>> name
'ACME'
>>> year
2012
>>> mon
12
>>> day
21
>>>

只想解压一部分,丢弃其他的值。对于这种情况Python并没有提供特殊的语法。但是可以使用任意变量名去占位,到时候丢掉这些变量就行了。

1
2
3
4
5
6
7
>>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]
>>> _, shares, price, _ = data
>>> shares
50
>>> price
91.1
>>>
0%