0%

在 Linux 系统中,很多服务都要调用域名解析服务,一般通过/etc/resolv.conf来指定 DNS 服务器的 ip。每次外部访问都会首先访问 DNS 服务器,这样会导致延时增加。如果网络不好的情况下,还会遇到域名解析失败的情况。在这种情况下,需要通过配置 DNS 解析缓存服务,从而优化 DNS 响应速度,减少外部网络依赖。

NSCD安装

NSCD(Name Service Caching Daemon,名称服务缓存进程)不需要对应用程序和 DNS 配置文件做任何修改,对系统的部署影响最小。因此,NSCD 成为 Linux 下广泛使用的域名缓存软件。

CentOS 下安装

1
yum -y install nscd

Ubuntu 下安装同样简单

1
apt-get install nscd

NSCD的配置文件是/etc/nscd.conf,核心配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#是否启用缓存
enable-cache hosts yes
#解析成功的结果缓存时间
positive-time-to-live hosts 3600
#解析失败的结果缓存时间
negative-time-to-live hosts 20
#缓存条目数量
suggested-size hosts 211
#是否检查hosts文件变化
check-files hosts yes
#是否在重启时保留之前缓存条目
persistent hosts yes
#是否允许客户端直接查询NSCD的内存镜像以获得结果
shared hosts yes
#DNS的缓存大小

焙新酒,点孤灯。

对花语,问东风。

蜂蝶寐,月半弓。

云遮影,树挽藤。

举杯对,我与卿。

醉姮娥,话三更。

语未断,杯莫停。

天下事,谈笑中。

酌复酌,语不清。

灯花落,听鼾声。

你在 git 项目中见过 .gitkeep 文件吗? 你应该遇到过。如果你通过 angular Cli 安装过最新的 angular2 或者 angular4 项目,你一定会发现 ./src/app/assets 文件夹下有一个 .gitkeep 文件。你是不是很好奇这个文件是干嘛用的?我们都知道 .gitignore,那么你是不是觉得这个文件是 .gitignore 的兄弟? Git 对这个神奇文件做了什么特别的事情吗?

什么是 .gitkeep?

在这之前,我们必须清楚 Git 不跟踪空文件夹 。如果你的项目文件夹里边有任何的空文件夹,Git 都会忽略掉。但如果你在文件夹里边添加了一个文件,Git 就会开始跟踪这个文件夹。 无论这个文件是什么,内容如何,名字是什么。

所以什么是 .gitkeep 呢?实际上,这个名字是社区起的,其他人可以容易的把它和 .gitignore 联系起来。Git 并没有给这个文件任何的像 .gitignore 文件一样的特殊权限。

如果你想跟踪你上游项目的空文件夹,那么你就在这个文件夹中创建一个 .gitkeep 文件。其他开发者很容易就会理解这是干什么用的。通常来说,assets 文件夹和 log 文件夹需要这样做。记住,不要把 .gitkeep 添加到 .gitignore 文件中,那样的话空文件夹就从你的文件系统中消失了。

一小部分人通过在空文件夹中添加 .gitigonre 文件来保持跟踪,但是这一举措经常会让其他人感到困惑,毕竟 .gitignore 是用来让你的版本控制系统忽略文件用的。不过这也是另一种保持跟踪空文件夹的方法。

原文链接:What is .gitkeep? Differences between .gitignore and .gitkeep

问题

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

翻译

给出一个罗马数字,将其转化为整数。 输入被限定在在1到3999之间。

解决方法

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
func romanToInt(s string) int {
var result int
for len(s) > 0 {
if len(s) >= 2 {
curr := findRoman(s[:2])
if curr != 0 {
result += curr
s = s[2:]
continue
}

}
result += findRoman(s[:1])
s = s[1:]
}
return result
}

func findRoman(s string) int {
switch s {
case "I":
return 1
case "IV":
return 4
case "V":
return 5
case "IX":
return 9
case "X":
return 10
case "XL":
return 40
case "L":
return 50
case "XC":
return 90
case "C":
return 100
case "CD":
return 400
case "D":
return 500
case "CM":
return 900
case "M":
return 1000
default:
return 0
}
}

问题

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer _n_, generate the _n_th sequence.

Note: The sequence of integers will be represented as a string.

翻译

数数问题是像如下顺序的整数:

1, 11, 21, 1211, 111221, ...

1被读作 "1个1" 或者 11

11被读作2个1或者21

21被读作1个2,然后1个1或者1211

给出整数 n_, 求出第 _n 个结果。

进一步解释:

1念作11, 11念作21, 21念作1211

求出第 _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
func countAndSay(n int) string {
if n == 1 {
return "1"
}
return getResult(countAndSay(n - 1))
}

func getResult(nStr string) string {
var curr rune
currCount := 1
var s bytes.Buffer
for _, v := range nStr {
if curr == v {
currCount++
} else {
if curr != 0 {
s.WriteString(strconv.Itoa(currCount))
s.WriteRune(curr)
}
curr = v
currCount = 1
}
}
s.WriteString(strconv.Itoa(currCount))
s.WriteRune(curr)
return s.String()
}

问题

The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|
| 3 | 3 | 12 | 6 | completed |2013-10-01|
| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|
| 5 | 1 | 10 | 1 | completed |2013-10-02|
| 6 | 2 | 11 | 6 | completed |2013-10-02|
| 7 | 3 | 12 | 6 | completed |2013-10-02|
| 8 | 2 | 12 | 12 | completed |2013-10-03|
| 9 | 3 | 10 | 12 | completed |2013-10-03|
| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+

The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).

1
2
3
4
5
6
7
8
9
10
11
12
+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+

Write a SQL query to find the cancellation rate of requests made by unbanned clients between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.

1
2
3
4
5
6
7
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+

翻译

Trips 表中包含了所有的出租车行程。每个行程都有一个唯一的 Id,Client_Id 字段和 Driver_Id 字段都是 Users 表的外键。Status 字段的类型是枚举,包含 ‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’ 四种情况。 Users 表包含了所有用户。每个用户都有唯一的 Users_IdRole 字段的类型是枚举, 包含 ‘client’, ‘driver’, ‘partner’ 三种情况。 写出能够找出10月1号到3号间未冻结账户订单取消率的 sql 语句。取消率保留两位小数。

解决方法

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
SELECT
A.Request_at AS 'Day',
Round(
IFNULL(B.canceled, 0) / A.total,
2
) AS 'Cancellation Rate'
FROM
(
SELECT
count(Request_at) AS total,
Request_at
FROM
Trips,
Users
WHERE
Trips.client_id = Users.Users_id
AND Users.Banned != "Yes"
AND Trips.Request_at >= "2013-10-01"
AND Trips.Request_at <= "2013-10-03"
GROUP BY
Request_at
) A
LEFT JOIN (
SELECT
count(Request_at) AS canceled,
Request_at
FROM
Trips,
Users
WHERE
Trips.Client_id = Users.Users_id
AND Users.Banned != "Yes"
AND Trips.Request_at >= "2013-10-01"
AND Trips.Request_at <= "2013-10-03"
AND (
Trips.`Status` = 'cancelled_by_client'
OR Trips.`Status` = 'cancelled_by_driver'
)
GROUP BY
Request_at
) B ON A.Request_at = B.Request_at

一落又一落,

几堕复几堕?

无极向下去,

天下不知我。

slice 类型的 ip 地址

1
type IPAddr [4]byte

字符串型的

1
2
"8.8.8.8"
"192.168.1.1"

将第一种转换为第二种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//IPAddr 
type IPAddr [4]byte

func (ip IPAddr) String() string {
s := make([]string, len(ip))
for i := range ip {
s[i] = strconv.Itoa(int(ip[i]))
}
return strings.Join(s, ".")
}

func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
···

ssh登录的设置

生成 ssh 公钥和私钥

1
ssh-keygen -t rsa -C "you@email.com"

输入文件名,完成创建。

更改 ssh config

打开 ~/.ssh/config,添加如下的文件

1
2
3
4
5
Host your_host
HostName example.com
RSAAuthentication yes
IdentityFile ~/.ssh/your_rsa_file
User git

更改服务器的ssh设置

将你的公钥文件(后缀名为.pub)导入到服务器中(服务器会将其添加到 authorized_keys 文件中)

切换远程仓库

首先使用 git remote -v 找到自己原来的远程仓库,例如 github.com/phpcyy/first.git。 在自己的目录中输入以下命令完成换源

1
git remote set-url origin your_host:/phpcyy/first.git

从此之后使用 git 命令再也不用输入密码了

问题:

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

Employee 表中含有所有的雇员。每个雇员有自己的 id 和 department id。)

+—-+——-+——–+————–+
| Id | Name | Salary | DepartmentId |
+—-+——-+——–+————–+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+—-+——-+——–+————–+

The Department table holds all departments of the company.

Department 表中含有公司的所有部门。)

+—-+———-+
| Id | Name |
+—-+———-+
| 1 | IT |
| 2 | Sales |
+—-+———-+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

(写出能够找到每个部门薪水前三名员工的 sql 语句。根据上边的表,你的结果应该包含以下列。)

+————+———-+——–+
| Department | Employee | Salary |
+————+———-+——–+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+————+———-+——–+

我的解决方法

SELECT
t2. NAME AS Department,
t1. NAME AS Employee,
t1.Salary AS Salary
FROM
Employee t1,
Department t2
WHERE
t1.DepartmentId = t2.Id
AND (
SELECT
count(distinct(Salary))
FROM
Employee t3
WHERE
t3.DepartmentId = t1.DepartmentId
AND t3.Salary > t1.Salary
) < 3
ORDER BY
Department,
Salary DESC