zzp's blog


  • 首页

  • 分类

  • 归档

  • 搜索

search-insert-position

发表于 2019-05-30 | 分类于 algorithm , leetcode | 阅读次数
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
"""https://leetcode.com/problems/search-insert-position/submissions/"""

class Solution:
def searchInsert(self, nums, target):
if target < nums[0]:
return 0
pos = 0
for num in nums:
if target > num:
pos += 1
if target <= num:
return pos
return len(nums)


if __name__ == '__main__':
s = Solution()
nums, target = [1,3,5,6], 5
assert s.searchInsert(nums, target) == 2
nums, target = [1,3,5,6], 2
assert s.searchInsert(nums, target) == 1
nums, target = [1,3,5,6], 7
assert s.searchInsert(nums, target) == 4
nums, target = [1,3,5,6], 0
assert s.searchInsert(nums, target) == 0

valid-palindrome

发表于 2019-05-30 | 分类于 algorithm , leetcode | 阅读次数
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
"""
判断字符串是否回文
https://leetcode.com/problems/valid-palindrome/
"""
class Solution:
def isPalindrome(self, s: str) -> bool:
if len(s) <= 1:
return True
start = 0
end = len(s) - 1
s = s.lower()
while start < end:
if not s[start].isalnum():
start += 1
elif not s[end].isalnum():
end -= 1
else:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True


if __name__ == '__main__':
s = Solution()
assert s.isPalindrome("A man, a plan, a canal: Panama") == True
assert s.isPalindrome("race a car") == False

windows-pymol

发表于 2019-05-29 | 分类于 python | 阅读次数

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pymol

pip install Pmw-2.0.1-py3-none-any.whl
pip install pymol-2.4.0a0-cp37-cp37m-win32.whl
pip install pymol_launcher-2.1-cp37-cp37m-win32.whl

C:\Users\zzp\AppData\Local\Programs\Python\Python37-32\Scripts\pymol.exe

draw-io-export

发表于 2019-05-28 | 分类于 trick | 阅读次数

draw.io做好图后,crop 导出为pdf格式,然后用linux下命令gs, convert[负责压缩]转化为tif格式

pdb2tif

1
2
3
4
5
6
7
8
#!/bin/sh

for i in $@; do
BN=$(basename $i .pdf)
gs -q -r300x300 -dNOPAUSE -sDEVICE=tiff24nc -sOutputFile=$BN.tif $i -c quit
convert $BN.tif -compress lzw $BN.tif
# convert $BN.tif $BN.png # 也可以生成新的格式,如png
done

pip

发表于 2019-05-21 | 分类于 python | 阅读次数

pypi 镜像使用帮助

临时使用

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

设为默认

升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

1
2
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

如果您到 pip 默认源的网络连接较差,临时使用本镜像站来升级 pip:

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

服务器上同步/备份大量文件的处理方式

发表于 2019-05-15 | 分类于 linux | 阅读次数

parsyncfp

hjmangalam/parsyncfp: follow-on to parsync (parallel rsync) with better startup perf

1
./parsyncfp --NP=4 --CS=100K --nowait --rsyncopts="-uv" --startdir="/home/zzp" "test" "/home/zzp/program/"

rsync + parallel

将

1
2
3
4
5
6
7
8
9
10
11
12


并行增量备份的另一种实现(rsync+parallel)

```bash
#!/bin/bash

dst="/mnt/500G移动硬盘/results/"
filename="backuplist"
ncpu=8

cat $filename | parallel -j $ncpu "rsync -urv {} $dst"

sys-settrace

发表于 2019-04-26 | 分类于 python | 阅读次数

sys.settrace 为built-in函数

底层实现代码

sys.settrace 调用关系

  1. https://github.com/python/cpython/blob/master/Python/sysmodule.c
  2. https://github.com/python/cpython/blob/master/Python/ceval.c

PyFrameObject 实现

  1. https://github.com/python/cpython/blob/master/Include/frameobject.h
  2. https://github.com/python/cpython/blob/master/Objects/frameobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import sys


def trace_func(frame, event, arg):
value = frame.f_locals["a"]
if value % 2 == 0:
value += 1
frame.f_locals["a"] = value


def f(a):
print(a)


if __name__ == "__main__":
trace = sys.gettrace()
sys.settrace(trace_func)
for i in range(0, 5):
f(i)
sys.settrace(trace)

pysnooper.debug

发表于 2019-04-26 | 分类于 python | 阅读次数

输出日志文件名称

1
@pysnooper.snoop("test.log")

定义debug的函数调用层级

1
@pysnooper.snoop("test.log", depth=2)

添加非局部变量的修改记录

1
@pysnooper.snoop("test.log", variables=('K', 'P'))
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
import pysnooper


K = 0


def f(i):
global K
K += 1
j = i ** 2
return j


def g(i):
global K
K += 1
j = i + f(i)
return j


#@pysnooper.snoop("test.main.log", depth=1, variables=('K', ))
@pysnooper.snoop("test.main.log", depth=1)
def main():
for i in range(4):
print(g(i))


if __name__ == '__main__':
main()

install-htop-ncurses-locally

发表于 2019-04-22 | 分类于 linux | 阅读次数

install ncurses

1
2
3
tar zxf ncurses-6.1.tar.gz
./configure --prefix=/home/zzp/.local
make && make install

install htop

1
2
tar zxf htop.tar.gz
./configure --prefix=/home/zzp/.local CFLAGS=-I/home/zzp/.local/include LDFLAGS=-L/home/zzp/.local/lib

python-SMS-notify

发表于 2019-01-09 | 分类于 python | 阅读次数
1
pip3 install twilio

https://www.twilio.com/

1
2
3
4
5
6
7
8
9
from twilio.rest import Client


def send_message(message):
account = "ACXXXXXXXXXXXXX"
token = "XXXXXXXXXXXXXXXXX"
client = Client(account, token)
message = client.messages.create(
to="+86XXXXXXXX", from_="+XXXXXXXXX", body=message)
123…9
ningning

ningning

86 日志
9 分类
GitHub
© 2018 - 2020 ningning
本站访客数 人次