Skip to content

Instantly share code, notes, and snippets.

@libswan
Created April 11, 2013 08:40
Show Gist options
  • Save libswan/5361763 to your computer and use it in GitHub Desktop.
Save libswan/5361763 to your computer and use it in GitHub Desktop.
V1. L12 Problem 7 - "present an angle of the data (words.txt) that you find interesting".
import random
import string
import pylab
WORDLIST_FILENAME = "words.txt"
def loadWords():
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = string.split(line)
print " ", len(wordlist), "words loaded."
return wordlist
wordList = loadWords()
def maxLength(wordList):
maxLen = 0
for i in wordList:
if len(i) > maxLen:
maxLen = len(i)
return maxLen
maxLen = maxLength(wordList)
def minLength(wordList):
minLen = maxLen
for i in wordList:
if len(i) < minLen:
minLen = len(i)
return minLen
minLen = minLength(wordList)
def countLengths(wordList):
thisLen = minLen
countIt = 0
thisLenList = []
countItList = []
sum = 0
while thisLen >= minLen and thisLen <= maxLen:
for i in wordList:
if len(i) == thisLen:
countIt += 1
thisLenList.append(thisLen)
countItList.append(countIt)
thisLen += 1
countIt = 0
for j in range(0, len(countItList)):
sum += countItList[j]
if sum != len(wordList):
print "Not matching up!"
else:
pylab.figure(1)
pylab.plot(thisLenList, countItList)
pylab.title('Number of Words by Word Length in the words.txt File')
pylab.xlabel('Length of Words')
pylab.ylabel('Count')
pylab.savefig('L12_P7_graph')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment