Skip to content

Instantly share code, notes, and snippets.

View dapangmao's full-sized avatar
🏠
Working from home

Dapangmao dapangmao

🏠
Working from home
View GitHub Profile
@dapangmao
dapangmao / demo.md
Last active August 29, 2015 14:06
Blackjack implementation by Python
from doctest import testmod
import math
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
If the result is small enough to fit in an int, return an int.
Else return a long.
>>> [factorial(n) for n in range(6)]
#
# Implementation of the CHAR compression algorithm which corresponds to the literal "SASYZCRL".
def decompressCHAR(offset, page):
length = len(page)
resultByteArray = []
currentResultArrayIndex = 0
for currentByteIndex in range(length):
controlByte = page[offset+currentByteIndex] & 0xF0
import sqlite3
import glob
import sas7bdat
class SASexport(sas7bdat.SAS7BDAT):
def head(self, n=5):
for i, row in enumerate(self.readData()):
print row
if i == n:
break
# Test-driven development in Python for data analysis
import doctest, sys
def maxProduct(A):
""" A function to find the maximum value for a continuous subarray.
:param A: an array or list
:type A: lst
- testcase1
>>> maxProduct([0, 2, 3,-2,4, 1, -1])

One example of test-driven development in Python

Function or method is the most basic unit in Python programming. Test-driven development is a key for a developer to assure the code quality of those units. In his book, Harry Percival illustrated a few great examples about how to use TDD with Python and Django. It seems that for web development, TDD including unit testing and integration testing is the cornerstone for every success. For data analysis, coding mostly relies on built-in packages instead large framework like Django, which makes TDD easier. In my opnion, TDD in data analysis could have three steps.

  • Step 1: requirement analysis

    Before writing any code for data analysis, the programmer should seriously ask the customer or himself about the requirements.

    • What are the input parameter?
  • what if the input data doesn't fit the assumptions?

  • Python's string object is immutable. An extra space has to be used to lower alphanumeric characters and squeeze others.
    • Time O(n); Space O(n)
#-------------------------------------------------------------------------------
# Name:        Valid Palindrome
# Purpose:
#
#            Given a string, determine if it is a palindrome, considering only
#            alphanumeric characters and ignoring cases.
  • Two iterations: forward and backward
#-------------------------------------------------------------------------------
# Name:        Trapping Rain Water
# Purpose:
#
# Given n non-negative integers representing an elevation map where the
# width of each bar is 1, compute how much water it is
# able to trap after raining.
# For example,
  • Use iterations
#-------------------------------------------------------------------------------
# Name: Pascal's Triangle II
# Purpose:
#
# Given an index k, return the kth row of the Pascal's triangle.
#
# For example, given k = 3,
# Return [1,3,3,1].
  • Use yield
#-------------------------------------------------------------------------------
# Name:        Generate Parentheses
# Purpose:
#
#            Given n pairs of parentheses, write a function to
#            generate all combinations of well-formed parentheses.
#
#            For example, given n = 3, a solution set is: