Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / huffman_compression.py
Last active January 14, 2016 02:19
String compressions - Simple compression and Huffman compression - compare with unpack/pack string problem
"""
Use serveral ways to compress string `everyday is awesome!`
1. use simple bits to replace ASCII value
2. use huffman coding
"""
def get_rate(compressed_binary, uncompressed_bits):
return len(compressed_binary) * 100 / uncompressed_bits
class SimpleCompression:
def __init__(self, string):
# helpers start
class Tree:
def __init__(self, val):
self.val = val
self.left = self.right = None
def __repr__(self):
return '[ %d ]' % self.val
def get_tree():
def print_spiral(matrix):
m, n = len(matrix), len(matrix[0])
top, down = 0, m - 1
left, right = 0, n - 1
while True:
for j in xrange(left, right + 1):
print(matrix[top][j])
top += 1
##
# Trie Solution
import collections
class Trie:
class TrieRoot:
def __init__(self):
self.children = {}
self.shortest_word = None
class TrieNode:
def __init__(self, char=''):
@Shaunwei
Shaunwei / check_duplicate_files.py
Last active December 23, 2015 06:50
Check directory duplicate files
"""
Dropbox
Question: Given root directory, search all directories and return duplicate files
input: root path
output: list[list[]]
"""
import os
import collections