python
「已注销」
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
exponential complexity DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'def getBinaryRep(n, numDigits): """Assumes n and numDigits are non-negative ints Returns a numDigits str转载 2015-07-09 08:25:49 · 360 阅读 · 0 评论 -
ADT in python DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import datetimeclass Person(object): def __init__(self, name): """Create a person""" self转载 2015-07-10 08:12:54 · 489 阅读 · 0 评论 -
interweaving of objects DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import datetimeclass Person(object): def __init__(self, name): """Create a person""" self转载 2015-07-10 08:50:24 · 365 阅读 · 0 评论 -
histogram DEMO
vals = [1, 199]for i in range(1000): num1 = random.choice(range(1, 100)) num2 = random.choice(range(1, 100)) vals.append(num1+num2)pylab.hist(vals, bins = 10)pylab.show()转载 2015-07-12 10:05:17 · 589 阅读 · 0 评论 -
How to use pylab to plot figure()?
#quote from MIT 'introduction to computation and programming using python, Revised'import pylabprinciple = 10000 #initial investmentinterestRate = 0.05years = 20values = []for i in range(years +转载 2015-07-10 20:11:53 · 574 阅读 · 0 评论 -
flip a coin(WLLN?)
#quote from MIT 'introduction to computation and programming using python, Revised'import randomdef flip(numFlips): heads = 0.0 for i in range(numFlips): if random.random() < 0.5:转载 2015-07-12 08:14:57 · 774 阅读 · 0 评论 -
flip coins(more and more flips per each trial)(WLLN)
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylabdef flipPlot(minExp, maxExp): """Assumes minExp and maxExp positive integers; minExp转载 2015-07-12 08:27:59 · 570 阅读 · 0 评论 -
random DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import randomdef rollDie(): """Returns a random int between 1 and 6""" return random.choice([1,2,3,4,5,6])转载 2015-07-12 08:09:24 · 369 阅读 · 0 评论 -
Coefficient of Variation(CV) DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylab def stdDev(X): """Assumes that X is a list of numbers. Returns the standa转载 2015-07-12 09:33:45 · 1581 阅读 · 0 评论 -
memorization DEMO
# quote from 'introduction to computation and programming # using Python, revised, MIT press def fastFib(n, memo = {}): """Assumes n is an int >= 0, memo used only by recursive calls转载 2015-07-26 12:44:57 · 790 阅读 · 0 评论 -
WLLN(Weak Law of Large Numbers) DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylabdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot += (x-转载 2015-07-12 10:41:54 · 2592 阅读 · 0 评论 -
function invoke objects DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import datetimeclass Person(object): def __init__(self, name): """Create a person""" self转载 2015-07-10 09:11:06 · 451 阅读 · 0 评论 -
inheritance & class variable DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'import datetimeclass Person(object): def __init__(self, name): """Create a person""" self转载 2015-07-10 08:34:31 · 384 阅读 · 0 评论 -
linear search DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'def linearSearch(L, x): for e in L: if e == x: return True return FalseL = [1, 2,转载 2015-07-09 08:07:45 · 436 阅读 · 0 评论 -
tuple DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'def findDivisors(n1, n2): """Assumes that n1 and n2 are positive ints Returns a tuple containing all comm转载 2015-07-09 09:58:43 · 392 阅读 · 0 评论 -
binary search DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'def search(L, e): """Assumes L is a list, the elements of which are in ascending order. Returns转载 2015-07-09 08:55:37 · 376 阅读 · 0 评论 -
sequence DEMO(tuple or string)
#quote from MIT 'introduction to computation and programming using python, Revised'def findExtremeDivisors(n1, n2): """Assumes that n1 and n2 are positive ints Returns a tuple containing t转载 2015-07-09 10:04:40 · 407 阅读 · 0 评论 -
list DEMO
L = ['hello', 100, 89.78]for i in range(len(L)): print L[i] hello10089.78原创 2015-07-09 10:06:38 · 486 阅读 · 0 评论 -
traverse a string demo
s = 'hello'for e in s: print e for i in range(len(s)): print s[i]原创 2015-05-25 21:32:56 · 422 阅读 · 0 评论 -
how to get cwd(current working directory) in python?
quote from 'think python, how to think like a computer scientist'转载 2015-05-26 10:06:07 · 2189 阅读 · 0 评论 -
list is passed by reference in python!!!
def fun(L): L.append(999)原创 2015-05-25 21:22:03 · 397 阅读 · 0 评论 -
reference & alias demo
原创 2015-05-26 19:24:29 · 488 阅读 · 0 评论 -
IntSet() DEMO
#quote from MIT 'introduction to computation and programming using python, Revised'class IntSet(object): """An IntSet is a set of integers""" #Information about the implementation (not the ab转载 2015-07-10 07:44:49 · 421 阅读 · 0 评论 -
Dynamic Programming(DP) (Optimal substructure + overlapping subproblems(memorization))DEMO
# quote from 'introduction to computation and programming # using Python, revised, MIT press class Item(object): def __init__(self, n, v, w): self.name = n self.value转载 2015-07-26 13:14:36 · 821 阅读 · 0 评论 -
change to log coordinate & dot plot
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylabdef flipPlot(minExp, maxExp): """Assumes minExp and maxExp positive integers; minExp转载 2015-07-12 08:33:58 · 579 阅读 · 0 评论 -
plot ErrorBar
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylabdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot += (x-转载 2015-07-13 09:17:42 · 2133 阅读 · 0 评论 -
polymorphic read
#quote from MIT 'introduction to computation and programming using python, Revised' def readVal(valType, requestMsg, errorMsg): while True: val = raw_input(requestMsg + ' ') try:转载 2015-07-08 12:13:01 · 479 阅读 · 0 评论 -
exception as a control flow mechanism
#quote from MIT 'introduction to computation and programming using python, Revised' def getRatios(vect1, vect2): """Assumes: vect1 and vect2 are lists of equal length of numbers Returns :转载 2015-07-08 12:20:52 · 441 阅读 · 0 评论 -
random walk for different types of Drunks
#quote from 'introduction to computation and programming #using Python, revised, MIT press'import randomimport pylabdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: t转载 2015-07-20 10:15:36 · 620 阅读 · 0 评论 -
get final location DEMO
#quote from 'introduction to computation and programming #using Python, revised, MIT press'import randomimport pylabdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: t转载 2015-07-20 10:29:31 · 586 阅读 · 0 评论 -
trace walk DEMO
#quote from 'introduction to computation and programming #using Python, revised, MIT press'import randomimport pylabdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: t转载 2015-07-20 10:38:55 · 450 阅读 · 0 评论 -
random walk DEMO
#quote from 'introduction to computation and programming #using Python, revised, MIT press'import randomdef stdDev(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot += (x-mean转载 2015-07-20 09:16:36 · 636 阅读 · 0 评论 -
Coefficient of Determination(R Squared)(How to determine goodness of fit)?
# quote from 'introduction to computation and programming # using Python, revised, MIT press import pylabdef getTrajectoryData(fileName): dataFile = open(fileName, 'r') distances = []转载 2015-07-24 09:38:38 · 992 阅读 · 0 评论 -
Exponential Fitting
# quote from 'introduction to computation and programming # using Python, revised, MIT press import pylabimport math#define an arbitrary exponential functiondef f(x): return 3*(2**(1.2*x转载 2015-07-24 10:00:01 · 693 阅读 · 0 评论 -
best fit line
#quote from 'introduction to computation and programming #using Python, revised, MIT pressimport pylabdef getData(fileName): dataFile = open(fileName, 'r') distances = [] masses = [转载 2015-07-24 08:30:54 · 1500 阅读 · 0 评论 -
Mortgage DEMO
# quote from 'introduction to computation and programming # using Python, revised, MIT press import pylabdef findPayment(loan, r, m): """Assumes: loan and r are floats, m an int Ret转载 2015-07-24 13:01:27 · 697 阅读 · 0 评论 -
exception as a control flow mechanism
#quote from MIT 'introduction to computation and programming using python, Revised' while True: val = raw_input('Enter an integer: ') try: val = int(val) print 'The square of转载 2015-07-08 12:07:08 · 457 阅读 · 0 评论 -
aliasing demo(binding, reference)
L = ['a', 'b', 'c']L1 = LL1.reverse() #also reverse Lprint Lprint L1 %run "C:\Users\Administrator\test.py"['c', 'b', 'a']['c', 'b', 'a']原创 2015-07-08 12:01:26 · 449 阅读 · 0 评论 -
greedy algorithm DEMO
# quote from 'introduction to computation and programming # using Python, revised, MIT press class Item(object): def __init__(self, n, v, w): self.name = n self.value = flo转载 2015-07-26 10:57:37 · 583 阅读 · 0 评论 -
BFS & DFS DEMO
# quote from 'introduction to computation and programming # using Python, revised, MIT press class Node(object): def __init__(self, name): """Assumes name is a string""" se转载 2015-07-26 12:19:28 · 451 阅读 · 0 评论
分享