|
|
Line 149: |
Line 149: |
| <br /> | | <br /> |
| <source lang="python"> | | <source lang="python"> |
| + | |
| # hw4sol.py | | # hw4sol.py |
| # D. Thiebaut | | # D. Thiebaut |
Line 162: |
Line 163: |
| fName = input( "Your first name? " ) | | fName = input( "Your first name? " ) |
| lName = input( "Your last name? " ) | | lName = input( "Your last name? " ) |
| + | print( ) |
| full = fName[0] + lName | | full = fName[0] + lName |
| full = full.lower() | | full = full.lower() |
− | print( full) | + | print( "Your computer account: {0:1}{1:1}".format( full, len(full) ) ) |
| | | |
| # solution for Problem 2 | | # solution for Problem 2 |
Line 235: |
Line 237: |
| | | |
| | | |
− | # solution for Problem 7
| + | |
− | def problem7():
| |
− | | |
− | # get the input from the user
| |
− | fName = input( "Enter first name: " )
| |
− | lName = input( "Enter last name: " )
| |
− | dob = input( "Date of birth (yyyymmdd)" )
| |
− | dod = input( "Date of death (yyyymmdd)" )
| |
− | accomp = input( "Enter accomplishment: " )
| |
− | | |
− | | |
− | # Figure out the length of the bar
| |
− | bar = "=" * len( accomp )
| |
− | | |
− | # reformat names
| |
− | fName = fName.capitalize()
| |
− | lName = lName.capitalize()
| |
− | accomp = accomp.capitalize()
| |
− | | |
− | # use a list of strings for the month, to get
| |
− | # the correct month for a give number.
| |
− | months = ["January", "February", "March",
| |
− | "April", "May", "June",
| |
− | "July", "August", "September",
| |
− | "October", "November", "December" ]
| |
− | | |
− | # extract day, month and year from dates.
| |
− | dobDay = int( dob[-2:] )
| |
− | dobYear = dob[0:4]
| |
− | dobMonth= int( dob[4:6] ) - 1
| |
− | dobMonth= months[ dobMonth ]
| |
− | | |
− | dodDay = int( dod[-2:] )
| |
− | dodYear = dod[0:4]
| |
− | dodMonth= int( dod[4:6] ) - 1
| |
− | dodMonth= months[ dodMonth ]
| |
− | | |
− | # output the result, nicely formatted
| |
− | #print()
| |
− | print( bar )
| |
− | line1 = "{0:1} {1:1} ({2:1} {3:1}, {4:1} -- {5:1} {6:1}, {7:1})"
| |
− | print( line1.format( fName, lName, dobMonth, dobDay, dobYear,
| |
− | dodMonth, dodDay, dodYear ) )
| |
− | print( accomp )
| |
− | print( bar + "\n" )
| |
− | | |
− | | |
| # main program. | | # main program. |
| # Calls all the functions that solve the different problems. | | # Calls all the functions that solve the different problems. |
Line 288: |
Line 244: |
| | | |
| def main(): | | def main(): |
− | #problem1() | + | problem1() |
| #problem2() | | #problem2() |
| #problem3() | | #problem3() |
Line 294: |
Line 250: |
| #problem5() | | #problem5() |
| #problem6() | | #problem6() |
− | problem7()
| |
| | | |
| main() | | main() |
| + | |
| | | |
| | | |
Line 305: |
Line 261: |
| </showafterdate> | | </showafterdate> |
| <br /> | | <br /> |
− | <onlydft>
| |
− | =VPL Module=
| |
− | <br />
| |
− | ==Problem 1==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "ALMA" "zella" "Maria" )
| |
− | TEMP2=( "BRAVO" "BONO" "smith" )
| |
− |
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1 and \$2"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -1 > dummy.out
| |
− | cp dummy.out \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− | echo "\${TEMP1[i]}" > input
| |
− | echo "\${TEMP2[i]}" >> input
| |
− |
| |
− | #echo "\${TEMP1[i]}"
| |
− | #echo "\${TEMP2[i]}"
| |
− |
| |
− | # =================================================
| |
− | # 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
| |
− |
| |
− | keepLast userOut
| |
− | keepLast expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \${TEMP1[i]} \${TEMP2[i]}
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | ==Problem 2==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− |
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "ALMA" "zella" "M" )
| |
− | TEMP2=( "BRAVO" "BONO" "s" )
| |
− |
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1 and \$2"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -1 > dummy.out
| |
− | cp dummy.out \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− | echo "\${TEMP1[i]}" > input
| |
− | echo "\${TEMP2[i]}" >> input
| |
− |
| |
− | #echo "\${TEMP1[i]}"
| |
− | #echo "\${TEMP2[i]}"
| |
− |
| |
− | # =================================================
| |
− | # 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
| |
− |
| |
− | keepLast userOut
| |
− | keepLast expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \${TEMP1[i]} \${TEMP2[i]}
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | ==Problem 3==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "ALMA" "zella" "M" )
| |
− | TEMP2=( "BRAVO" "BONO" "s" )
| |
− |
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1 and \$2"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -1 > dummy.out
| |
− | cp dummy.out \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− |
| |
− | testNo=\$((i+1))
| |
− | echo "Comment :=>>- Test \$testNo"
| |
− |
| |
− | echo "\${TEMP1[i]}" > input
| |
− | echo "\${TEMP2[i]}" >> input
| |
− |
| |
− | #echo "\${TEMP1[i]}"
| |
− | #echo "\${TEMP2[i]}"
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input 2> errorOut 1> userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | if [ -s errorOut ] ; then
| |
− | echo "Comment :=>> Your program crashed..."
| |
− | echo "Comment :=>> The input was"
| |
− | echo "<|--"
| |
− | cat input
| |
− | echo "--|>"
| |
− | echo "Comment :=>> Error message:"
| |
− | echo "<|--"
| |
− | cat errorOut
| |
− | echo "--|>"
| |
− | continue
| |
− | fi
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | #cleanup userOut
| |
− | #cleanup expectedOut
| |
− |
| |
− | keepLast userOut
| |
− | keepLast expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \${TEMP1[i]} \${TEMP2[i]}
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | ==Problem 4==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | if [[ `hostname -s` = "beowulf2" ]]; then
| |
− | python=/usr/bin/python3.3
| |
− | else
| |
− | python=/usr/local/bin/python3.4
| |
− | fi
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "CHOCO" "z" "s" )
| |
− | TEMP2=( "LATE" "ebra" "o" )
| |
− | TEMP3=( 9 5 2 )
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1 and \$2"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -\$2 > dummy.out
| |
− | cp dummy.out \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− | echo "\${TEMP1[i]}" > input
| |
− | echo "\${TEMP2[i]}" >> input
| |
− |
| |
− | testNo=\$((i+1))
| |
− | echo "Comment :=>>- Test \$testNo"
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$solutionProg < input > expectedOut
| |
− | \$python \$prog < input 2> errorOut 1> userOut
| |
− |
| |
− | if [ -s errorOut ] ; then
| |
− | echo "Comment :=>> Your program crashed..."
| |
− | echo "Comment :=>> The input was"
| |
− | echo "<|--"
| |
− | cat input
| |
− | echo "--|>"
| |
− | echo "Comment :=>> Error message:"
| |
− | echo "<|--"
| |
− | cat errorOut
| |
− | echo "--|>"
| |
− | continue
| |
− | fi
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | #cleanup userOut
| |
− | #cleanup expectedOut
| |
− |
| |
− | keepLast userOut \${TEMP3[i]}
| |
− | keepLast expectedOut \${TEMP3[i]}
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \$TEMP1 \$TEMP2
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | ==Problem 5==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− |
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | if [[ `hostname -s` = "beowulf2" ]]; then
| |
− | python=/usr/bin/python3.3
| |
− | else
| |
− | python=/usr/local/bin/python3.4
| |
− | fi
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "02142015" "01311900" "12012000" )
| |
− | #TEMP1=( "02142015" ) # "01311900" "12012000" )
| |
− |
| |
− | # =================================================
| |
− | # function that looks for forbidden patterns in the program
| |
− | testForPatterns() {
| |
− |
| |
− | requiredPattern="\"January\""
| |
− | forbiddenPattern="\"Jan\""
| |
− |
| |
− | # remove comments
| |
− | cat \$prog | sed 's:#.*$::g' > user.temp
| |
− |
| |
− | # look for required patterns
| |
− | grep "\$requiredPattern" user.temp &> grepLines1.out
| |
− |
| |
− | if [ -s grepLines1.out ] ; then
| |
− | nothing="happens"
| |
− | else
| |
− | echo "Comment :=>> Your program must use months"
| |
− | echo "Comment :=>> expressed as full names (e.g. \"February\")."
| |
− | echo "<|--"
| |
− | cat grepLines1.out
| |
− | echo "--|>"
| |
− | echo "Grade :=>> 50"
| |
− | exit
| |
− | fi
| |
− |
| |
− | grep "\$forbiddenPattern" user.temp &> grepLines.out
| |
− | if [ -s grepLines.out ] ; then
| |
− | echo "Comment :=>> You cannot use strings of 3 characters"
| |
− | echo "Comment :=>> for the months."
| |
− | echo "Grade :=>> 50"
| |
− | exit
| |
− | fi
| |
− | }
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -3 | tr '\n' ' ' > dummy.out
| |
− | cp dummy.out \$1
| |
− | echo "" >> \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− | echo "\${TEMP1[i]}" > input
| |
− | #echo "input = "
| |
− | #cat input
| |
− |
| |
− | testNo=\$((i+1))
| |
− | echo "Comment :=>>- Test \$testNo"
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$prog < input 2> errorOut 1> userOut
| |
− | \$python \$solutionProg < input > expectedOut
| |
− |
| |
− | if [ -s errorOut ] ; then
| |
− | echo "Comment :=>> Your program crashed..."
| |
− | echo "Comment :=>> The input was"
| |
− | echo "<|--"
| |
− | cat input
| |
− | echo "--|>"
| |
− | echo "Comment :=>> Error message:"
| |
− | echo "<|--"
| |
− | cat errorOut
| |
− | echo "--|>"
| |
− | continue
| |
− | fi
| |
− |
| |
− | # test to see if the program uses the right
| |
− | # strings for the months.
| |
− | testForPatterns
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | #cleanup userOut
| |
− | #cleanup expectedOut
| |
− |
| |
− | keepLast userOut
| |
− | keepLast expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \$TEMP1
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | ==Problem 6==
| |
− | <br />
| |
− | ===vpl_run.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | python=/usr/local/bin/python3.4
| |
− | #python=/usr/bin/python3.3
| |
− |
| |
− | prog=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− |
| |
− | #! /bin/bash
| |
− | # D. Thiebaut
| |
− |
| |
− | cat > vpl_execution <<EEOOFF
| |
− | #! /bin/bash
| |
− | #set -x
| |
− |
| |
− | # --- program tested (no extension) ---
| |
− | prog=hw4.py
| |
− | solutionProg=hw4sol.py
| |
− |
| |
− | # --- Python ----
| |
− | if [[ `hostname -s` = "beowulf2" ]]; then
| |
− | python=/usr/bin/python3.3
| |
− | else
| |
− | python=/usr/local/bin/python3.4
| |
− | fi
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # Pick 3 random inputs to test program with
| |
− | TEMP1=( "02142015" "01311900" "12012000" )
| |
− |
| |
− | # =================================================
| |
− | # function that looks for forbidden patterns in the program
| |
− | testForPatterns() {
| |
− |
| |
− | requiredPattern="\"JanFebMarApr"
| |
− | forbiddenPattern="\"Jan\""
| |
− |
| |
− | # remove comments
| |
− | cat \$prog | sed 's:#.*$::g' > user.temp
| |
− |
| |
− | # look for required patterns
| |
− | grep "\$requiredPattern" user.temp &> grepLines1.out
| |
− |
| |
− | if [ -s grepLines1.out ] ; then
| |
− | nothing="happens"
| |
− | else
| |
− | echo "Comment :=>> Your program must use months"
| |
− | echo "Comment :=>> expressed as full names (e.g. \"February\")."
| |
− | echo "<|--"
| |
− | cat grepLines1.out
| |
− | echo "--|>"
| |
− | echo "Grade :=>> 50"
| |
− | exit
| |
− | fi
| |
− |
| |
− | grep "\$forbiddenPattern" user.temp &> grepLines.out
| |
− | if [ -s grepLines.out ] ; then
| |
− | echo "Comment :=>> You cannot use strings of 3 characters"
| |
− | echo "Comment :=>> for the months."
| |
− | echo "Grade :=>> 50"
| |
− | exit
| |
− | fi
| |
− | }
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # 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 \$1 and \$2"
| |
− | echo "--|>"
| |
− |
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Your output:"
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | echo ""
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "Comment :=>>- Expected output: "
| |
− | echo "Comment :=>> ---------------"
| |
− | echo "<|--"
| |
− | cat expectedOut
| |
− | 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 puts all words in file on separate lines
| |
− | # and keep last line
| |
− | keepLast ( ) {
| |
− | if [ "\$1" ]; then # if there's a parameter
| |
− | cat \$1 | tr ' ' '\n' | sed '/^\s*\$/d' > dummy.out
| |
− | cp dummy.out \$1
| |
− |
| |
− | cat \$1 | tail -3 | tr '\n' ' ' > dummy.out
| |
− | cp dummy.out \$1
| |
− | echo "" >> \$1
| |
− | 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
| |
− | }
| |
− |
| |
− | # initial grade
| |
− | grade=40
| |
− |
| |
− | for ((i=0;i<\${#TEMP1[@]};i++)); do
| |
− | echo "\${TEMP1[i]}" > input
| |
− |
| |
− | testNo=\$((i+1))
| |
− | echo "Comment :=>>- Test \$testNo"
| |
− |
| |
− |
| |
− | # =================================================
| |
− | # generate user output and exptect output
| |
− | \$python \$solutionProg < input > expectedOut
| |
− | \$python \$prog < input 2> errorOut 1> userOut
| |
− |
| |
− | if [ -s errorOut ] ; then
| |
− | echo "Comment :=>> Your program crashed..."
| |
− | echo "Comment :=>> The input was"
| |
− | echo "<|--"
| |
− | cat input
| |
− | echo "--|>"
| |
− | echo "Comment :=>> Error message:"
| |
− | echo "<|--"
| |
− | cat errorOut
| |
− | echo "--|>"
| |
− | continue
| |
− | fi
| |
− |
| |
− | # test to see if the program uses the right
| |
− | # strings for the months.
| |
− | testForPatterns
| |
− |
| |
− | cp userOut userOut.org
| |
− | cp expectedOut expectedOut.org
| |
− |
| |
− | #cleanup userOut
| |
− | #cleanup expectedOut
| |
− |
| |
− | keepLast userOut
| |
− | keepLast expectedOut
| |
− |
| |
− | #--- compute difference ---
| |
− | diff -y -w --ignore-all-space userOut expectedOut > diff.out
| |
− |
| |
− | #--- reject if different ---
| |
− | if ((\$? > 0)); then
| |
− | incorrectOutput \$TEMP1
| |
− | grade=\$((grade+5))
| |
− | # --------------------- REWARD IF CORRECT OUTPUT -----------------
| |
− | else
| |
− | #--- good output ---
| |
− | echo "Comment :=>>- Congrats, your output is correct."
| |
− | echo "Comment :=>> --------------------------------."
| |
− | echo "<|--"
| |
− | echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
| |
− | echo ""
| |
− | cat userOut
| |
− | echo "--|>"
| |
− | grade=\$((grade+20))
| |
− | fi
| |
− |
| |
− | done
| |
− |
| |
− | # =================================================
| |
− | # Report grade
| |
− | if (( grade > 100 )); then
| |
− | grade=100
| |
− | fi
| |
− | echo "Grade :=>> \$grade"
| |
− |
| |
− |
| |
− | exit
| |
− |
| |
− |
| |
− |
| |
− |
| |
− | EEOOFF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− | ==Problem 7==
| |
− | <br />
| |
− | ===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=hw4.py
| |
− | \$python \$prog
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− | </source>
| |
− | <br />
| |
− | ===vpl_evaluate.sh===
| |
− | <br />
| |
− | <source lang="bash">
| |
− | #! /bin/bash
| |
− |
| |
− | cat > vpl_execution <<EOF
| |
− | #! /bin/bash
| |
− |
| |
− | # --- Python ----
| |
− | if [[ `hostname -s` = "beowulf2" ]]; then
| |
− | python=/usr/bin/python3.3
| |
− | else
| |
− | python=/usr/local/bin/python3.4
| |
− | fi
| |
− |
| |
− |
| |
− |
| |
− | \$python evaluate.py
| |
− |
| |
− | EOF
| |
− |
| |
− | chmod +x vpl_execution
| |
− |
| |
− | </source>
| |
− | <br />
| |
− | ===evaluate.py===
| |
− | <br />
| |
− | <source lang="python">
| |
− | # evaluate.py
| |
− | # D. Thiebaut
| |
− | import sys
| |
− | import random
| |
− | import subprocess
| |
− |
| |
− | #--- define what the student program is called, and what the solution
| |
− | #--- program name is.
| |
− | module = "hw4"
| |
− | solutionModule = "hw4sol"
| |
− | stripOutputsBeforeCompare = True # set to true if extra space in output is not important
| |
− |
| |
− | def commentLong( line ):
| |
− | print( "<|--\n" + line + "\n --|>" )
| |
− |
| |
− | def commentShort( text ):
| |
− | print( "Comment :=>> " + text )
| |
− |
| |
− | def printGrade( grade ):
| |
− | print( "Grade :=>> ", grade )
| |
− |
| |
− | def checkForFunctionPresence( module, functionName ):
| |
− | foundDef = False
| |
− | foundCall = False
| |
− |
| |
− | for line in open( module+".py", "r" ).readlines():
| |
− | # remove comments
| |
− | idx = line.find( "#" )
| |
− | if ( idx >=0 ): line = line[0:idx]
| |
− |
| |
− | if line.startswith( "def " + functionName + "(" ):
| |
− | foundDef = True
| |
− | continue
| |
− | if line.startswith( "def " + functionName + " (" ):
| |
− | foundDef = True
| |
− | continue
| |
− | if line.find( functionName+"(" ) != -1:
| |
− | foundCall = True
| |
− | continue
| |
− |
| |
− | return (foundDef, foundCall)
| |
− |
| |
− | # checkModuleRunsOK: runs the module as a shell subprocess and
| |
− | # look for errors in the output. This is required, because otherwise
| |
− | # importing the module in this program will make this program crash.
| |
− | # It's not possible (as far as I can tell0 to catch exceptions from
| |
− | # the import or __module__ statements.
| |
− | # returns True, none if no errors, otherwise False, string if there's
| |
− | # an exception, and the error message (string) is in the returned 2nd
| |
− | # arg.
| |
− | # The module name is assumed to not include ".py"
| |
− | def checkModuleRunsOk( module, inputFileName ):
| |
− | p = subprocess.Popen( [ "python3", module+".py" ],
| |
− | stdout=subprocess.PIPE,
| |
− | stderr=subprocess.PIPE,
| |
− | stdin=subprocess.PIPE)
| |
− |
| |
− | p.stdin.write( bytes( open( inputFileName, "r" ).read(), 'UTF-8' ) )
| |
− | data = p.communicate( )
| |
− | p.stdin.close()
| |
− |
| |
− | error = data[1].decode( 'UTF-8' )
| |
− | if len( error ) > 1:
| |
− | return False, error
| |
− | return True, None
| |
− |
| |
− |
| |
− | def generateInputFileWithRandomInputs( inputFileName ):
| |
− | #--- generate random inputs ---
| |
− | dates1 = [ "17960807", "19010111", "19450501", "19640815", "19640815" ]
| |
− | dates2 = [ "20140114", "20100903", "20090218", "20040109", "20040119" ]
| |
− | names1 = [ "Flip", "Min", "Lei", "Sophia", "Gloria", "Ann" ]
| |
− | names2 = [ "Thomas", "Gold", "Lundt", "Blake", "Ramdath" ]
| |
− | accoms = [ "Invented sliced bread",
| |
− | "Flew over Paradise Pond with a human powered glider",
| |
− | "Created the first encylopedia of chocolate",
| |
− | "Graduated summa cum laude" ]
| |
− |
| |
− | idx = random.randrange( len( dates1 ) )
| |
− | date1 = dates1[idx]
| |
− | date2 = dates2[idx]
| |
− | name1 = random.choice( names1 )
| |
− | name2 = random.choice( names2 )
| |
− | accom = random.choice( accoms )
| |
− |
| |
− | #--- create input file with input data ---
| |
− | file = open( inputFileName, "w" )
| |
− | text = ""
| |
− | for word in ( name1, name2, date1, date2, accom ):
| |
− | file.write( word+"\n" )
| |
− | text += word + "\n"
| |
− | file.close()
| |
− | return text
| |
− |
| |
− | # extractTextFromErrorMessage( sys_exc_info ):
| |
− | def extractTextFromErrorMessage( sys_exc_info ):
| |
− | print( "sys_exec_info = ", sys_exc_info )
| |
− | text = ""
| |
− | for field in sys_exc_info:
| |
− | if type( field )==type( " " ):
| |
− | text += field + "\n"
| |
− | return text
| |
− |
| |
− | # runModule:
| |
− | # runs the module, passes it data from the input file on its stdin
| |
− | # and get its output on stdout captured in outputFileName.
| |
− | # We assume the module will not crash, because we already tested
| |
− | # it with checkModuleRunsOk().
| |
− | def runModule( module, inputFileName, outputFileName ):
| |
− | error = False
| |
− |
| |
− | #--- make stdin read information from the text file
| |
− | sys.stdin = open( inputFileName, "r" )
| |
− |
| |
− | #--- capture the stdout of the program to test into a file
| |
− | saveStdOut = sys.stdout
| |
− | saveStdErr = sys.stderr
| |
− |
| |
− | sys.stdout = open( outputFileName, "w" )
| |
− | sys.stderr = open( "errorOut", "w" )
| |
− |
| |
− | #--- run the student program ---
| |
− | try:
| |
− | _module = __import__( module )
| |
− | except:
| |
− | error = True
| |
− | sys.stderr.close()
| |
− | sys.stderr = saveStdErr
| |
− | sys.stdout.close()
| |
− | sys.stdout = saveStdOut
| |
− | text = sys.exc_info()[0]
| |
− | text = extractTextFromErrorMessage( text )
| |
− | print( "*** sys.exc_info() = ", text )
| |
− | return error, text
| |
− |
| |
− | #--- filter out junk from output of program ---
| |
− | sys.stdout.close()
| |
− | sys.stdout = saveStdOut
| |
− | file = open( outputFileName, "r" )
| |
− | text = file.read()
| |
− | index = text.find( "=" )
| |
− | text = text[index:]
| |
− | text = text.strip( ).strip( "\n" ) + "\n"
| |
− | #print( text, end="" )
| |
− | file.close()
| |
− | return False, text
| |
− |
| |
− | def removeBlankLines( lines ):
| |
− | newLines = []
| |
− | for line in lines.split( "\n" ):
| |
− | if len( line )==0:
| |
− | continue
| |
− | newLines.append( line )
| |
− |
| |
− | return ( "\n".join( newLines ) ) + "\n"
| |
− |
| |
− | def compareUserExpected( inputLines, userOutText, expectedOutText ):
| |
− | global stripOutputsBeforeCompare
| |
− |
| |
− | userOutText = removeBlankLines( userOutText )
| |
− | expectedOutText = removeBlankLines( expectedOutText )
| |
− | #print( "userOutText:" + userOutText )
| |
− | #print( "expectedOutText:" + expectedOutText )
| |
− | misMatchLineNumbers = []
| |
− | userTextOutLines = userOutText.split( "\n" )
| |
− | expectedOutTextLines = expectedOutText.split( "\n" )
| |
− |
| |
− | for i in range( len( userTextOutLines ) ):
| |
− | lineNo = i+1
| |
− | userLine = userTextOutLines[i]
| |
− | expectedLine = userTextOutLines[i]
| |
− | if stripOutputsBeforeCompare:
| |
− | userLine = userLine.strip()
| |
− | expectedLine = expectedLine.strip()
| |
− | if userLine != expectedLine:
| |
− | #print( "user >" + userTextOutLines[i] + "<" )
| |
− | #print( "expected >" + expectedOutTextLines[i] + "<" )
| |
− | misMatchLineNumbers.append( lineNo )
| |
− |
| |
− | return misMatchLineNumbers
| |
− |
| |
− |
| |
− |
| |
− | def main():
| |
− | global module
| |
− | global solutionModule
| |
− |
| |
− |
| |
− | #--- check that the main module uses a main() function
| |
− | foundDef, foundCall = checkForFunctionPresence( module, "main" )
| |
− | if (not foundDef) or (not foundCall):
| |
− | commentShort( "-Missing main() program" )
| |
− | commentShort( "Your program must use a main() function." )
| |
− | printGrade( 40 )
| |
− | return
| |
− |
| |
− |
| |
− | #--- generate input file with random data ---
| |
− | inputLines = generateInputFileWithRandomInputs( "input" )
| |
− |
| |
− | Ok, errorMessage = checkModuleRunsOk( module, "input" )
| |
− | if not Ok:
| |
− | commentLong( "- Your program crashed...\n"
| |
− | + "Your program was tested with:\n"
| |
− | + inputLines + "\n"
| |
− | + "Error message:\n"
| |
− | + errorMessage + "\n" )
| |
− | printGrade( 50 )
| |
− | return
| |
− |
| |
− |
| |
− | error, userOutText = runModule( module, "input", "userOut" )
| |
− | if error:
| |
− | commentLong( "- Your program crashed...\n"
| |
− | + "Your program was tested with:\n"
| |
− | + inputLines + "\n"
| |
− | + "Error message:\n"
| |
− | + userOutText + "\n" )
| |
− | printGrade( 50 )
| |
− | return
| |
− |
| |
− | dummy, expectedOutText = runModule( solutionModule, "input", "expectedOut" )
| |
− |
| |
− | misMatchLineNumbers = compareUserExpected( inputLines,
| |
− | userOutText,
| |
− | expectedOutText )
| |
− | if len( misMatchLineNumbers ) == 0:
| |
− | commentLong( "- Congrats, correct output!\n"
| |
− | +"Your program was tested with:\n"
| |
− | +inputLines + "\n"
| |
− | +"Your output:\n"
| |
− | +userOutText + "\n" )
| |
− | printGrade( 100 )
| |
− | else:
| |
− | s = "" if len( misMatchLineNumbers ) == 1 else "s"
| |
− |
| |
− | commentLong( "-Incorrect output.\n"
| |
− | +"Your program was tested with:\n"
| |
− | +inputLines + "\n"
| |
− | +"Your output:\n"
| |
− | +userOutText + "\n"
| |
− | +"Expected output:\n"
| |
− | +expectedOutText + "\n"
| |
− | +("There are differences in Line%s " % s) +
| |
− | ", ".join( [str(k) for k in misMatchLineNumbers] )
| |
− | +"\n" )
| |
− | noLinesGood = len( expectedOutText.strip().split("\n") )
| |
− | noErrors = len( misMatchLineNumbers )
| |
− | #print( "noLinesGood = ", noLinesGood, " noErrors = ", noErrors )
| |
− |
| |
− | grade = round( 60 + 40 / noLinesGood *( noLinesGood- noErrors ) )
| |
− | printGrade( grade )
| |
− |
| |
− | main()
| |
− |
| |
− |
| |
− | </source>
| |
− | <br />
| |
− |
| |
− |
| |
− | </onlydft>
| |
| <br /> | | <br /> |
| <br /> | | <br /> |