Difference between revisions of "CSC111 Homework 2 2015"

From dftwiki3
Jump to: navigation, search
(Problem 2)
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
 
<bluebox>
 
<bluebox>
The due date for this homework is Tuesday 2/10 at 11:55 p.m.
+
This homework will be available on 2/5 at 5 p.m., right after the last lab section.  The due date for this homework is Tuesday 2/10 at 11:55 p.m.
 
</bluebox>
 
</bluebox>
 +
<br />
 +
<showafterdate after="20150205 17:00" before="20150601 00:00">
  
 
<br />
 
<br />
Line 14: Line 16:
 
  Please enter the starting temperature:  '''30'''
 
  Please enter the starting temperature:  '''30'''
 
  How many lines do you want for the table?  '''10'''
 
  How many lines do you want for the table?  '''10'''
 
+
 +
Fahrenheit Celsius
 
  30        -1.11111111111
 
  30        -1.11111111111
 
  31        -0.555555555556
 
  31        -0.555555555556
Line 31: Line 34:
 
  How many lines do you want for the table?  '''4'''
 
  How many lines do you want for the table?  '''4'''
 
    
 
    
 +
Fahrenheit Celsius
 
  32        0.0
 
  32        0.0
 
  33        0.555555555556
 
  33        0.555555555556
Line 36: Line 40:
 
  35        1.66666666667
 
  35        1.66666666667
  
 +
* Make sure to label the columns with " Fahrenheit" and  "Celsius" please!
 
* Submit your program on Moodle, in the HW2 PB1 section.
 
* Submit your program on Moodle, in the HW2 PB1 section.
 
<br />
 
<br />
Line 62: Line 67:
 
And here is another example:
 
And here is another example:
  
  Please enter the starting temperature:  '''38'''
+
  Please enter the starting temperature:  '''32'''
  How many lines do you want for the table?  '''4'''
+
  How many lines do you want for the table?  '''3'''
 
  What step should be used? '''2'''  
 
  What step should be used? '''2'''  
 
    
 
    
  38         3.33333333333
+
  32         0.0
  37         2.77777777778
+
  34         1.11111111111
 
  36        2.22222222222
 
  36        2.22222222222
35        1.66666666667
 
  
  
Line 109: Line 113:
 
Here is an example of how your program should behave:
 
Here is an example of how your program should behave:
 
   
 
   
  Enter low temperature: 30
+
  Enter low temperature in Fahrenheit: 30
 
  How many lines? 6
 
  How many lines? 6
 
   
 
   
  Fahrenheit Celsius Kelvin
+
  Kelvin Celsius Fahrenheit
 
  272.038888889 -1.11111111111 30
 
  272.038888889 -1.11111111111 30
 
  272.594444444 -0.555555555556 31
 
  272.594444444 -0.555555555556 31
Line 124: Line 128:
 
<br />
 
<br />
 
<br />
 
<br />
=Problem 5: Triangles=
+
 
 +
=Problem 5: Loops and Triangles=
 
<br />
 
<br />
  
Line 151: Line 156:
 
</pre></code>
 
</pre></code>
  
Using this information, write a new program  that uses a for-loop to display the following triangle:
+
Using this information, write a new program  that uses one for-loop to display the following triangle:
 
<code><pre>
 
<code><pre>
 
*
 
*
Line 184: Line 189:
 
   
 
   
 
When your program works well, you are ready to write the program you need to submit.
 
When your program works well, you are ready to write the program you need to submit.
 
+
<br />
Your new program will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.
+
==Your Assignment==
 +
<br />
 +
Write a new program that will prompt the user for a number and use this number to print two triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.
  
 
Here is an example of how the program will work when the user enters 5:
 
Here is an example of how the program will work when the user enters 5:
Line 191: Line 198:
  
 
  How many lines? <u>5</u>
 
  How many lines? <u>5</u>
+
 
 
  triangle 1
 
  triangle 1
 
   
 
   
Line 209: Line 216:
 
   
 
   
 
<br />
 
<br />
Note that all the triangles are flush against the left margin.
+
Note that all the triangles are flush against the left margin.  
  
 
Submit your program to the HW2 PB5 section on Moodle.
 
Submit your program to the HW2 PB5 section on Moodle.
 
<br />
 
<br />
 +
</showafterdate>
 +
<br />
 +
 +
<showafterdate after="20150211 00:00" before="20150601 00:00">
 +
=Solution Program=
 +
<br />
 +
<source lang="python">
 +
# hw2.py
 +
# D. Thiebaut
 +
# This program contains the solution programs for Homework 2.
 +
# The different solutions are stored in separate functions.  Do not
 +
# worry about using functions.  You didn't have to use functions
 +
# for this assignment.  All you have to do is look at the code inside
 +
# a function to see what you were supposed to do.
 +
 +
# This is a solution for Lab 2
 +
def prompt1():
 +
    fname = input( "your first name?  " )
 +
    lname = input( "your last name?    " )
 +
    box  = input( "your box number?  " )
 +
    phone = input( "your phone number? " )
 +
    Id    = input( "your Id number?    " )
 +
 +
    print( "+---------------------------------" )
 +
    print( "| Name:  ", fname, lname, "(", Id, ")" )
 +
    print( "| Box    ", box )
 +
    print( "| Phone: ", phone )
 +
    print( "+---------------------------------" )
 +
 +
# another solution for Lab 2
 +
def prompt2():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    temp2 = eval( input( "Enter high temperature: " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp2+1 ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
 +
# Solution for Problem 1
 +
# The code asks the user for 2 numbers and displays a table
 +
# of temperatures in Fahrenheit and Celsius
 +
def hw2_1():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
   
 +
# Solution for Problem 2
 +
# The code asks the user for 3 numbers and displays a table
 +
# of temperatures in Fahrenheit and Celsius.
 +
# The 3 numbers are the starting temperature, the number
 +
# of lines, and the step between Fahrenheit temperatures.
 +
def hw2_2():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    step = eval( input( "Step? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp1 + step*noLines, step ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
   
 +
# Solution for Problem 3.  The program asks for 2 numbers
 +
# and displays a table of temperatures, with Fahrenheit,
 +
# Celsius and Kelvin
 +
def hw2_3():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius Kelvin" )
 +
 +
    # display the table
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        celsius = (temp-32)*5/9
 +
        kelvin = celsius + 273.15
 +
        print( temp, "      ", celsius,  kelvin )
 +
 +
# Solution for Problem 4.  The program asks for 2 numbers
 +
# and displays a table of temperatures, with Kelvin,
 +
# Celsius and Fahrenheit. 
 +
def hw2_4():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
 +
    # display the table
 +
    print( "Kelvin Celsius Fahrenheit" )
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        celsius = (temp-32)*5/9
 +
        kelvin = celsius + 273.15
 +
        print( kelvin, celsius, temp )
 +
 +
# Solution for Problem 5
 +
# Asks the user for a number of lines of stars
 +
# and displays 2 triangles, one with the tip at the top
 +
# and the second one with the tip at the bottom.
 +
def hw2_5():
 +
    noStars = eval( input( "How many lines of stars? " ) )
 +
    print()
 +
   
 +
    # display first trianble
 +
    print( "triangle 1" )
 +
    print()
 +
    for n in range( 1, noStars+1 ):
 +
        print( n * '*' )
 +
    print()
 +
 +
    # display second trianble
 +
    print( "triangle 2" )
 +
    print()
 +
    for n in range( noStars, 0, -1 ):
 +
        print( n * '*' )
 +
    print()
 +
   
 +
# --------------------------------------------------------------------
 +
# uncomment the line below that corresponds
 +
# to the solution you want to run
 +
# --------------------------------------------------------------------
 +
#hw2_1()
 +
#hw2_2()
 +
#hw2_3()
 +
#hw2_4()
 +
hw2_5()
 +
</source>
 +
</showafterdate>
 +
 +
<br />
 +
<onlydft>
 +
=Moodle VPL Module=
 +
<br />
 +
All the modules are variations of each other.  Only the vpl_evaluate.sh differs.
 +
==vpl_run.sh==
 +
<source lang="bash">
 +
#! /bin/bash
 +
 +
cat > vpl_execution <<EOF
 +
#! /bin/bash
 +
 +
prog=hw2.py
 +
python=/usr/local/bin/python3.4
 +
#python=/usr/bin/python3.3
 +
 +
\$python \$prog
 +
 +
EOF
 +
 +
chmod +x vpl_execution
 +
</source>
 +
<br />
 +
==vpl_evaluate.sh  for Problem 1==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
#set -x
 +
 +
# --- program tested (no extension) ---
 +
prog=hw2_1.py
 +
solutionProg=hw2sol.py
 +
 +
# --- Python ----
 +
#python=/usr/local/bin/python3.4
 +
python=/usr/bin/python3.3
 +
 +
# =================================================
 +
# Pick 2 random inputs to test program with
 +
TEMP1=\$RANDOM
 +
let "TEMP1 %= 30"
 +
let "NOLINES = 6"
 +
let "TEMP2 = TEMP1 + NOLINES"
 +
 +
#echo "TEMP1 = \$TEMP1"
 +
#echo "NOLINES = \$NOLINES"
 +
 +
echo \$TEMP1 > input
 +
echo \$NOLINES >> input
 +
 +
# =================================================
 +
# generate user output and exptect output
 +
\$python \$prog < input > userOut
 +
\$python \$solutionProg < input > expectedOut
 +
 +
 +
# =================================================
 +
# 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"
 +
        echo "temperatures starting from"
 +
        echo "\$TEMP1 with \$NOLINES lines"
 +
        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 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
 +
        mv dummy.out \$1
 +
 +
        #--- remove multiple spaces ---
 +
        cat \$1 | sed 's/  */ /g' > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove blank lines ---
 +
        cat \$1 | sed '/^\s*\$/d' > dummy.out
 +
        mv dummy.out \$1
 +
  fi
 +
}
 +
 +
cleanup userOut
 +
cleanup expectedOut
 +
 +
#echo "userOut = "
 +
#cat userOut
 +
 +
#echo "expectedOut = "
 +
#cat expectedOut
 +
 +
#--- compute difference ---
 +
diff -y -w --ignore-all-space userOut expectedOut > diff.out
 +
 +
#--- reject if different ---
 +
if ((\$? > 0)); then
 +
      incorrectOutput
 +
      grade=70
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
else
 +
      #--- good output ---
 +
      echo "Comment :=>>- Congrats, your output is correct."
 +
      echo "Comment :=>> --------------------------------."
 +
      echo "<|--"                                                                           
 +
      echo "Your program tested with"                                                       
 +
      echo "temperatures starting from"
 +
      echo "\$TEMP1 with \$NOLINES lines"                               
 +
      echo ""
 +
      cat userOut
 +
      echo "--|>"
 +
      grade=100
 +
fi
 +
 +
# =================================================
 +
# Report grade
 +
if (( grade > 100 )); then
 +
  grade=100
 +
fi
 +
echo "Grade :=>> \$grade"
 +
 +
 +
exit
 +
 +
 +
 +
 +
EEOOFF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
 +
==vpl_evaluate.sh  for Problem 2==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
#set -x
 +
 +
# --- program tested (no extension) ---
 +
prog=hw2_2.py
 +
solutionProg=hw2sol.py
 +
 +
# --- Python ----
 +
python=/usr/local/bin/python3.4
 +
#python=/usr/bin/python3.3
 +
 +
# =================================================
 +
# Pick 2 random inputs to test program with
 +
TEMP1=\$RANDOM
 +
let "TEMP1 %= 30"
 +
let "NOLINES = 6"
 +
let "TEMP2 = TEMP1 + NOLINES"
 +
STEP=\$RANDOM
 +
let "STEP %= 4"
 +
let "STEP += 1"
 +
 +
#echo "TEMP1 = \$TEMP1"
 +
#echo "NOLINES = \$NOLINES"
 +
 +
echo \$TEMP1 > input
 +
echo \$NOLINES >> input
 +
echo \$STEP >> input
 +
 +
# =================================================
 +
# generate user output and exptect output
 +
\$python \$prog < input > userOut
 +
\$python \$solutionProg < input > expectedOut
 +
 +
 +
# =================================================
 +
# 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"
 +
        echo "temperatures starting from"
 +
        echo "\$TEMP1 with \$NOLINES lines"
 +
        echo "in steps of \$STEP"
 +
        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 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
 +
        mv dummy.out \$1
 +
 +
        #--- remove multiple spaces ---
 +
        cat \$1 | sed 's/  */ /g' > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove blank lines ---
 +
        cat \$1 | sed '/^\s*\$/d' > dummy.out
 +
        mv dummy.out \$1
 +
  fi
 +
}
 +
 +
cleanup userOut
 +
cleanup expectedOut
 +
 +
#echo "userOut = "
 +
#cat userOut
 +
 +
#echo "expectedOut = "
 +
#cat expectedOut
 +
 +
#--- compute difference ---
 +
diff -y -w --ignore-all-space userOut expectedOut > diff.out
 +
 +
#--- reject if different ---
 +
if ((\$? > 0)); then
 +
      incorrectOutput
 +
      grade=70
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
else
 +
      #--- good output ---
 +
      echo "Comment :=>>- Congrats, your output is correct."
 +
      echo "Comment :=>> --------------------------------."
 +
      echo "<|--"                                                                           
 +
      echo "Your program tested with"                                                       
 +
      echo "temperatures starting from"
 +
      echo "\$TEMP1 with \$NOLINES lines"                               
 +
      echo "in steps of \$STEP"
 +
      echo ""
 +
      cat userOut
 +
      echo "--|>"
 +
      grade=100
 +
fi
 +
 +
# =================================================
 +
# Report grade
 +
if (( grade > 100 )); then
 +
  grade=100
 +
fi
 +
echo "Grade :=>> \$grade"
 +
 +
 +
exit
 +
 +
 +
 +
 +
EEOOFF
 +
 +
chmod +x vpl_execution
 +
 +
</source>
 +
 +
==vpl_evaluate.sh  for Problem 3==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
#set -x
 +
 +
# --- program tested (no extension) ---
 +
prog=hw2.py
 +
solutionProg=hw2sol.py
 +
 +
# --- Python ----
 +
python=/usr/local/bin/python3.4
 +
#python=/usr/bin/python3.3
 +
 +
# =================================================
 +
# Pick 2 random inputs to test program with
 +
TEMP1=\$RANDOM
 +
let "TEMP1 %= 30"
 +
NOLINES=\$RANDOM
 +
let "NOLINES %= 6"
 +
let "NOLINES += 1"
 +
let "TEMP2 = TEMP1 + NOLINES"
 +
 +
#echo "TEMP1 = \$TEMP1"
 +
#echo "NOLINES = \$NOLINES"
 +
 +
echo \$TEMP1 > input
 +
echo \$NOLINES >> input
 +
 +
# =================================================
 +
# generate user output and exptect output
 +
\$python \$prog < input > userOut
 +
\$python \$solutionProg < input > expectedOut
 +
 +
 +
# =================================================
 +
# 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"
 +
        echo "temperatures starting from"
 +
        echo "\$TEMP1 with \$NOLINES lines"
 +
        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 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
 +
        mv dummy.out \$1
 +
 +
        #--- remove multiple spaces ---
 +
        cat \$1 | sed 's/  */ /g' > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove blank lines ---
 +
        cat \$1 | sed '/^\s*\$/d' > dummy.out
 +
        mv dummy.out \$1
 +
  fi
 +
}
 +
 +
cleanup userOut
 +
cleanup expectedOut
 +
 +
#echo "userOut = "
 +
#cat userOut
 +
 +
#echo "expectedOut = "
 +
#cat expectedOut
 +
 +
#--- compute difference ---
 +
diff -y -w --ignore-all-space userOut expectedOut > diff.out
 +
 +
#--- reject if different ---
 +
if ((\$? > 0)); then
 +
      incorrectOutput
 +
      grade=70
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
else
 +
      #--- good output ---
 +
      echo "Comment :=>>- Congrats, your output is correct."
 +
      echo "Comment :=>> --------------------------------."
 +
      echo "<|--"                                                                           
 +
      echo "Your program tested with"                                                       
 +
      echo "temperatures starting from"
 +
      echo "\$TEMP1 with \$NOLINES lines"                               
 +
      echo ""
 +
      cat userOut
 +
      echo "--|>"
 +
      grade=100
 +
fi
 +
 +
# =================================================
 +
# Report grade
 +
if (( grade > 100 )); then
 +
  grade=100
 +
fi
 +
echo "Grade :=>> \$grade"
 +
 +
 +
exit
 +
 +
 +
 +
 +
EEOOFF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
 +
==vpl_evaluate.sh  for Problem 4==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
#set -x
 +
 +
# --- program tested (no extension) ---
 +
prog=hw2.py
 +
solutionProg=hw2sol.py
 +
 +
# --- Python ----
 +
python=/usr/local/bin/python3.4
 +
#python=/usr/bin/python3.3
 +
 +
# =================================================
 +
# Pick 2 random inputs to test program with
 +
TEMP1=\$RANDOM
 +
let "TEMP1 %= 30"
 +
NOLINES=\$RANDOM
 +
let "NOLINES %= 6"
 +
let "NOLINES += 1"
 +
let "TEMP2 = TEMP1 + NOLINES"
 +
 +
#echo "TEMP1 = \$TEMP1"
 +
#echo "NOLINES = \$NOLINES"
 +
 +
echo \$TEMP1 > input
 +
echo \$NOLINES >> input
 +
 +
# =================================================
 +
# generate user output and exptect output
 +
\$python \$prog < input > userOut
 +
\$python \$solutionProg < input > expectedOut
 +
 +
 +
# =================================================
 +
# 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"
 +
        echo "temperatures starting from"
 +
        echo "\$TEMP1 with \$NOLINES lines"
 +
        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 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
 +
        mv dummy.out \$1
 +
 +
        #--- remove multiple spaces ---
 +
        cat \$1 | sed 's/  */ /g' > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove blank lines ---
 +
        cat \$1 | sed '/^\s*\$/d' > dummy.out
 +
        mv dummy.out \$1
 +
  fi
 +
}
 +
 +
cleanup userOut
 +
cleanup expectedOut
 +
 +
#echo "userOut = "
 +
#cat userOut
 +
 +
#echo "expectedOut = "
 +
#cat expectedOut
 +
 +
#--- compute difference ---
 +
diff -y -w --ignore-all-space userOut expectedOut > diff.out
 +
 +
#--- reject if different ---
 +
if ((\$? > 0)); then
 +
      incorrectOutput
 +
      grade=70
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
else
 +
      #--- good output ---
 +
      echo "Comment :=>>- Congrats, your output is correct."
 +
      echo "Comment :=>> --------------------------------."
 +
      echo "<|--"                                                                           
 +
      echo "Your program tested with"                                                       
 +
      echo "temperatures starting from"
 +
      echo "\$TEMP1 with \$NOLINES lines"                               
 +
      echo ""
 +
      cat userOut
 +
      echo "--|>"
 +
      grade=100
 +
fi
 +
 +
# =================================================
 +
# Report grade
 +
if (( grade > 100 )); then
 +
  grade=100
 +
fi
 +
echo "Grade :=>> \$grade"
 +
 +
 +
exit
 +
 +
 +
 +
 +
EEOOFF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
 +
==vpl_evaluate.sh for Probem 5==
 +
<source lang="bash">
 +
#! /bin/bash
 +
# D. Thiebaut
 +
 +
cat > vpl_execution <<EEOOFF
 +
#! /bin/bash
 +
#set -x
 +
 +
# --- program tested (no extension) ---
 +
prog=hw2.py
 +
solutionProg=hw2sol.py
 +
 +
# --- Python ----
 +
#python=/usr/local/bin/python3.4
 +
python=/usr/bin/python3.3
 +
 +
# =================================================
 +
# Pick 1 random input to test program with
 +
TEMP1=\$RANDOM
 +
let "TEMP1 %= 30"
 +
NOLINES=\$RANDOM
 +
let "NOLINES %= 11"
 +
let "NOLINES += 1"
 +
let "TEMP2 = TEMP1 + NOLINES"
 +
 +
#echo "TEMP1 = \$TEMP1"
 +
#echo "NOLINES = \$NOLINES"
 +
 +
#echo \$TEMP1 > input
 +
echo \$NOLINES >> input
 +
 +
# =================================================
 +
# generate user output and exptect output
 +
\$python \$prog < input > userOut
 +
\$python \$solutionProg < input > expectedOut
 +
cp userOut userOut.org
 +
cp expectedOut expectedOut.org
 +
 +
# =================================================
 +
# 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"
 +
        echo "\$NOLINES lines of stars"
 +
        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 * and non minus---
 +
        grep "*" \$1 > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove multiple spaces ---
 +
        cat \$1 | sed 's/  */ /g' > dummy.out
 +
        mv dummy.out \$1
 +
 +
        #--- remove blank lines ---
 +
        cat \$1 | sed '/^\s*\$/d' > dummy.out
 +
        mv dummy.out \$1
 +
  fi
 +
}
 +
 +
cleanup userOut
 +
cleanup expectedOut
 +
 +
#echo "userOut = "
 +
#cat userOut
 +
 +
#echo "expectedOut = "
 +
#cat expectedOut
 +
 +
#--- compute difference ---
 +
diff -y -w --ignore-all-space userOut expectedOut > diff.out
 +
 +
#--- reject if different ---
 +
if ((\$? > 0)); then
 +
      incorrectOutput
 +
      grade=70
 +
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
 +
else
 +
      #--- good output ---
 +
      echo "Comment :=>>- Congrats, your output is correct."
 +
      echo "Comment :=>> --------------------------------."
 +
      echo "<|--"                                                                           
 +
      echo "Your program tested with"
 +
      echo "\$NOLINES lines of stars"                               
 +
      echo ""
 +
      cat userOut
 +
      echo "--|>"
 +
      grade=100
 +
fi
 +
 +
# =================================================
 +
# Report grade
 +
if (( grade > 100 )); then
 +
  grade=100
 +
fi
 +
echo "Grade :=>> \$grade"
 +
 +
 +
exit
 +
 +
 +
 +
 +
EEOOFF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
<br />
 +
==hw2sol.py==
 +
<source lang="python">
 +
# hw2.py
 +
# D. Thiebaut
 +
# This program contains the solution programs for Homework 2.
 +
# The different solutions are stored in separate functions.  Do not
 +
# worry about using functions.  You didn't have to use functions
 +
# for this assignment.  All you have to do is look at the code inside
 +
# a function to see what you were supposed to do.
 +
 +
# This is a solution for Lab 2
 +
def prompt1():
 +
    fname = input( "your first name?  " )
 +
    lname = input( "your last name?    " )
 +
    box  = input( "your box number?  " )
 +
    phone = input( "your phone number? " )
 +
    Id    = input( "your Id number?    " )
 +
 +
    print( "+---------------------------------" )
 +
    print( "| Name:  ", fname, lname, "(", Id, ")" )
 +
    print( "| Box    ", box )
 +
    print( "| Phone: ", phone )
 +
    print( "+---------------------------------" )
 +
 +
# another solution for Lab 2
 +
def prompt2():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    temp2 = eval( input( "Enter high temperature: " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp2+1 ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
 +
# Solution for Problem 1
 +
# The code asks the user for 2 numbers and displays a table
 +
# of temperatures in Fahrenheit and Celsius
 +
def hw2_1():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
   
 +
# Solution for Problem 2
 +
# The code asks the user for 3 numbers and displays a table
 +
# of temperatures in Fahrenheit and Celsius.
 +
# The 3 numbers are the starting temperature, the number
 +
# of lines, and the step between Fahrenheit temperatures.
 +
def hw2_2():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    step = eval( input( "Step? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius" )
 +
    for temp in range( temp1, temp1 + step*noLines, step ):
 +
        print( temp, "      ", (temp-32)*5/9 )
 +
   
 +
# Solution for Problem 3.  The program asks for 2 numbers
 +
# and displays a table of temperatures, with Fahrenheit,
 +
# Celsius and Kelvin
 +
def hw2_3():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
    print( "Fahrenheit Celsius Kelvin" )
 +
 +
    # display the table
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        celsius = (temp-32)*5/9
 +
        kelvin = celsius + 273.15
 +
        print( temp, "      ", celsius,  kelvin )
 +
 +
# Solution for Problem 4.  The program asks for 2 numbers
 +
# and displays a table of temperatures, with Kelvin,
 +
# Celsius and Fahrenheit. 
 +
def hw2_4():
 +
    temp1 = eval( input( "Enter low temperature: " ) )
 +
    noLines = eval( input( "How many lines? " ) )
 +
    print()
 +
 +
    # display the table
 +
    print( "Kelvin Celsius Fahrenheit" )
 +
    for temp in range( temp1, temp1 + noLines ):
 +
        celsius = (temp-32)*5/9
 +
        kelvin = celsius + 273.15
 +
        print( kelvin, celsius, temp )
 +
 +
# Solution for Problem 5
 +
# Asks the user for a number of lines of stars
 +
# and displays 2 triangles, one with the tip at the top
 +
# and the second one with the tip at the bottom.
 +
def hw2_5():
 +
    noStars = eval( input( "How many lines of stars? " ) )
 +
    print()
 +
   
 +
    # display first trianble
 +
    print( "triangle 1" )
 +
    print()
 +
    for n in range( 1, noStars+1 ):
 +
        print( n * '*' )
 +
    print()
 +
 +
    # display second trianble
 +
    print( "triangle 2" )
 +
    print()
 +
    for n in range( noStars, 0, -1 ):
 +
        print( n * '*' )
 +
    print()
 +
   
 +
# --------------------------------------------------------------------
 +
# uncomment the line below that corresponds
 +
# to the solution you want to run
 +
# --------------------------------------------------------------------
 +
#hw2_1()
 +
#hw2_2()
 +
#hw2_3()
 +
#hw2_4()
 +
hw2_5()
 +
 +
</source>
 +
<br />
 +
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Homework]][[Category:Python]]
 
[[Category:CSC111]][[Category:Homework]][[Category:Python]]

Latest revision as of 16:54, 11 June 2015

--D. Thiebaut (talk) 20:38, 3 February 2015 (EST)


This homework will be available on 2/5 at 5 p.m., right after the last lab section. The due date for this homework is Tuesday 2/10 at 11:55 p.m.


<showafterdate after="20150205 17:00" before="20150601 00:00">


Problem 1


Write a program called hw2_1.py that displays a table of temperatures, the left column in Fahrenheit, and the right column in Celsius. The program asks the user for two numbers, a temperature T, and a number of temperatures n, and displays a conversion table showing n lines of temperature conversions.

Here is an example of how your program should work (the user input is in boldface):

Please enter the starting temperature:  30
How many lines do you want for the table?  10

Fahrenheit Celsius
30         -1.11111111111
31         -0.555555555556
32         0.0
33         0.555555555556
34         1.11111111111
35         1.66666666667
36         2.22222222222
37         2.77777777778
38         3.33333333333
39         3.88888888889

And here is another example:

Please enter the starting temperature:  32
How many lines do you want for the table?  4
 
Fahrenheit Celsius
32         0.0
33         0.555555555556
34         1.11111111111
35         1.66666666667
  • Make sure to label the columns with " Fahrenheit" and "Celsius" please!
  • Submit your program on Moodle, in the HW2 PB1 section.


Problem 2


Write a program called hw2_2.py that displays a table of temperatures, the left column in Celsius, and the right column in Fahrenheit. The program asks the user for three numbers:

  • a temperature T,
  • a number of temperatures n,
  • and the step used to skip temperatures.

If the user enters 1 for the step, the output of this program will be the same as the output of the solution program for Problem 1.

Note: we assume that the step will always be positive.

Here is an example of how your program should work (the user input is in boldface):

Please enter the starting temperature:  30
How many lines do you want for the table?  5
What step should be used? 2 

30         -1.11111111111
32         0.0
34         1.11111111111
36         2.22222222222
38         3.33333333333

And here is another example:

Please enter the starting temperature:  32
How many lines do you want for the table?  3
What step should be used? 2 
 
32         0.0
34         1.11111111111
36         2.22222222222


  • Submit your program on Moodle, in the HW2 PB2 section.


Problem 3


This problem is the same as Problem 1, except that the output now shows degrees Kelvin, as well. A degree Kelvin is computed as degrees celsius + 273.15.

For example, if you have a variable called celsius that contains the temperature expressed in Celsius, and you want to compute the equivalent Kelvin temperature, and store that value in a variable called kelvin, you would write:

      kelvin = celsius + 273.15

Here is an example of how your program should behave:

Enter low temperature: 30
How many lines? 5

Fahrenheit Celsius Kelvin
30         -1.11111111111 272.038888889
31         -0.555555555556 272.594444444
32         0.0 273.15
33         0.555555555556 273.705555556
34         1.11111111111 274.261111111


Do not worry about the unpleasantly formatted output. Right now your job is to concentrate on the loop construct. We'll see how to format real numbers later!
Submit your program to the HW2 PB3 section on Moodle.

Problem 4


This problem is the same as Problem 3, except that the output now shows degrees Kelvin listed first on a line, then Celsius, then Fahrenheit.

Here is an example of how your program should behave:

Enter low temperature in Fahrenheit: 30
How many lines? 6

Kelvin Celsius Fahrenheit
272.038888889 -1.11111111111 30
272.594444444 -0.555555555556 31
273.15 0.0 32
273.705555556 0.555555555556 33
274.261111111 1.11111111111 34
274.816666667 1.66666666667 35 


Submit your program to the HW2 PB4 section on Moodle.

Problem 5: Loops and Triangles



Preparation


Using the Python Shell window, type the following statements one after the other, and try to figure out what is going on:

>>> line = "!"

>>> line

>>> line = 5 * "!"

>>> line 

>>> line = 20 * "a"

>>> line

>>> n = 7

>>> line = n * "b"

>>> line

Using this information, write a new program that uses one for-loop to display the following triangle:

*
**
***
****
*****

Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars (Hints: use a loop that goes from 1 to 5!)

When this test program works well, modify it so that it first asks the user how many lines the triangle should have, and then the program displays a triangle with that number of lines.

Here's an example of how your program should work (the user input is underlined):


How many lines? 7

* 
**
***
****
*****
******
*******

Here's another example:

How many lines? 2

* 
**

When your program works well, you are ready to write the program you need to submit.

Your Assignment


Write a new program that will prompt the user for a number and use this number to print two triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.

Here is an example of how the program will work when the user enters 5:


How many lines? 5
 
triangle 1

*
**
***
****
***** 

triangle 2

*****
****
***
**
*


Note that all the triangles are flush against the left margin.

Submit your program to the HW2 PB5 section on Moodle.
</showafterdate>

<showafterdate after="20150211 00:00" before="20150601 00:00">

Solution Program


# hw2.py
# D. Thiebaut
# This program contains the solution programs for Homework 2.
# The different solutions are stored in separate functions.  Do not
# worry about using functions.  You didn't have to use functions
# for this assignment.  All you have to do is look at the code inside
# a function to see what you were supposed to do.

# This is a solution for Lab 2
def prompt1():
    fname = input( "your first name?   " )
    lname = input( "your last name?    " )
    box   = input( "your box number?   " )
    phone = input( "your phone number? " )
    Id    = input( "your Id number?    " )

    print( "+---------------------------------" )
    print( "| Name:  ", fname, lname, "(", Id, ")" )
    print( "| Box    ", box )
    print( "| Phone: ", phone )
    print( "+---------------------------------" )

# another solution for Lab 2
def prompt2():
    temp1 = eval( input( "Enter low temperature: " ) )
    temp2 = eval( input( "Enter high temperature: " ) )
    print()
    print( "Fahrenheit Celsius" )
    for temp in range( temp1, temp2+1 ):
        print( temp, "       ", (temp-32)*5/9 )

# Solution for Problem 1
# The code asks the user for 2 numbers and displays a table
# of temperatures in Fahrenheit and Celsius
def hw2_1():
    temp1 = eval( input( "Enter low temperature: " ) )
    noLines = eval( input( "How many lines? " ) )
    print()
    print( "Fahrenheit Celsius" )
    for temp in range( temp1, temp1 + noLines ):
        print( temp, "       ", (temp-32)*5/9 )
    
# Solution for Problem 2
# The code asks the user for 3 numbers and displays a table
# of temperatures in Fahrenheit and Celsius.
# The 3 numbers are the starting temperature, the number
# of lines, and the step between Fahrenheit temperatures.
def hw2_2():
    temp1 = eval( input( "Enter low temperature: " ) )
    noLines = eval( input( "How many lines? " ) )
    step = eval( input( "Step? " ) ) 
    print()
    print( "Fahrenheit Celsius" )
    for temp in range( temp1, temp1 + step*noLines, step ):
        print( temp, "       ", (temp-32)*5/9 )
    
# Solution for Problem 3.  The program asks for 2 numbers
# and displays a table of temperatures, with Fahrenheit, 
# Celsius and Kelvin
def hw2_3():
    temp1 = eval( input( "Enter low temperature: " ) )
    noLines = eval( input( "How many lines? " ) )
    print()
    print( "Fahrenheit Celsius Kelvin" )

    # display the table
    for temp in range( temp1, temp1 + noLines ):
        celsius = (temp-32)*5/9
        kelvin = celsius + 273.15
        print( temp, "       ", celsius,  kelvin )

# Solution for Problem 4.  The program asks for 2 numbers
# and displays a table of temperatures, with Kelvin,
# Celsius and Fahrenheit.  
def hw2_4():
    temp1 = eval( input( "Enter low temperature: " ) )
    noLines = eval( input( "How many lines? " ) )
    print()

    # display the table
    print( "Kelvin Celsius Fahrenheit" )
    for temp in range( temp1, temp1 + noLines ):
        celsius = (temp-32)*5/9
        kelvin = celsius + 273.15
        print( kelvin, celsius, temp )

# Solution for Problem 5
# Asks the user for a number of lines of stars
# and displays 2 triangles, one with the tip at the top
# and the second one with the tip at the bottom.
def hw2_5():
    noStars = eval( input( "How many lines of stars? " ) )
    print()
     
    # display first trianble
    print( "triangle 1" )
    print()
    for n in range( 1, noStars+1 ):
        print( n * '*' )
    print()

    # display second trianble
    print( "triangle 2" )
    print()
    for n in range( noStars, 0, -1 ):
        print( n * '*' )
    print()
    
# --------------------------------------------------------------------
# uncomment the line below that corresponds 
# to the solution you want to run
# --------------------------------------------------------------------
#hw2_1()
#hw2_2()
#hw2_3()
#hw2_4()
hw2_5()

</showafterdate>



...