Skip to content

Instantly share code, notes, and snippets.

@Shaunwei
Shaunwei / G_pack_unpack_strings.py
Last active January 13, 2016 08:07
Pack and Unpack string
class Packer:
delimiter = '#|#'
@staticmethod
def pack(string_list):
packed = ''
for s in string_list:
mlist = list(s)
for i, char in enumerate(mlist):
if char == '#':
@Shaunwei
Shaunwei / gistLoader.js
Created January 12, 2016 22:06
Embed Gist in Blogpost
<script src="https://cdn.rawgit.com/moski/gist-Blogger/master/public/gistLoader.js" type="text/javascript"></script>
@Shaunwei
Shaunwei / git-cards.js
Created January 13, 2016 07:46
Add github widget
<div class="github-card" data-user="UserID"></div>
<script src="//cdn.jsdelivr.net/github-cards/latest/widget.js"></script>
@Shaunwei
Shaunwei / git-widgets.js
Last active January 13, 2016 08:10
Add gist widget to blogspot, changed git line to cdn.rawgit for blogspot. Detailed usage and optionals in https://github.com/gilliek/bootstrap-github-widget
<div class="github-widget pull-left"
data-toggle="github-widget"
data-user="UserId"
data-widget="gists"
data-body="auto"
data-footer="auto"
data-limit="4">
</div>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css" />
@Shaunwei
Shaunwei / tree_serialization.py
Last active December 17, 2017 17:10
Solution0 used post order traversal. Solution1 used pre order traversal and tricks to use python closure. Solution2 used bfs. Solution0 is recommended to use in on this problem.
"""
Design an algorithm and write code to serialize and deserialize a binary tree.
Writing the tree to a file is called 'serialization'
and reading back from the file to reconstruct
the exact same binary tree is 'deserialization'.
There is no limit of how you deserialize or serialize a binary tree,
you only need to make sure you can serialize a binary tree to a string
and deserialize this string to the original structure.
Have you met this question in a real interview? Yes
Example
@Shaunwei
Shaunwei / failure_chars_detection.py
Last active January 15, 2016 00:12
File encoding issue with chardet library. A few characters '{', '}', '~' with several combinations will cause chardet to fail. Find out who are the bad guys!! For fancy looks, I used colorclass and terminaltables.
#!/usr/bin/env python3
import chardet
from colorclass import Color
from terminaltables import AsciiTable
def generate_test(chars_list):
table_data = [['Chars', 'Confidence', 'Encoding', 'Issue'], ]
for chars in chars_list:
result = chardet.detect(chars.encode('utf-8'))
@Shaunwei
Shaunwei / search_for_a_range.py
Created January 19, 2016 06:10
Algorithm Post: Binary Search - Search for a range
class BinarySearch:
@staticmethod
def find_first_position(arr, target):
st, ed = 0, len(arr) - 1
while st + 1 < ed:
mid = (st + ed) / 2
if arr[mid] == target:
ed = mid
elif arr[mid] < target:
st = mid
@Shaunwei
Shaunwei / lowest_common_ancestor.py
Created January 19, 2016 07:13
Algorithm Post: Divide and Conquer - Lowest Common Ancestor
class TreeNode:
def __init__(self, val):
self.val = val
self.left = self.right = None
def __str__(self):
return '[' + str(self.val) + ']'
class LCA:
def find_lca(self, root, A, B):
if not root:
@Shaunwei
Shaunwei / mergesort_quicksort.py
Created January 19, 2016 08:20
Algorithm Post: Divide and Conquer - Merge Sort and Quick Sort
class QuickSort:
def sort(self, arr):
self.qsort(arr, 0, len(arr) - 1)
return arr
def qsort(self, arr, st, ed):
if st >= ed:
return
index = self.partition(arr, st, ed)
@Shaunwei
Shaunwei / general_trees2binary_trees.py
Created January 20, 2016 07:26
Encoding general trees as binary trees. There is a one-to-one mapping between general ordered trees and binary trees, which in particular is used by Lisp to represent general ordered trees as binary trees.
class TreeNode:
def __init__(self, label):
self.label = label
self.childrens = []
self.sibling = None
def __str__(self):
return 'Tree[' + str(self.label) + ']'
class BinaryTreeNode: