Skip to content

Instantly share code, notes, and snippets.

View liang456's full-sized avatar

James liang456

  • SJTU
  • Shanghai, China
View GitHub Profile
@liang456
liang456 / Russian Peasants Algorithm.py
Created October 19, 2012 03:34
Russian Peasants Algorithm
# This is an algorithm that people actually implemented by hand before
# there were computers. Here's the Russian Peasants Algorithm in Python.
#
def russian(a, b):
x = a
y = b
z = 0
while x > 0:
@liang456
liang456 / GraphAlg.py
Created October 19, 2012 15:55
Graph Algorithms in Python
def mark_component(G, node, marked):
marked[node] = True
total_marked = 1
for neighbor in G[node]:
if neighbor not in marked:
total_marked += mark_component(G, neighbor, marked)
return total_marked
def check_connection(G, v1, v2):
# Return True if v1 is connected to v2 in G
@liang456
liang456 / weiboaccess.py
Created October 22, 2012 03:18
This a client program to access the Sina Weibo API
# This program rely on the following python SDK for sina
# https://github.com/michaelliao/sinaweibopy
from weibo import APIClient
APP_KEY = '1234567' # app key
APP_SECRET = 'abcdefghijklmn' # app secret
CALLBACK_URL = 'http://www.example.com/callback' # callback url
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
url = client.get_authorize_url()
@liang456
liang456 / PermandComp.py
Created October 29, 2012 20:31
Permutation and Composition Algorithm using Iterator in Python
def PermutationEnumerator(items, n=None):
if n is None:
n = len(items)
for i in range(len(items)):
v = items[i:i+1]
if n == 1:
yield v
else:
rest = items[:i] + items[i+1:]
for p in PermutationEnumerator(rest, n-1):
import random
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
# Tic Tac Toe
import random
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 10 strings representing the board (ignore index 0)
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
@liang456
liang456 / Bagels.py
Created October 31, 2012 12:28
This is a guess number game Bagels from http://inventwithpython.com/chapter11.html
import random
def getSecretNum(numDigits):
# Returns a string that is numDigits long, made up of unique random digits.
numbers = list(range(10))
random.shuffle(numbers)
secretNum = ''
for i in range(numDigits):
secretNum += str(numbers[i])
return secretNum
@liang456
liang456 / reversi.py
Created October 31, 2012 13:52
This is a Reversi game from http://inventwithpython.com/chapter15.html, I have revised it so you can run it under python 2.7
# Reversi
# This is a Reversi game from http://inventwithpython.com/chapter15.html, I have revised it so you can run it under python 2.7.
# Enjoy it
import random
import sys
def drawBoard(board):
# This function prints out the board that it was passed. Returns None.
HLINE = ' +---+---+---+---+---+---+---+---+'
http://inventwithpython.com/pygame/chapter3.html
@liang456
liang456 / sinaWeibo.py
Created October 31, 2012 22:15
This is the client code accessing the Sina weibo
This is the client code accessing the Sina weibo