Difference between revisions of "CSC111 Programs Created in Class 2018"

From dftwiki3
Jump to: navigation, search
(2/12/18)
Line 312: Line 312:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=2/16/18=
 
<br />
 
<br />
 +
::<source lang="python">
 +
# accumulate.py
 +
# D. Thiebaut
 +
# solution programs for 2/16/18
 +
 +
# --------------------------------------
 +
# sum of all the numbers from 1 to 100
 +
total = 0
 +
#print( "initial total:", total )
 +
for i in range( 0, 100+1, 5):
 +
    #print( i, total )
 +
    total = total + i
 +
    #print( "after sum:", i, total )
 +
    #print()
 +
 +
print( total )
 +
 +
# --------------------------------------
 +
# average of [100, 95,100, 90, 60, 85]
 +
total = 0
 +
count = 0
 +
for i in [100, 95,100, 90, 60, 85]:
 +
    total = total + i
 +
    print( "i =", i )
 +
    count = count + 1
 +
    print( "count = ", count )
 +
   
 +
print( "total = ", total, "count = ", count )
 +
print( "average = ", total/count )
 +
 +
 +
# --------------------------------------
 +
a = "#"
 +
b = "+"
 +
final = ""
 +
# from numbers to string
 +
print( "before loop: a=", a, "b=", b )
 +
for i in  [1,2,1,1,3,4]:
 +
    #print( i * a, end="", sep="" )
 +
    print( "i=", i, "a=", a, "b=", b )
 +
    final = final + i*a
 +
    print( "final =", final )
 +
    a, b = b, a
 +
 +
print( "final string = ", final )
 +
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 13:56, 16 February 2018

D. Thiebaut (talk) 12:30, 31 January 2018 (EST)


1/30/18

# wedWeek1.py
# D. Thiebaut
# Demo program week 1


# variables
age = 20
year = 2018

# compute year born
print( year - age )


# example of for loop
for name in [ "smith", 23, 3.14159, "hello" ]:
    print( name )

"""
challenge: print the following lines
***
Mae
*****
Alice
*******
Felicia
"""

for name in [ "Mae", "Alice", "Felicia" ]:
    print( '*' * len( name ) )
    print( name )


"""
new challenge: print the following lines
*
Mae
********
Alice
****
Felicia
**
"""

for name in [ "*", "Mae", "********", "Alice", "****", "Felicia", "**" ]:
    print( name )

02/05/18


Table of Fahrenheit to Celsius conversion

# 020518.py
# D. Thiebaut
#
# print table of temperatures
# start at 100 F down to -30 F
# in steps of 10
# using the formula: Celsius = (Farhenheit - 32 ) * 5 / 9

# print the header 
print( "Fahrenheit --> Celsius" )

# display the table of temperatures
# ranging from 100 down to -30F.
for fTemp in range( 100, -31, -10 ):
    cTemp = ( fTemp - 32 ) * 5 / 9
    print( fTemp, '-->', cTemp )

Problem: get user name and grade and display user information and grade as a bar graph.

# barGraph.py
# D. Thiebaut
# this program prompts the user for her information
# and grade and displays a bar-graph.
#
#First name?  Dominique
#Last name?   Thiebaut
#Id?          990123456
#Final grade? 90
#
#+———————————————————————————————————-———————————-+
#|Dominique Thiebaut                    990123456 |
#+———————————————————————————————————-———————————-+
#      00...10...20...30...40...50...60...70...80...90...100
#grade: #############################################
#class: ########################################
#
#

# input section
fName = input( "First name?  " )
lName = input( "Last name?   " )
Id    = input( "Id?          " )
final = input( "Final grade? " )

# output section
print( fName, lName, Id )
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
print( "barLen =", barLen )
nameIdLen = len( fName )+1+len( lName ) + len( Id )+1
print( "nameIdLen =", nameIdLen )

print( bar )
print( "|", fName, lName, ' '*(barLen-nameIdLen), Id, '|' )
print( bar )

2/7/18


We didn't have time to cover this problem, because of the snow storm... But it simply displays an 8 by 8 chessboard using hash tags.

# chessBoard.py
# D. Thiebaut
# displays an 8x8 chessboard, as shown below.
"""
###   ###   ###   ###   
###   ###   ###   ###   
###   ###   ###   ###   
   ###   ###   ###   ###
   ###   ###   ###   ###
   ###   ###   ###   ###
###   ###   ###   ###   
###   ###   ###   ###   
###   ###   ###   ###   
   ###   ###   ###   ###
   ###   ###   ###   ###
   ###   ###   ###   ###
###   ###   ###   ###   
###   ###   ###   ###   
###   ###   ###   ###   
   ###   ###   ###   ###
   ###   ###   ###   ###
   ###   ###   ###   ###
###   ###   ###   ###   
###   ###   ###   ###   
###   ###   ###   ###   
   ###   ###   ###   ###
   ###   ###   ###   ###
   ###   ###   ###   ###
"""
numRows = eval( input( "Number of rows?    " ) )
numCols = eval( input( "Number of columns? " ) )
white="   "
black="###"

bar1 = ""
bar2 = ""
for i in range( numCols//2 ):
    bar1 = bar1 + white + black
    bar2 = bar2 + black + white

for i in range( numRows//2 ):
    # display 2 lines of squares
    # alternating the colors
    # 5    ###   ###   ###   ###
    #     ###   ###   ###   ###
    #     ###   ###   ###   ###
    #  ###   ###   ###   ###   
    #  ###   ###   ###   ###   
    #  ###   ###   ###   ###   
    for n in range( 3 ):
        print( bar1 )
    for n in range( 3 ):
        print( bar2 )


2/9/18


# barGraph.py
# D. Thiebaut
# this program prompts the user for her information
# and grade and displays a bar-graph.
#
#First name?  Dominique
#Last name?   Thiebaut
#Id?          990123456
#Final grade? 90
#
#+———————————————————————————————————-———————————-+
#|Dominique Thiebaut                    990123456 |
#+———————————————————————————————————-———————————-+
#      00...10...20...30...40...50...60...70...80...90...100
#grade: #############################################
#class: ########################################
#
#

# box geometry
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )

# input section
fName      = input( "First name? " )
fNameLen   = len( fName )
lName      = input( "Last name?  " )
lNameLen   = len( lName )
Id         = input( "Id?         " )
IdLen      = len( Id )
numSpaces  = barLen -3 -(fNameLen+1+lNameLen+IdLen)
grade      = eval( input( "Final grade? " ) )
classGrade = 80

# output section
print( bar )
print( '|' + fName, ' ', lName, ' '*(numSpaces), Id, ' |', sep='' )
print( bar )

# print the scale
print( "      00...10...20...30...40...50...60...70...80...90...100" )

# print the student grade as a bar
numHashTags = grade // 2
print( "grade:", '#' * numHashTags )

#print the class grade as a bar
numHashTags = classGrade // 2
print( "class:", '#' * numHashTags )


2/12/18


# teller.py
# D. Thiebaut
# simulates teller machine program
# prompts user, displays number of bills

# input amount to withdraw (use eval)
amount = eval( input( "Amount to withdraw? " ) )
amount = abs( amount )
print( "Amount:", amount )


# compute number of bills, 20, 10, 5, 1
no20s  = amount // 20
amount = amount % 20  # leftover after giving out all the 20s 

no10s  = amount // 10 
amount = amount % 10 # leftover after giving out all the 10s 

no5s   = amount // 5
amount = amount % 5 # leftover after giving out all the 5s 

no1s   = amount


# display number of bills
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )


2/14/18


# generate a table of the powers of 2
"""
    0    1
    1    2
    2    4
    3    8
    4   16
    5   32
    6   64
    7  128
    8  256
    9  512
1234567890123
"""
for i in range( 10 ):
    print( "{0:5}{1:5}".format( i, 2**i ) )


# display the following table:
# 1: len(Doc    )= 3
# 2: len(Grumpy )= 6
# 3: len(Happy  )= 5
# 4: len(Sleepy )= 6
# 5: len(Dopey  )= 5
# 6: len(Bashful)= 7
# 7: len(Sneezy )= 6

count = 1
for name in ["Doc", "Grumpy", "Happy", 
                    "Sleepy", "Dopey", "Bashful",
                    "Sneezy" ]:
    print( "{0:1}: len({1:<7})= {2:1}"
            .format( count, name, len(name) ) )
    count = count + 1


2/16/18


# accumulate.py
# D. Thiebaut
# solution programs for 2/16/18

# --------------------------------------
# sum of all the numbers from 1 to 100
total = 0
#print( "initial total:", total )
for i in range( 0, 100+1, 5):
    #print( i, total )
    total = total + i
    #print( "after sum:", i, total )
    #print()

print( total )

# --------------------------------------
# average of [100, 95,100, 90, 60, 85]
total = 0
count = 0
for i in [100, 95,100, 90, 60, 85]:
    total = total + i
    print( "i =", i )
    count = count + 1
    print( "count = ", count )
    
print( "total = ", total, "count = ", count )
print( "average = ", total/count )


# --------------------------------------
a = "#"
b = "+"
final = ""
# from numbers to string
print( "before loop: a=", a, "b=", b )
for i in   [1,2,1,1,3,4]:
    #print( i * a, end="", sep="" )
    print( "i=", i, "a=", a, "b=", b )
    final = final + i*a
    print( "final =", final )
    a, b = b, a

print( "final string = ", final )