CSC111 Class Demo Programs 2015

From dftwiki3
Revision as of 11:48, 12 February 2015 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- ==2/11/15== <br /> <source lang="python"> # demo2.py # D. Thiebaut # Programs demonstrated in class on 2/11/15 # See lecture notes for that day for details. # i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 11:48, 12 February 2015 (EST)


2/11/15


# demo2.py
# D. Thiebaut
# Programs demonstrated in class on 2/11/15
# See lecture notes for that day for details.


# in this function, we look at the problem of transforming
# information from numbers in a list [1, 2, 1, 3, 4] to
# a string of characters: #++#+++####.
def main3():

    # set 2 variables that contain the 2 types of characters
    # we are going to print.  
    ch1 = "#"
    ch2 = "+"

    # we are going to accumulate characters in the string line.
    # we have to initialize it to something before we start.
    line = ""

    # we loop through the list of numbers.  For each one, we
    # generate a list of that number of # or + characters
    # and we accumulate it at the end of the string line.
    for i in [1, 2, 1, 3, 4]:
        # debugging statement to be removed later
        print( "beg loop: line=", line, " i=", i,
               "ch1=",ch1,"ch2=",ch2 )
        
        line = line + (i*ch1)  # add i characters to the end of line
        ch1, ch2 = ch2, ch1    # swap the characters

        # debugging statements to be removed later
        print( "end loop: line=", line, " i=", i,
               "ch1=",ch1,"ch2=",ch2 )
        print()

    print( "line =", line )


# computation of the factorial of a number.
# we observe that Python can computer *very large* numbers!
def main2():
    
    n = 1000

    fact = 1
    for i in range( n, 1, -1 ):
        #print( "enter loop: fact =",fact,"i=", i )
        fact = fact * i
        #print( "end of loop: fact=",fact,"i=", i )
        #print()
    
    print( "factorial of", n, "= ", fact )


# Compute the sum of a list of numbers
def main1():
    ages = [12, 14, 10]

    # we accumulate the sum in sumAge.  It has to
    # be initialized to 0 first.
    sumAge = 0

    # loop and get each number in the list into age.  Then
    # add age to sumAge, so that we accumulate the sum, one
    # loop at a time.
    for age in [12, 14, 10]:
        #print( age, sumAge )
        sumAge = sumAge + age

    print( "sum = ", sumAge )

# Displaying a nicely formatted table of temperatures.
def main():
    start = 30
    stop  = 50
    increment = 2

    # create a string that contains the top and bottom bars
    bar    ="+" + ("-"*6 ) + "+" + ("-"*6 ) + "+"
    print( bar )

    # display the Fahrenheit and Celsius, in a columnar fashion
    for fahr in range( start, stop+1, increment ):
        celsius = ( fahr - 32 ) * 5 / 9
        print(  "|{1:6.2f}|{0:6.2f}|" . format( celsius, fahr ) )

    print( bar )

# MAIN ENTRY POINT OF OUR PROGRAM
#main()
#main1()
#main2()
main3()