Contents

  1. daysOfWeek.py
  2. fibs2.py
  3. fibs.py
  4. writeSumReport.py

daysOfWeek.py 1/4

[
top][prev][next]
# Example illustrating list operations: concatenation and iteration
# by Sara Sprenkle

weekDays = ["Mon", "Tue", "Wed", "Thu", "Fri"]
weekendDays = ["Sat", "Sun"]

# combine two lists into one
daysOfWeek = weekDays + weekendDays

print "The Days of the Week:"

# iterate through elements of list
for day in daysOfWeek:
    print day

print "\nAGAIN!"

# iterate through positions of list
for x in xrange(len(daysOfWeek)):
    print daysOfWeek[x]

fibs2.py 2/4

[
top][prev][next]
# Example of creating a list of the appropriate size
# Computes the first SIZE Fibonacci numbers
# Sara Sprenkle

SIZE = 15

print "This program generates the first", SIZE, "Fibonacci numbers"

# creates a list of size 15, containing elements 0 to 14
fibs = range(SIZE) 

fibs[0] = 1
fibs[1] = 1

for x in xrange(2,SIZE):
    newfib = fibs[x-1]+fibs[x-2]
    fibs[x] = newfib

#for num in fibs:
#    print num

print fibs

fibs.py 3/4

[
top][prev][next]
# Example of appending to a list
# Computes the first SIZE Fibonacci numbers
# Sara Sprenkle

SIZE = 15

print "This program generates the first", SIZE, "Fibonacci numbers"

# create an empty list
fibs = []

# append the first two Fibonacci numbers
fibs.append(1)
fibs.append(1)

# compute the next 13 Fibonacci numbers
for x in xrange(2,SIZE):
    newfib = fibs[x-1]+fibs[x-2]
    fibs.append(newfib)


# print the Fibonacci numbers as a list
print fibs


# Tradeoff of using more space (the list) for easier writing

writeSumReport.py 4/4

[
top][prev][next]
# Given a file containing students names and their years (freshman, sophomore,
# junior, or senior) for this class, creates a report (in a file) that
# says the year and how many students from that year are in this class, on the
# same line.
#
# Note: this is not the most efficient solution because we are going through a 
# file multiple times, instead of checking for all the search times in the line
# at the same time.  There is sometimes a tradeoff between ease of writing
# (e.g., using a function) and efficiency.  Usually, the programmer's time is
# more valuable than computational time, but there are limits.
#
# CSCI 111

FILENAME="data/years2.dat"
REPORTNAME="data/report.dat"

def main():
    term = "FY"

    reportFile = file(REPORTNAME, "w")
    
    reportFile.write("# This file contains ...\n")

    fycount = getNumLinesTermOccurs(FILENAME, term)
    reportFile.write("%-10s %5d\n" % (term, fycount))
    
    term = "SO"
    yearcount = getNumLinesTermOccurs(FILENAME, term)
    reportFile.write("%-10s %5d\n" % (term, yearcount))
    
    term = "JR"
    yearcount = getNumLinesTermOccurs(FILENAME, term)
    reportFile.write("%-10s %5d\n" % (term, yearcount))
    
    term = "SR"
    yearcount = getNumLinesTermOccurs(FILENAME, term)
    reportFile.write("%-10s %5d\n" % (term, yearcount))

    reportFile.close()

# given the name of the file (string) and the term to search for (string)
# returns the number of lines that the term occurs on.
def getNumLinesTermOccurs(filename, term):
    count = 0
    lineNum = 1
    searchFile = file(filename, "r")
    
    # check every line in the file and see where term occurs
    for line in searchFile:
        line = line.strip()
        if line[0] != "#":
            if term in line:
                count += 1
                #print lineNum, line    
        lineNum += 1
    searchFile.close()
    return count

main()

Generated by GNU enscript 1.6.4.