Difference between revisions of "CSC111 Lab 2 Solutions 2012"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- Solution programs for Lab 2. <br /> <onlydft> <br /> <source lang="python"> # Lab 2 solution programs # D. Thiebaut # # ======= A...")
 
Line 3: Line 3:
 
Solution programs for [[CSC111 Lab 2 Solutions 2012| Lab 2]].
 
Solution programs for [[CSC111 Lab 2 Solutions 2012| Lab 2]].
 
<br />
 
<br />
<onlydft>
+
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
Line 261: Line 261:
 
</source>
 
</source>
 
<br />
 
<br />
</onlydft>
+
 
 
 
<br /><br />
 
<br /><br />
 
<br /><br /><br />
 
<br /><br /><br />

Revision as of 16:47, 6 February 2014

--D. Thiebaut (talk) 18:53, 4 February 2014 (EST)


Solution programs for Lab 2.


# Lab 2 solution programs
# D. Thiebaut
#

# ======= ATM Program Version 1 ==============
"""
# set the amount
amount = 97
print( "Amount to withdraw = ", amount )
print( "Please lift keyboard and find: " )

# number $20 is amount divided by 20
# leftover is amount modulo 20
no20 = amount // 20
leftover = amount % 20
print( no20, "$20-bill(s)" )

# number $10 is previous leftover divided by 10
# new leftover is previous leftover modulo 10
no10 = leftover // 10
leftover = leftover % 10
print( no10, "$10-bill(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
no5 = leftover // 5
leftover = leftover % 5
print( no5, "$5-bill(s)" )

# number $1 is previous leftover
no1 = leftover
print( no1, "$1-bill(s)" )
"""

# ======= ATM Program Version 2 ==============
"""
DENOM1 = 20
DENOM2 = 10
DENOM3 = 5
DENOM4 = 1

# set the amount
amount = 97
print( "Amount to withdraw = $", amount )
print( "Please lift keyboard and find: " )

# number $20 is amount divided by 20
# leftover is amount modulo 20
noBills = amount // DENOM1
leftover = amount % DENOM1
print( noBills, "$", DENOM1, "bill(s)" )

# number $10 is previous leftover divided by 10
# new leftover is previous leftover modulo 10
noBills = leftover // DENOM2
leftover = leftover % DENOM2
print( noBills, "$", DENOM2, "bill(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
noBills = leftover // DENOM3
leftover = leftover % DENOM3
print( noBills, "$", DENOM3, "bill(s)" )

# number $1 is previous leftover
noBills = leftover 
print( noBills, "$", DENOM4, "bill(s)" )

"""
# ======= ATM Program Version 3 ==============

"""
DENOM1 = 100
DENOM2 = 50
DENOM3 = 10
DENOM4 = 1

# set the amount
amount = 97
print( "Amount to withdraw = $", amount )
print( "Please lift keyboard and find: " )

# number $20 is amount divided by 20
# leftover is amount modulo 20
noBills = amount // DENOM1
leftover = amount % DENOM1
print( noBills, "$", DENOM1, "bill(s)" )

# number $10 is previous leftover divided by 10
# new leftover is previous leftover modulo 10
noBills = leftover // DENOM2
leftover = leftover % DENOM2
print( noBills, "$", DENOM2, "bill(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
noBills = leftover // DENOM3
leftover = leftover % DENOM3
print( noBills, "$", DENOM3, "bill(s)" )

# number $1 is previous leftover
noBills = leftover 
print( noBills, "$", DENOM4, "bill(s)" )
"""

# ======= ATM Program Version 4: PENNIES! ==============
"""
# set the amount
amount = 84
print( "Amount to withdraw =", amount, "penny or pennies" )
print( "Please lift keyboard and find: " )

# number quarters is amount divided by 25
# leftover is amount modulo 20
noCoins = amount // 25
leftover = amount % 25
print( noCoins, "quarter(s)" )

# number dimes is previous leftover divided by 10
# new leftover is previous leftover modulo 10
noCoins = leftover // 10
leftover = leftover % 10
print( noCoins, "dime(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
noCoins = leftover // 5
leftover = leftover % 5
print( noCoins, "nickel(s)" )

# number $1 is previous leftover
noCoins = leftover 
print( noCoins, "penny or pennies" )
"""

# ================= Bills and Coins ====================
"""
# set the amount
amount = 97.38
print( "Amount to withdraw = ", amount )
print( "Please lift keyboard and find: " )

integerAmount = int( amount )
pennies = int( amount * 100 ) % 100

# number $20 is amount divided by 20
# leftover is amount modulo 20
no20 = integerAmount // 20
leftover = integerAmount % 20
print( no20, "$20-bill(s)" )

# number $10 is previous leftover divided by 10
# new leftover is previous leftover modulo 10
no10 = leftover // 10
leftover = leftover % 10
print( no10, "$10-bill(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
no5 = leftover // 5
leftover = leftover % 5
print( no5, "$5-bill(s)" )

# number $1 is previous leftover
no1 = leftover
print( no1, "$1-bill(s)" )

# number quarters is amount divided by 25
# leftover is amount modulo 20
noCoins = pennies // 25
leftover = pennies % 25
print( noCoins, "quarter(s)" )

# number dimes is previous leftover divided by 10
# new leftover is previous leftover modulo 10
noCoins = leftover // 10
leftover = leftover % 10
print( noCoins, "dime(s)" )

# number $5 is previous leftover divided by 5
# new leftover is previous leftover modulo 5
noCoins = leftover // 5
leftover = leftover % 5
print( noCoins, "nickel(s)" )

# number $1 is previous leftover
noCoins = leftover 
print( noCoins, "cent(s)" )
"""

#======================== Get User Information =========================
"""
firstName = input( "your first name? " )
lastName  = input( "your first name? " )
box       = input( "your box number? " )
phone     = input( "your phone number? " )
id        = input( "your Smith Id? " )

line = "Name:  " + firstName + " " + lastName + " (" + id + ")"
length = len( line ) + 2
print( "+" + "-" * ( length ) + "+" )
print( "| " + line + " |" )

line = "box #: " + box 
print( "| " + line + " "*(length-len( line )-1 ) + "|" )

line = "phone: " + phone
print( "| " + line + " "*(length-len( line )-1 ) + "|" )

print( "+" + "-" * ( length ) + "+" )

"""

#========================== Second Converter ===========================

# get user input
answer = input( "Please enter a time expressed in seconds: " )
seconds = int( answer )
#print( seconds )

# number of days = seconds / 24 hours * 60 minutes * 60 seconds
# left over is what's left 
noDays  = seconds // ( 24 * 60 * 60 )
leftOver = seconds % ( 24 * 60 * 60 )
#print( leftOver )

# number of hours is left over / 60 minutes * 60 seconds
# left over is left over after that
noHours = leftOver // ( 60 * 60 )
leftOver = leftOver % ( 60 * 60 )
#print( leftOver )

# number minutes is left over from previous step / 60
# number of seconds is what's left over after that
noMinutes = leftOver // 60
noSeconds = leftOver % 60

line = "| " + str( noDays ) + " day(s) " + str( noHours )
line = line + " hour(s) " + str( noMinutes ) + " minute(s) "
line = line + str( noSeconds ) + " second(s) |"

length = len( line )

print()
print()
print( "+" + "-" * (length-2) + "+" )
print( line )
print( "+" + "-" * (length-2) + "+" )