Contents

  1. descendSort.py
  2. file_read.py
  3. for_file_read.py
  4. swap.py
  5. using_readline.py

descendSort.py 1/5

[
top][prev][next]
# Demonstrate passing lists to functions
# One function modifies the list parameter, 
# one function is a "pure function" and does not modify the parameter
# CSCI111

import test

def main():
    # test descendSort3Nums
    aList = [1,2,3]
    descendSort3Nums(aList)
    print(aList)

    aList = [0, 5, -3]
    descendSort3Nums(aList)
    print(aList)
    
    aList = [7,4,1]
    descendSort3Nums(aList)
    print(aList)
    
    aList = [-1, -1, -3]
    descendSort3Nums(aList)
    print(aList)
    
    aList = [-1, -5, -3]
    descendSort3Nums(aList)
    print(aList)

def descendSort3Nums(list3):
    """
    Parameter: list3: a list containing three numbers
    Sorts the list in descending order
    Note: does not return anything, no output 
    """
    if list3[1] > list3[0]:
        # swap 'em
        tmp = list3[0]
        list3[0] = list3[1]
        list3[1] = tmp

    if list3[2] > list3[1]:
        # swap 'em
        tmp = list3[1]
        list3[1] = list3[2]
        list3[2] = tmp
    
    if list3[1] > list3[0]:
        # swap 'em
        tmp = list3[0]
        list3[0] = list3[1]
        list3[1] = tmp

def createDescendingList(list3):
    """
    Parameter: list3: a list containing three numbers
    Sorts the list in descending order and returns that list
    (Example of "pure function")
    """
    copyOfList3 = list3 + []
    
    if copyOfList3[1] > copyOfList3[0]:
        # swap 'em
        tmp = copyOfList3[0]
        copyOfList3[0] = copyOfList3[1]
        copyOfList3[1] = tmp

    if copyOfList3[2] > copyOfList3[1]:
        # swap 'em
        tmp = copyOfList3[1]
        copyOfList3[1] = copyOfList3[2]
        copyOfList3[2] = tmp
    
    if copyOfList3[1] > copyOfList3[0]:
        # swap 'em
        tmp = copyOfList3[0]
        copyOfList3[0] = copyOfList3[1]
        copyOfList3[1] = tmp
        
    return copyOfList3

def testDescendSort3Nums():
    origList = [1, 2, 3]
    descendSort3Nums(origList)
    # test that the list sorted is in the other order
    test.testEqual( origList, [3, 2, 1])
    
    aList = [-1, -5, -3]
    descendSort3Nums(aList)
    test.testEqual( aList, [-1, -3, -5])

def testCreateDescendingSort3Nums():
    origList = [1, 2, 3]
    test.testEqual( createDescendingList(origList), [3, 2, 1])
    # verify that the original list didn't change.
    test.testEqual( origList, [1, 2, 3] )
    
    aList = [-1, -5, -3]
    test.testEqual( createDescendingList(aList), [-1, -3, -5])
    test.testEqual( aList, [-1, -5, -3])

testDescendSort3Nums()
testCreateDescendingSort3Nums()

#main()

file_read.py 2/5

[
top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# creates a new file object, opening the file in read mode
myFile = open(FILENAME, "r")

# read the file and put it into one string
contents = myFile.read()

# close the file when you're done reading the file
myFile.close()

# display the contents of the file
print(contents)

for_file_read.py 3/5

[
top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# creates a new file object, opening the file in "read" mode
dataFile = open(FILENAME, "r")

# reads in the file line-by-line and prints the content of the file
for line in dataFile:
    #line = line.strip("\n")
    print(line)

# close the file with the method "close"
dataFile.close()

swap.py 4/5

[
top][prev][next]
# Review: Attempt at swapping two variables in a function
# FAIL!  Swapping within a function won't work. :(
# Why?  With immutable data types (like integers), the functions are
# passed *copies* of the parameters, not the original variables

def main():
    x = 5
    y = 7
    
    print("In main:")
    print("x =", x)
    print("y =", y) 
    
    swap(x, y)
    
    print("In main after call to swap:")
    print("x =", x)
    print("y =", y) 


def swap(a, b):
    """
    Swaps the values stored in a and b
    """
    tmp = a
    a = b
    b = tmp
    print("In swap function: ", a, b)

main()

using_readline.py 5/5

[
top][prev][next]
# One way to use readline()
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# creates a new file object, opening the file in "read" mode
dataFile = open(FILENAME, "r")

# reads in the file line-by-line and prints the content of the file
line = dataFile.readline()

while line != "":
    #print(line[:-1])
    #print(line, end="")
    print(line.strip())
    line = dataFile.readline()

# What happens when we try to read the next line, after we have already
# read through the file?
#line = dataFile.readline()
# Answer: That line is also an empty string:
#print(line)

# close the file with the method "close"
dataFile.close()

Generated by GNU Enscript 1.6.6.