My teacher gives a review sheet which is a lot like the exam if not the exam but with some minor changes. I'm stuck with these fill in the blanks.
Fill-In-The-Blank Word Frequency
The following program gets the name of a file as a command-line argument, opens that file, reads from it and creates a dictionary of the words in the file and the number of times each word occurred.
import _______________
import sys
NUM_ARGS = ___________
words = {}
if len(sys.argv) != ______________:
print "This program needs a command-line argument"
print "that is the name of the file to evaluate."
sys._______________
file = open(_______________, 'r')
for line in ____________:
string.strip(___________)
_____________ = string.split(line)
for word in wordList:
# change word to be in all lower-case
word = string.lower(word)
# if the word has punctuation after it
if ord(____________) < ord('a') or ord(____________) > ord('z'):
# remove the punctuation mark from the word
word = word[: ___________ ]
# put the word in the dictionary and/or increment the word counter
count = words.___________(word, 0)
words[ ____________ ] = count + 1
file.______________
print _____________
this is what I have so far, I have no idea how to fill in the last two blanks.
import string
import sys
NUM_ARG = 2
words = {}
if len(sys.argv) != NUM_ARG:
print "This program needs a command-line argument"
print "that is the name of the file to evaluate."
sys.close
file = open(sys.argv[1], 'r')
for line in file:
string.strip(file)
wordList = string.split(line)
for word in wordList:
# change word to be in all lower-case
word = string.lower(word)
# if the word has punctuation after it
if ord(word) < ord('a') or ord(word) > ord('z'):
# remove the punctuation mark from the word
word = word[: ___________ ]
# put the word in the dictionary and/or increment the word counter
count = words.get(word, 0)
words[ word ] = count + 1
file.close
print _____________
Then the other one I am having trouble with.
Fill-In-The-Blank Sieve of Eratosthenes
The Sieve of Eratosthenes is an elegant well-known algorithm for finding the primes up to a specified n.
# sieve() implements the Sieve of Eratosthenes algorithm
# that finds the prime numbers up to n.
#
# Inputs: n, the ending value
# primes, a list to hold the primes
# Output: None, but the list of primes will be modified
# by the function to hold the primes less than
# or equal to n. The list doesn't need to be
# returned, since lists are mutable.
________ sieve( _____________, primes):
numbers = _______________
# make a list of integers from 2 to n, inclusive
for i in range(______________):
numbers.append(______________)
del(numbers[ _____________ ])
del(numbers[ _____________ ])
# while there are still numbers in the list
while len(numbers) > _____________:
# the first number is always a prime
prime = numbers[0]
primes.append(______________)
del(numbers[0])
# make a list of indices where there are
# multiples of the current prime number
indices = []
for i in range(len(______________)):
if numbers[i] % _____________ == 0:
indices.append(_____________)
# reverse the indices for easy deletes
indices._____________
# delete these multiples since they're not primes
for index in _____________:
del(numbers[ ________________ ])
what I was able to figure out
def sieve( n, primes):
numbers = []
# make a list of integers from 2 to n, inclusive
for i in range(2, n + 1):
numbers.append(i)
del(numbers[ _____________ ])
del(numbers[ _____________ ])
# while there are still numbers in the list
while len(numbers) > -1:
# the first number is always a prime
prime = numbers[0]
primes.append(______________)
del(numbers[0])
# make a list of indices where there are
# multiples of the current prime number
indices = []
for i in range(len(______________)):
if numbers[i] % ____________ == 0:
indices.append(_____________)
# reverse the indices for easy deletes
indices._____________
# delete these multiples since they're not primes
for index in _____________:
del(numbers[ ________________ ])
I'm really stumped on the last one.