Contents

  1. ascii_dictionary.py
  2. descendSort2.py
  3. descendSort.py
  4. fibs2.py
  5. fibs.py

ascii_dictionary.py 1/5

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

ascii= {}
x = ord('a')

while x <= ord('z'):
    ascii[chr(x)] = x
    x+=1

print ascii

descendSort2.py 2/5

[
top][prev][next]
# Demonstrate passing lists to functions
# Sara Sprenkle, 10.22.2007

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

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

# input: a list containing three numbers
# sorts the list in descending order
# Note: does not return anything, no output
def descendSort3Nums(list3):
    list3.sort()
    list3.reverse()


main()

descendSort.py 3/5

[
top][prev][next]
# Demonstrate passing lists to functions
# Sara Sprenkle, 10.22.2007

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

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

# input: a list containing three numbers
# sorts the list in descending order
# Note: does not return anything, no output
def descendSort3Nums(list3):
    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
        
main()

fibs2.py 4/5

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

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 5/5

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

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

Generated by GNU enscript 1.6.4.