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
  • 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.

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?

# 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])
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
#
# 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
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)]
@dapangmao
dapangmao / demo.md
Last active August 29, 2015 14:06
Blackjack implementation by Python
@dapangmao
dapangmao / TDD.py
Last active August 29, 2015 14:06
TDD in Python
"""Convert to and from Roman numerals"""
#-------------------------------------------------------------------------------
#
# http://woodpecker.org.cn/diveintopython/unit_testing/stage_1.html
#
#-------------------------------------------------------------------------------
import re
@dapangmao
dapangmao / _1search.md
Last active August 29, 2015 14:05
Search and others
  • Recursive method
def binary_search(A, target):
    return _binary_search(A, target, 0, len(A) - 1)
    
def _binary_search(A, target, lo, hi):
    if lo > hi:
        return -1
    mid = (lo + hi) / 2
 if A[mid] == target:
@dapangmao
dapangmao / tree.md
Last active August 29, 2015 14:05
Trees
  • The class to create tree
class TreeNode:
    def __init__(self, val):
        self.left = None
        self.right = None
        self.val = val

def to_root(input):
    a = [TreeNode(i) if i != '#' else None for i in input]