|
|
(15 intermediate revisions by the same user not shown) |
Line 3: |
Line 3: |
| <showafterdate after="20150923 12:00" before="20151231 00:00"> | | <showafterdate after="20150923 12:00" before="20151231 00:00"> |
| <bluebox> | | <bluebox> |
| + | This homework is due on Thursday <strike>9/30/15</strike> 10/01/15, at 11:55 p.m. |
| + | <br /> |
| The problems in this assignment slowly take you from the program we solved in class, of a simple teller machine problem that expects integers and returns $20, $10, $5, and $1-bills, to a more sophisticated program that is robust enough to deal with erroneous inputs, and even inputs including pennies, and outputs the right amount of information in a nicely formatted way. | | The problems in this assignment slowly take you from the program we solved in class, of a simple teller machine problem that expects integers and returns $20, $10, $5, and $1-bills, to a more sophisticated program that is robust enough to deal with erroneous inputs, and even inputs including pennies, and outputs the right amount of information in a nicely formatted way. |
| <br /> | | <br /> |
| Please work in pairs on this homework, and submit your program twice, with a header containing both names. | | Please work in pairs on this homework, and submit your program twice, with a header containing both names. |
| + | <br /> |
| </bluebox> | | </bluebox> |
| <br /> | | <br /> |
| __TOC__ | | __TOC__ |
| + | <br /> |
| <br /> | | <br /> |
| | | |
Line 24: |
Line 28: |
| <br /> | | <br /> |
| Please enter amount to withdraw: <u>139</u> | | Please enter amount to withdraw: <u>139</u> |
| + | |
| Withdrawal amount: 139 | | Withdrawal amount: 139 |
| Please lift your keyboard and find: | | Please lift your keyboard and find: |
Line 43: |
Line 48: |
| <br /> | | <br /> |
| Please enter amount to withdraw: <u>139.55</u> | | Please enter amount to withdraw: <u>139.55</u> |
| + | |
| Withdrawal amount: 139 | | Withdrawal amount: 139 |
| Please lift your keyboard and find: | | Please lift your keyboard and find: |
Line 59: |
Line 65: |
| | | |
| Please enter amount to withdraw: <u>-122.99</u> | | Please enter amount to withdraw: <u>-122.99</u> |
| + | |
| Withdrawal amount: 122 | | Withdrawal amount: 122 |
| Please lift your keyboard and find: | | Please lift your keyboard and find: |
Line 82: |
Line 89: |
| | | |
| Please enter amount to withdraw: <u>124.46</u> | | Please enter amount to withdraw: <u>124.46</u> |
| + | |
| Withdrawal amount: 124.46 | | Withdrawal amount: 124.46 |
| Please lift your keyboard and find: | | Please lift your keyboard and find: |
Line 97: |
Line 105: |
| Imagine that we want to make the program super user-friendly, and have it output images of bills corresponding to the bills that will be given out by the machine. Our programs currently can't do graphics, but we can use a shortcut, and have the program output a string such as "[$20]" to represent a $20-bill. | | Imagine that we want to make the program super user-friendly, and have it output images of bills corresponding to the bills that will be given out by the machine. Our programs currently can't do graphics, but we can use a shortcut, and have the program output a string such as "[$20]" to represent a $20-bill. |
| | | |
− | Take your program for '''Problem 3''' above, and make it output "''icons''" for the bills that will be given out. | + | Take your program for '''Problem 3''' above, modify it, and make it output "''icons''" for the bills that will be given out. |
| | | |
| An example will illustrate what your program should emulate: | | An example will illustrate what your program should emulate: |
| <br /> | | <br /> |
− | Amount? '''91''' | + | Amount to withdraw? <u>91</u> |
| | | |
− | 4 $20-bill(s) [$20] [$20] [$20] [$20]
| + | 4 $20-bill(s) [$20] [$20] [$20] [$20] |
− | 1 $10-bill(s) [$10]
| + | 1 $10-bill(s) [$10] |
− | 0 $5-bill(s)
| + | 0 $5-bill(s) |
− | 1 $1-bill(s) [$1]
| + | 1 $1-bill(s) [$1] |
− | Total: [$20] [$20] [$20] [$20] [$10] [$1] | + | Total = 6 bill(s): [$20] [$20] [$20] [$20] [$10] [$1] |
| <br /> | | <br /> |
| + | Notice that the line for $20-bills indicates that 4 bills will be given out, and shows 4 strings [$20], indicating that the user will get 4 bills of this denomination. <font color="magenta">Make sure there is a space between the [$xx] strings</font>, otherwise the automatic grader will give you an error. |
| + | |
| + | Notice that the last line shows 6 different strings, indicating that 4 $20-, 1 $10-, and 1 $1-bill will be given out. |
| + | |
| Here's another example of the way the program should behave, for a different input: | | Here's another example of the way the program should behave, for a different input: |
| <br /> | | <br /> |
− | Amount? 11 | + | Amount to withdraw? <u>11</u> |
| | | |
− | 0 $20-bill(s)
| + | 0 $20-bill(s) |
− | 1 $10-bill(s) [$10]
| + | 1 $10-bill(s) [$10] |
− | 0 $5-bill(s)
| + | 0 $5-bill(s) |
− | 1 $1-bill(s) [$1]
| + | 1 $1-bill(s) [$1] |
− | Total: [$10] [$1] | + | Total = 2 bill(s): [$10] [$1] |
| | | |
| <br /> | | <br /> |
Line 126: |
Line 138: |
| </showafterdate> | | </showafterdate> |
| <br /> | | <br /> |
− | <showafterdate after="20150217 23:55" before="20150601 12:00"> | + | =Solution Programs= |
− | =Solution Program=
| + | <showafterdate after="20151002 00:" before="20151231 12:00"> |
| <br /> | | <br /> |
| <source lang="python"> | | <source lang="python"> |
Line 284: |
Line 296: |
| print( cents, "cent(s)" ) | | print( cents, "cent(s)" ) |
| | | |
− | | + | def main6(): |
− | def main7(): | + | # input section |
− | # get amount | + | amount = eval( input( "Amount? " ) ) |
− | amount = input( "Please enter amount to withdraw: " ) | + | print() |
− | amount = int( eval( amount ) )
| |
− | amount = abs( amount ) | |
− |
| |
− | originalAmount = amount
| |
| | | |
− | # compute no20s, no10s, no5s and no1s | + | # computation |
| no20s = amount // 20 | | no20s = amount // 20 |
| amount = amount % 20 | | amount = amount % 20 |
Line 301: |
Line 309: |
| | | |
| no5s = amount // 5 | | no5s = amount // 5 |
− | no1s = amount % 5 | + | amount = amount % 5 |
| + | no1s = amount |
| + | |
| + | # display results |
| + | print( "{0:5} $20-bill(s) ".format( no20s ), "[$20] "*no20s, sep="" ) |
| + | |
| + | print( "{0:5} $10-bill(s) ".format( no10s ), "[$10] "*no10s, sep="" ) |
| + | |
| + | print( "{0:5} $5-bill(s) ".format( no5s ), end="", sep="" ) |
| + | for i in range( no5s ): |
| + | print( "[$5] ", sep="", end="" ) |
| + | print() |
| | | |
− | # output result
| + | print( "{0:5} $1-bill(s) ".format( no1s ), end="", sep="" ) |
− | bar = "+--------------------------------+"
| + | for i in range( no1s ): |
− | print( bar )
| + | print( "[$1] ", sep="", end="" ) |
− | print( "| Withdrawal amount: ${0:<10} |".format( originalAmount ) ) | + | print() |
− | print( "| $20-bill(s): {0:<10} |".format( no20s ) )
| |
− | print( "| $10-bill(s): {0:<10} |".format( no10s ) )
| |
− | print( "| $5-bill(s): {0:<10} |".format( no5s ) ) | |
− | print( "| $1-bill(s): {0:<10} |".format( no1s ) )
| |
− | print( bar ) | |
| | | |
| + | print( "Total =", no20s+no10s+no5s+no1s, "bills:", |
| + | "[$20] "*no20s, "[$10] "*no10s, "[$5] "*no5s, "[$1] " * no1s ) |
| + | |
| + | |
| main1() | | main1() |
| main2() | | main2() |
Line 325: |
Line 343: |
| <br /> | | <br /> |
| <br /> | | <br /> |
− | <onlydft>
| |
− | =VPL Modules=
| |
− | ==Problem 1==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | #python=/usr/local/bin/python3.4
| |
− | python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 16"
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− | def main():
| |
− | # get amount
| |
− | amount = int( input( "Enter amount: " ) )
| |
− | originalAmout = amount
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no20s = amount // 20
| |
− | amount = amount % 20
| |
− |
| |
− | no10s = amount // 10
| |
− | amount = amount % 10
| |
− |
| |
− | no5s = amount // 5
| |
− | no1s = amount % 5
| |
− |
| |
− | # output result
| |
− | print( "amount: ", originalAmout )
| |
− | print( no20s, "$20-bill(s)" )
| |
− | print( no10s, "$10-bill(s)" )
| |
− | print( no5s, "$5-bill(s)" )
| |
− | print( no1s, "$1-bill(s)" )
| |
− |
| |
− | main()
| |
− |
| |
− | </source>
| |
− |
| |
− |
| |
− |
| |
− | ==Problem 2==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 16"
| |
− | TEMP1=\${TEMP1}.79
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− |
| |
− | def main():
| |
− | # get amount
| |
− | amount = input( "Please enter amount to withdraw: " )
| |
− | amount = int( eval( amount ) )
| |
− |
| |
− | originalAmout = amount
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no20s = amount // 20
| |
− | amount = amount % 20
| |
− |
| |
− | no10s = amount // 10
| |
− | amount = amount % 10
| |
− |
| |
− | no5s = amount // 5
| |
− | no1s = amount % 5
| |
− |
| |
− | # output result
| |
− | print( "Withdrawal amount: ", originalAmout )
| |
− | print( "Please lift your keyboard and find: " )
| |
− | print( no20s, "$20-bill(s)" )
| |
− | print( no10s, "$10-bill(s)" )
| |
− | print( no5s, "$5-bill(s)" )
| |
− | print( no1s, "$1-bill(s)" )
| |
− |
| |
− | main()
| |
− | </source>
| |
− |
| |
− | ==Problem 3==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 16"
| |
− | TEMP1=-\${TEMP1}.79
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− | def main():
| |
− | # get amount
| |
− | amount = input( "Please enter amount to withdraw: " )
| |
− | amount = int( eval( amount ) )
| |
− | amount = abs( amount )
| |
− |
| |
− | originalAmout = amount
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no20s = amount // 20
| |
− | amount = amount % 20
| |
− |
| |
− | no10s = amount // 10
| |
− | amount = amount % 10
| |
− |
| |
− | no5s = amount // 5
| |
− | no1s = amount % 5
| |
− |
| |
− | # output result
| |
− | print( "Withdrawal amount: ", originalAmout )
| |
− | print( "Please lift your keyboard and find: " )
| |
− | print( no20s, "$20-bill(s)" )
| |
− | print( no10s, "$10-bill(s)" )
| |
− | print( no5s, "$5-bill(s)" )
| |
− | print( no1s, "$1-bill(s)" )
| |
− |
| |
− | main()
| |
− | </source>
| |
− | ==Problem 4==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− |
| |
− | def main():
| |
− | # get amount
| |
− | amount = input( "Please enter amount to withdraw: " )
| |
− | amount = int( eval( amount ) )
| |
− | amount = abs( amount )
| |
− |
| |
− | originalAmout = amount
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no50s = amount // 50
| |
− | amount = amount % 50
| |
− |
| |
− | no20s = amount // 20
| |
− | amount = amount % 20
| |
− |
| |
− | no10s = amount // 10
| |
− | amount = amount % 10
| |
− |
| |
− | no5s = amount // 5
| |
− | no1s = amount % 5
| |
− |
| |
− | # output result
| |
− | print( "Withdrawal amount: ", originalAmout )
| |
− | print( "Please lift your keyboard and find: " )
| |
− | print( no50s, "$50-bill(s)" )
| |
− | print( no20s, "$20-bill(s)" )
| |
− | print( no10s, "$10-bill(s)" )
| |
− | print( no5s, "$5-bill(s)" )
| |
− | print( no1s, "$1-bill(s)" )
| |
− |
| |
− | main()
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 50"
| |
− | #TEMP1=-\${TEMP1}.79
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− |
| |
− | ==Problem 5==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 50"
| |
− | TEMP2=\$RANDOM
| |
− | let "TEMP2 %= 90"
| |
− | let "TEMP2 = TEMP2 + 4"
| |
− | TEMP1=\${TEMP1}.\${TEMP2}
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− | def main():
| |
− | # get amount
| |
− | amount = input( "Please enter amount to withdraw: " )
| |
− | amount = eval( amount )
| |
− | amount = abs( amount )
| |
− | originalAmount = amount
| |
− |
| |
− | # break down float into two ints, the dollar part, and
| |
− | # the number of pennies.
| |
− | intAmount = abs( int( amount ) )
| |
− | pennies = abs( int ( round ( (amount - intAmount) * 100 ) ) )
| |
− |
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no20s = intAmount // 20
| |
− | intAmount = intAmount % 20
| |
− |
| |
− | no10s = intAmount // 10
| |
− | intAmount = intAmount % 10
| |
− |
| |
− | no5s = intAmount // 5
| |
− | no1s = intAmount % 5
| |
− |
| |
− | # computer quarters, dimes, nickels, cents
| |
− | quarters = pennies // 25
| |
− | pennies = pennies % 25
| |
− |
| |
− | dimes = pennies // 10
| |
− | pennies = pennies % 10
| |
− |
| |
− | nickels = pennies // 5
| |
− | cents = pennies % 5
| |
− |
| |
− | # output result
| |
− | print( "Withdrawal amount: ", originalAmount )
| |
− | print( "Please lift your keyboard and find: " )
| |
− | print( no20s, "$20-bill(s)" )
| |
− | print( no10s, "$10-bill(s)" )
| |
− | print( no5s, "$5-bill(s)" )
| |
− | print( no1s, "$1-bill(s)" )
| |
− | print( quarters, "quarter(s)" )
| |
− | print( dimes, "dime(s)" )
| |
− | print( nickels, "nickel(s)" )
| |
− | print( cents, "cent(s)" )
| |
− |
| |
− | main()
| |
− |
| |
− |
| |
− | </source>
| |
− |
| |
− |
| |
− |
| |
− | ==Problem 6==
| |
− | ===vpl_run.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw3.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===vpl_evaluate.sh===
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw3.py
| |
− | solutionProg=hw3sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=\$RANDOM
| |
− | let "TEMP1 %= 130"
| |
− | let "TEMP1 = TEMP1 + 20"
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # function that prints the difference between user and expected output
| |
− | incorrectOutput() {
| |
− | echo "Comment :=>>- Your output is incorrect."
| |
− | #--- display test file ---
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut.org
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut.org
| |
− | echo "--|>"
| |
− |
| |
− | }
| |
− |
| |
− | # function that tells user of program crash or infinite loop, and what the test was.
| |
− | timeoutOutput() {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | i=\$1
| |
− | echo "Comment :=>>- Your program has timed out or crashed."
| |
− |
| |
− | #--- display test file ---
| |
− | echo "Comment :=>>- Your program tested with:"
| |
− | echo "<|--"
| |
− | cat data\${i}.txt
| |
− | echo "--|>"
| |
− | fi
| |
− | }
| |
− |
| |
− | # function that removes non-digits, extra spaces, and extra blank lines from text file.
| |
− | cleanup () {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cp \$1 dummy.out
| |
− |
| |
− | #--- remove non numbers and non minus---
| |
− | #cat \$1 | sed 's/[^0-9*.0-9\ ]*//g' > dummy.out
| |
− | #cp dummy.out \$1
| |
− |
| |
− | #--- remove multiple spaces ---
| |
− | cat \$1 | sed 's/ */ /g' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | sed 's/^[ \t]*//' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | #--- remove blank lines ---
| |
− | cat \$1 | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− | fi
| |
− | }
| |
− |
| |
− | for TEMP in \$TEMP1 ; do
| |
− |
| |
− | echo \$TEMP > input
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input > userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | cleanup userOut
| |
− | cleanup expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | #diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− | diff -y -w userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput
| |
− | grade=65
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \$TEMP"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=100
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | ===hw3.py===
| |
− | <source lang="bash">
| |
− | def main():
| |
− | # get amount
| |
− | amount = input( "Please enter amount to withdraw: " )
| |
− | amount = int( eval( amount ) )
| |
− | amount = abs( amount )
| |
− |
| |
− | originalAmount = amount
| |
− |
| |
− | # compute no20s, no10s, no5s and no1s
| |
− | no20s = amount // 20
| |
− | amount = amount % 20
| |
− |
| |
− | no10s = amount // 10
| |
− | amount = amount % 10
| |
− |
| |
− | no5s = amount // 5
| |
− | no1s = amount % 5
| |
− |
| |
− | # output result
| |
− | print()
| |
− | bar = "+--------------------------------+"
| |
− | print( bar )
| |
− | print( "| Withdrawal amount: ${0:<10} |".format( originalAmount ) )
| |
− | print( "| $20-bill(s): {0:<10} |".format( no20s ) )
| |
− | print( "| $10-bill(s): {0:<10} |".format( no10s ) )
| |
− | print( "| $5-bill(s): {0:<10} |".format( no5s ) )
| |
− | print( "| $1-bill(s): {0:<10} |".format( no1s ) )
| |
− | print( bar )
| |
− |
| |
− | main()
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− |
| |
− |
| |
| | | |
− | </onlydft>
| |
| | | |
| <br /> | | <br /> |
--D. Thiebaut (talk) 10:42, 20 September 2015 (EDT)
<showafterdate after="20150923 12:00" before="20151231 00:00">
This homework is due on Thursday 9/30/15 10/01/15, at 11:55 p.m.
The problems in this assignment slowly take you from the program we solved in class, of a simple teller machine problem that expects integers and returns $20, $10, $5, and $1-bills, to a more sophisticated program that is robust enough to deal with erroneous inputs, and even inputs including pennies, and outputs the right amount of information in a nicely formatted way.
Please work in pairs on this homework, and submit your program twice, with a header containing both names.
Problem 1: Teller Machine: Version 1
Rewrite the program we saw in class that asks the user for some amount of dollars, and outputs the number of $20, $10, $5 and $1 that needs to be distributed. Call your program hw3.py.
In this version, we assume that the user is well behaved and will enter only valid integer numbers.
Make sure you test your program well, so that it works well with particular withdrawal values, such as 0, 1, 5, 10, and 20.
Submit your program to the Moodle HW 3 PB 1 section. Note that the Moodle test program removes words from the output of your program and concentrates only on whether the numbers are correct or not.
Here is an example of how your program should behave. The user enters 139 when prompted for a value:
Please enter amount to withdraw: 139
Withdrawal amount: 139
Please lift your keyboard and find:
6 $20-bill(s)
1 $10-bill(s)
1 $5-bill(s)
4 $1-bill(s)
Problem 2: Teller Machine: User sometimes inputs floats
This version of your program should be robust enough to work well, even when the user enters a number of the form 10.25. In such a case we assume that the teller machine does not return coins. So your program will only treat the integer part of the sum entered by the user, and will discard the decimal digits. So 10.25 will be treated as 10.
Call your program hw3.py. You will have to take the string returned by the input() function and one or more functions (such as abs(), round(), int(), eval(), for example) to transform the input into an integer. You will find the page on built-in functions at https://docs.python.org/3/library/functions.html very useful for figuring out which function to use!
Submit your program to the Moodle HW 3 PB 2 section.
Example of user interaction with the program, where the user enters 139.55:
Please enter amount to withdraw: 139.55
Withdrawal amount: 139
Please lift your keyboard and find:
6 $20-bill(s)
1 $10-bill(s)
1 $5-bill(s)
4 $1-bill(s)
Problem 3: The User sometimes inputs negative numbers
Make your program more robust so that if the user enters a minus sign in front of the number, by mistake, your program will discard it. In other words, if the user enters -30.50, your program will treat this number as 30. If the user enters -10, the program will treat it as 10.
Submit your program hw3.py program to the Moodle HW 3 PB 3 section.
Example of user interaction: this time the user enters -122.99
Please enter amount to withdraw: -122.99
Withdrawal amount: 122
Please lift your keyboard and find:
6 $20-bill(s)
0 $10-bill(s)
0 $5-bill(s)
2 $1-bill(s)
Problem 4: Teller Machine with $50-bills
Your new program, also called hw3.py, will now be able to accept negative numbers or floats as input, and will also output $50-bills, in addition to $20, $10, $5, and $1-bills.
Submit your program, called hw3.py, to the Moodle HW 3 PB 4 section.
Problem 5: Teller Machine That Returns Coins
This time the program accepts inputs of the form 123.55 and will output the change expressed in $20, $10, $5, and $1-bills, along with a number of quaters, dimes, nickels, and cents.
Submit your new version of hw3.py to the Moodle HW 3 PB 5 section.
Here is an example of an interaction with the user, who enters 124.46 as the amount to withdraw. We assume that the user may enter -124.46, in which case your program should treat that value as +124.46.
Please enter amount to withdraw: 124.46
Withdrawal amount: 124.46
Please lift your keyboard and find:
6 $20-bill(s)
0 $10-bill(s)
0 $5-bill(s)
4 $1-bill(s)
1 quarter(s)
2 dime(s)
0 nickel(s)
1 cent(s)
Problem 6: Formatted output
Imagine that we want to make the program super user-friendly, and have it output images of bills corresponding to the bills that will be given out by the machine. Our programs currently can't do graphics, but we can use a shortcut, and have the program output a string such as "[$20]" to represent a $20-bill.
Take your program for Problem 3 above, modify it, and make it output "icons" for the bills that will be given out.
An example will illustrate what your program should emulate:
Amount to withdraw? 91
4 $20-bill(s) [$20] [$20] [$20] [$20]
1 $10-bill(s) [$10]
0 $5-bill(s)
1 $1-bill(s) [$1]
Total = 6 bill(s): [$20] [$20] [$20] [$20] [$10] [$1]
Notice that the line for $20-bills indicates that 4 bills will be given out, and shows 4 strings [$20], indicating that the user will get 4 bills of this denomination. Make sure there is a space between the [$xx] strings, otherwise the automatic grader will give you an error.
Notice that the last line shows 6 different strings, indicating that 4 $20-, 1 $10-, and 1 $1-bill will be given out.
Here's another example of the way the program should behave, for a different input:
Amount to withdraw? 11
0 $20-bill(s)
1 $10-bill(s) [$10]
0 $5-bill(s)
1 $1-bill(s) [$1]
Total = 2 bill(s): [$10] [$1]
The number of spaces in your output is not important, but it must have a blank line between the input line and the output.
Submit your solution program in Section HW3 PB 6.
</showafterdate>
Solution Programs
<showafterdate after="20151002 00:" before="20151231 12:00">
# hw3Solutions.py
# D. Thiebaut
# solution programs for Homework #3
#
def main1():
# get amount
amount = int( input( "Please enter amount to withdraw: " ) )
originalAmout = amount
# compute no20s, no10s, no5s and no1s
no20s = amount // 20
amount = amount % 20
no10s = amount // 10
amount = amount % 10
no5s = amount // 5
no1s = amount % 5
# output result
print( "Withdrawal amount: ", originalAmout )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
def main2():
# get amount
amount = input( "Please enter amount to withdraw: " )
amount = int( eval( amount ) )
originalAmout = amount
# compute no20s, no10s, no5s and no1s
no20s = amount // 20
amount = amount % 20
no10s = amount // 10
amount = amount % 10
no5s = amount // 5
no1s = amount % 5
# output result
print( "Withdrawal amount: ", originalAmout )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
def main3():
# get amount
amount = input( "Please enter amount to withdraw: " )
amount = int( eval( amount ) )
amount = abs( amount )
originalAmout = amount
# compute no20s, no10s, no5s and no1s
no20s = amount // 20
amount = amount % 20
no10s = amount // 10
amount = amount % 10
no5s = amount // 5
no1s = amount % 5
# output result
print( "Withdrawal amount: ", originalAmout )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
def main4():
# get amount
amount = input( "Please enter amount to withdraw: " )
amount = int( eval( amount ) )
amount = abs( amount )
originalAmout = amount
# compute no20s, no10s, no5s and no1s
no50s = amount // 50
amount = amount % 50
no20s = amount // 20
amount = amount % 20
no10s = amount // 10
amount = amount % 10
no5s = amount // 5
no1s = amount % 5
# output result
print( "Withdrawal amount: ", originalAmout )
print( "Please lift your keyboard and find: " )
print( no50s, "$50-bill(s)" )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
def main5():
# get amount
amount = input( "Please enter amount to withdraw: " )
amount = eval( amount )
amount = abs( amount )
originalAmount = amount
# break down float into two ints, the dollar part, and
# the number of pennies.
intAmount = abs( int( amount ) )
pennies = abs( int ( round ( (amount - intAmount) * 100 ) ) )
# compute no20s, no10s, no5s and no1s
no20s = intAmount // 20
intAmount = intAmount % 20
no10s = intAmount // 10
intAmount = intAmount % 10
no5s = intAmount // 5
no1s = intAmount % 5
# computer quarters, dimes, nickels, cents
quarters = pennies // 25
pennies = pennies % 25
dimes = pennies // 10
pennies = pennies % 10
nickels = pennies // 5
cents = pennies % 5
# output result
print( "Withdrawal amount: ", originalAmount )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
print( quarters, "quarter(s)" )
print( dimes, "dime(s)" )
print( nickels, "nickel(s)" )
print( cents, "cent(s)" )
def main6():
# input section
amount = eval( input( "Amount? " ) )
print()
# computation
no20s = amount // 20
amount = amount % 20
no10s = amount // 10
amount = amount % 10
no5s = amount // 5
amount = amount % 5
no1s = amount
# display results
print( "{0:5} $20-bill(s) ".format( no20s ), "[$20] "*no20s, sep="" )
print( "{0:5} $10-bill(s) ".format( no10s ), "[$10] "*no10s, sep="" )
print( "{0:5} $5-bill(s) ".format( no5s ), end="", sep="" )
for i in range( no5s ):
print( "[$5] ", sep="", end="" )
print()
print( "{0:5} $1-bill(s) ".format( no1s ), end="", sep="" )
for i in range( no1s ):
print( "[$1] ", sep="", end="" )
print()
print( "Total =", no20s+no10s+no5s+no1s, "bills:",
"[$20] "*no20s, "[$10] "*no10s, "[$5] "*no5s, "[$1] " * no1s )
main1()
main2()
main3()
main4()
main6()
main7()
</showafterdate>