import math def stdDevOfLengths(L): """ L: a list of strings returns: float, the standard deviation of the lengths of the strings, or NaN if L is empty """ if len(L) == 0: return float("NaN") print len(L) curMean = stdMean(L) print curMean curVarience = stdVarience(curMean, L) print curVarience return math.sqrt(curVarience) def stdMean(L): totCalc = 0 for s in L: ##print len(s) totCalc += float(len(s)) #print totCalc return float(totCalc / len(L)) def stdVarience(mean, L): totVar = 0 for s in L: totVar += (len(s) - mean)**2 #print totVar return float(totVar / len(L)) #print float(51/7) #print stdMean(['', 'zgmto']) print stdDevOfLengths(['', 'zgmto'])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question