ARTS-week26

Algorithms

Number of 1 Bits

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while (n != 0) {
n &= n - 1;
++res;
}
return res;
}
};

House Robber

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int rob(vector<int> &nums) {
int n = nums.size();
int rob = 0, norob = 0;
for (int i = 0; i < n; ++i) {
int tmp = norob;
norob = max(rob, norob);
rob = tmp + nums[i];
}
return max(rob, norob);
}
};

Review

本周阅读英文文章 A Phishing Guide: Lessons Learned on the Journey to Detecting Phishing Domains

Technique

本周对一批数据进行了初步处理,就只举个栗子,过程和实际操作基本一致,先看一下tmp目录的结构,存在子目录

1
2
3
4
5
6
7
8
9
10
11
top@top:~/tmp$ tree
.
├── a.txt
├── b.txt
├── c.txt
├── d.bin
└── test
├── e.txt
└── f.txt

1 directory, 6 files

接下来需要将tmp目录中的所有普通文件拷贝到res中

1
$ find tmp/ -type f -maxdepth 10 -exec cp {} res \;

也可以通过如下操作只拷贝txt文件到res中

1
2
3
$ find tmp/ -maxdepth 20 -name "*.txt" -exec cp {} res/ \;

$ find tmp/ -maxdepth 20 -name "*.txt" |xargs -i cp {} res/

xargs命令从stdin处读取一系列参数,然后使用这些参数来执行指定命令。它能将单行或多行输入文本转换成其他格式,例如单行变多行或是多行变单行。xargs命令可以同find命令很好地结合在一起。find的输出可以通过管道传给xargs,由后者执行-exec选项所无法处理的复杂操作。如果文件系统的有些文件名中包含空格,find命令的-print0选项可以使用0(NULL)来分隔查找到的元素,然后再用xargs对应的-0选项进行解析。 ——《Linux Shell脚本攻略(第3版)》

然后我们查看res中的文件

1
2
3
4
5
6
7
8
9
10
top@top:~/res$ tree
.
├── a.txt
├── b.txt
├── c.txt
├── d.bin
├── e.txt
└── f.txt

0 directories, 6 files

接下我们给文件重新命名为数字编号的形式

1
$ i=1; for x in *; do mv $x $i.txt; let i=i+1; done

再次查看res中的文件

1
2
3
4
5
6
7
8
9
10
top@top:~/res$ tree
.
├── 1.txt
├── 2.txt
├── 3.txt
├── 4.txt
├── 5.txt
└── 6.txt

0 directories, 6 files

如果遇到文件名称中有空格的情况下,可以将空格替换为下划线后再进行处理

1
2
3
4
CentOS:
$ find tmp/ -depth -name "* *" -execdir rename " " "_" "{}" \;
Ubuntu:
$ find tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

比如tmp文件夹内存在多级子目录,我们只想打印tmp文件夹中的文件名称时,可以先导出全路径的filename.txt文件,之后可以通过如下方式获取只文件名称

1
2
3
4
$ for line in `cat filename.txt`
> do
> echo ${line##*/}
> done | sort -n > line.txt

Share

关于C11中TR24731的讨论:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1106.txt
https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions

一个safec libc extension with all C11 Annex K functions实现:https://github.com/rurban/safeclib

0%