ARTS第五周

Algorithm

Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

Example 1:

1
2
Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

1
2
3
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

1
All given inputs are in lowercase letters a-z.

answer
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
res = ""
if not strs: return res
min_len = min([len(s) for s in strs])
for i in range(min_len):
temp = set([s[i] for s in strs])
if len(temp) == 1:
res+=strs[0][i]
else:
break
return res

Rewiew

https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/

文章介绍了平时开发过程中使用Python记录日志好的实践方法。其中使用json或yaml格式设置日志配置值得借鉴,目前已在项目中使用该方法。

Tip

xargs

linux系统中查找某个目录下(如/test)包含特定字符串(如test)的文件,可用命令有如下两种:

1
grep -ri "test" -l /test

1
find /test | xargs grep -ri "test" -l

xargs能够处理管道或者stdin并将其转换成特定命令的命令参数。

Share

https://docs.python-guide.org/writing/tests/
https://hackernoon.com/untold-stories-about-python-unit-tests-a141501f0ee

上述两篇文章介绍了python编写单元测试的建议,可以指导自己开发过程中编写单元测试。