Skip to content

Instantly share code, notes, and snippets.

View eric-zhu's full-sized avatar

Eric (Ergang) Zhu eric-zhu

  • Apkudo
  • Sydney, Australia
View GitHub Profile
@eric-zhu
eric-zhu / iview_download.py
Last active March 26, 2020 00:49
A script to show all downloadable video links for iView
import os
import sys
import re
import requests
def main(args):
if args:
program = args[0]
else:
program = "hey-duggee"
@eric-zhu
eric-zhu / scrapper.py
Created July 27, 2017 12:34 — forked from madjar/scrapper.py
A example of scrapper using asyncio and aiohttp
import asyncio
import aiohttp
import bs4
import tqdm
@asyncio.coroutine
def get(*args, **kwargs):
response = yield from aiohttp.request('GET', *args, **kwargs)
return (yield from response.read_and_close(decode=True))
@eric-zhu
eric-zhu / coroutines_example.py
Created July 27, 2017 12:33 — forked from kunev/coroutines_example.py
Coroutines example with python's asyncio module
import asyncio
@asyncio.coroutine
def open_file(name):
print("opening {}".format(name))
return open(name)
@asyncio.coroutine
def close_file(file):
print("closing {}".format(file.name))
@eric-zhu
eric-zhu / dummy-web-server.py
Created July 27, 2017 12:31 — forked from bradmontgomery/dummy-web-server.py
a minimal http server in python. Responds to GET, HEAD, POST requests, but will fail on anything else.
#!/usr/bin/env python
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
@eric-zhu
eric-zhu / vimrc
Created January 12, 2015 03:04
My vimrc in use
" Sample .vimrc file by Martin Brochhaus
" Presented at PyCon APAC 2012
" ============================================
" Note to myself:
" DO NOT USE <C-z> FOR SAVING WHEN PRESENTING!
" ============================================

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
@eric-zhu
eric-zhu / revers_polish_notation
Last active December 29, 2015 17:09
Evaluate Reverse Polish Notation. Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
public class Solution {
public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0) return 0;
Stack<String> stack = new Stack<String>();
for (int i = 0; i < tokens.length; ++i) {
String str = tokens[i];
if (str.equals("+")) {
String operand2 = stack.pop();
@eric-zhu
eric-zhu / word_ladder
Last active December 28, 2015 20:39
Word Ladder. Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start = "hit" end = "cog" dict = ["hot","dot","dog","lot","log"] As one shortest transfo…
public class Solution {
private int mMinLen;
// DFS approach.
// Time: O(mn) for for each word. (m is the length of string, n is the string numbers in dict)
// In worst case, each path might have n nodes, and there are n paths in total.
public int ladderLength(String start, String end, HashSet<String> dict) {
if (start == null || end == null || dict == null) return 0;
int dictSize = dict.size(), startLen = start.length();
int endLen = end.length();
if (dictSize == 0 || startLen == 0 || endLen == 0) return 0;
@eric-zhu
eric-zhu / valid_number
Created November 19, 2013 05:53
Valid Number. Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
public class Solution {
public boolean isNumber(String s) {
if (s == null) return false;
State state = State.Initial; // Initial state.
Type type = Type.Invalid;
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
// Get input type.
if (ch <= '9' && ch >= '0') {
@eric-zhu
eric-zhu / sort_singly_linked_list
Created November 17, 2013 23:55
Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }