Skip to content

Instantly share code, notes, and snippets.

@gooooloo
gooooloo / utf8_to_unicode.py
Created Nov 9, 2020
convert hex utf-8 bytes to hex Unicode number
View utf8_to_unicode.py
import sys
def ch2int(ch):
if '0' <= ch <= '9':
return ord(ch) - ord('0')
if 'a' <= ch <= 'f':
return ord(ch) - ord('a') + 10
if 'A' <= ch <= 'F':
return ord(ch) - ord('A') + 10
@gooooloo
gooooloo / visual_studio_projects_dependency.py
Last active Jul 27, 2020
In some of visual studio solution I met, there are very much more projects inside. This is a simple script to extract and simplify the dependencies and generate VBA of Visio to draw the diagram better visualization.
View visual_studio_projects_dependency.py
import re
import os
import sys
def vba_vso_name(text):
ans = text.replace(' ', '_').replace('.', '_')
assert re.compile(r'^\w*$').match(ans)
return ans
def generate_VBA(d_dir_dep):
@gooooloo
gooooloo / simple_tklb.py
Last active Jul 14, 2020
my simple tkinter Listbox GUI
View simple_tklb.py
def simple_tklb(fn_get_list, fn_double_click_item):
from tkinter import Tk, Listbox
tk = Tk()
lb = Listbox(tk)
for idx, item in enumerate(fn_get_list()):
lb.insert(idx, item)
def onselect(event):
View var_arg_func_ptr_demo.cpp
#include <stdio.h>
#include <stdarg.h>
typedef void (*f_type)(int, ...);
void foo1(int n1) { printf("%d\n", n1); }
void foo2(int n1, int n2) { printf("%d %d\n", n1, n2); }
void foo3(int n1, int n2, int n3) { printf("%d %d %d\n", n1, n2, n3); }
void bar(f_type pf, ...) {
if ((void *)pf == (void *)foo1) {
@gooooloo
gooooloo / memleak.cpp
Last active Jul 24, 2019
a memory leak example in practice
View memleak.cpp
/**
* Here is an example of mem leak I met today. I didn't write the codes, I just help finding the memleak.
* Of course the real code is much more complicated, I simplify it here.
*
* To prove there is memleak, just compile and run:
* $ gcc a.cpp --std=c++11 -lstdc++ && ./a.out
*
* And you will see below:
* 4 bytes leaked.
*
@gooooloo
gooooloo / 2018年刑侦科目推理试题.py
Last active Mar 2, 2018
2018年刑侦科目推理试题
View 2018年刑侦科目推理试题.py
# http://news.sina.com.cn/s/wh/2018-03-02/doc-ifyrztfz6281140.shtml
def main():
ans = [None for _ in range(10+1)]
def least_answered(ans):
vs = ans[1:]
vc = {v:vs.count(v) for v in vs}
minc = min(vc.values())
for v,c in vc.items():
View 打印螺旋矩阵3.py
def main(n):
delta = [(1,0), (0,1), (-1,0), (0,-1)]
arr = [[0 for _ in range(n)] for _ in range(n)]
row,col,k,di = -1,0,1,0
while k <= n*n:
row += delta[di][0]
col += delta[di][1]
if not 0 <= row < n or not 0 <= col < n or arr[row][col] > 0:
row -= delta[di][0]
col -= delta[di][1]
@gooooloo
gooooloo / remove_nodes_with_val_from_linkedlist.py
Created Oct 26, 2017
从链表里删除指定内容的节点
View remove_nodes_with_val_from_linkedlist.py
class Node:
def __init__(self, v, n=None):
self.v = v
self.n = n
def do_print(head: Node):
t = head
print('=====')
while t is not None:
print(t.v)
@gooooloo
gooooloo / palindromic_number.py
Created Oct 26, 2017
判断一个整数是不是回文数
View palindromic_number.py
def main(a):
a = abs(a)
b = a
c = 0
while b > 0:
c = c * 10 + b % 10
b //= 10
print(c == a)
if __name__ == '__main__':
@gooooloo
gooooloo / 距离问题模拟.py
Created May 5, 2017
模拟这个题目的过程
View 距离问题模拟.py
# 模拟这个题目的过程
# https://mp.weixin.qq.com/s?__biz=MzIzMTc0NzAwMA==&mid=2247483759&idx=1&sn=933d3522dcc197b547696ad522652b8b&key=281224a0b07438ec8edb2e3a289a23f072754ac6a4b26d9e3ce3e38cca13ec9be38b36c21c8340b80c94deb264a2ff916029a398d55725d4128d04bbbef6653083d77490729372eca7851e6b4673eac2&ascene=0&uin=MTQ0OTk4MDMwMA%3D%3D&devicetype=iMac+MacBookPro11%2C1+OSX+OSX+10.12.4+build(16E195)&version=12020510&nettype=WIFI&fontScale=100&pass_ticket=1TrgoYI6A%2Fe9EM1BEXmWAwjoNDPwfCcCsU5W1LgXMf%2BV1wgkhzYikLhX8b3Ru9op
import numpy as np
import matplotlib.pyplot as plt
def f0(xarr, a, c, d):
l = []
cyclelen = d + 1.0 * c / a
for x in xarr:
You can’t perform that action at this time.