Difference between revisions of "CSC111 Homework 4 2015"
(→Problem #7) |
|||
Line 314: | Line 314: | ||
</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"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | ==Problem 1== | ||
+ | <br /> | ||
+ | ===vpl_run.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </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 1== | ||
+ | <br /> | ||
+ | ===vpl_run.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ===vpl_evaluate.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | ==Problem 1== | ||
+ | <br /> | ||
+ | ===vpl_run.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ===vpl_evaluate.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | ==Problem 1== | ||
+ | <br /> | ||
+ | ===vpl_run.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ===vpl_evaluate.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | ==Problem 1== | ||
+ | <br /> | ||
+ | ===vpl_run.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ===vpl_evaluate.sh=== | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | |||
+ | </onlydft> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 16:11, 17 February 2015
--D. Thiebaut (talk) 15:23, 15 February 2015 (EST)
Make sure you go through the Preparation Page for Homework 4 first, before attempting these programs!
Contents
Make sure you use a main() function in all your programs.
Problem #1
Write a program that prompts the user for her first and last name, and outputs a computer account made of the first initial of her first name, plus her last name, all in lowercase.
- Example
First name? Allie Last name? BABA Your computer account name is: ababa
- Submit your program to HW 4 PB 1 on Moodle.
Problem #2
Write a program that prompts the user for her first and last name, and outputs a computer account made of the first initial of her first name, plus her last name, all in lowercase. But now we want the computer account to be exactly 8 characters long. If the user names are too short, your program will pad it with the character 'z'.
- Example
First name? Allie Last name? BABA Your computer account name is: ababazzz
First name? Al Last name? Gus Your computer account name is:aguszzzz
First name? Alma Last name? Verylongname Your computer account name is: averylon
- Submit your program to HW 4 PB 2 on Moodle.
Problem #3
Write a program that prompts the user for her first and last name, and outputs a computer account made of the first initial of her first name, plus her last name, all in lowercase. We want the computer account to be exactly 8 characters long, but this time it is padded with the current year (2015 for us).
- Example
First name? Allie Last name? BABA Your computer account name is: ababa201
First name? Al Last name? Gus Your computer account name is: agus2015
First name? Alma Last name? Verylongname Your computer account name is: averylon
- Submit your program to HW 4 PB 3 on Moodle.
Problem #4
Write a program that prompts the user for her first and last name, and outputs a triangle made with her name, padded with dots in front of it, as illustrated below. There is no limit on the length of the first and last names.
- Example
First name? Maria Last name? LUCE ........M .......Ma ......Mar .....Mari ....Maria ...MariaL ..MariaLU .MariaLUC MariaLUCE
- Submit your program to HW 4 PB 4 on Moodle.
Problem #5
Write a program that prompts the user to enter a date in the form mmddyyyy, and displays it back as dd month yyyy, where month is the first 3 letters of the name of the month, in English.
Your program must use the list of months defined as follows:
monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
- Example
Enter date: 02042015 4 Feb 2015
- Submit your program to HW 4 PB 5 on Moodle.
Problem #6
Write a program that prompts the user to enter a date in the form mmddyyyy, and displays it back as dd month yyyy, where month is the first 3 letters of the name of the month, in English.
Your program must use the string defined as follows, and cannot use the list you used in Problem 5!
monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec"
- Example
Enter date: 02142015 2 Feb 2015
- Submit your program to HW 4 PB 6 on Moodle.
Problem #7
Write a program that asks the user for several pieces of information about some famous person, and outputs the information in a nicely formatted box, where the length of the bar of '=' signs before and before the information is the same as the length of the accomplishment information.
- Example
Please enter first name: Sophia Please enter last name: SMITH Please enter date of birth (yyyymmdd): 17960827 Please enter date of death( yyyymmdd): 18700602 Please enter accomplishment: Funded Smith College, the largest member of the seven sisters. ============================================================== Sophia Smith (August 27, 1796 -- June 2, 1870) Funded smith college, the largest member of the seven sisters. ==============================================================
- Note that the name is formatted correctly, even if the user enters all uppercase or lowercase letters for the name.
- Submit your program to HW 4 PB 7 on Moodle.
- Note
- The bar of '=' signs will not show up correctly on Moodle, because of it uses a proportional font. However, Idle will display the output correctly, so make sure you debug in Idle and use Moodle just for submission. Resist the urge to modify your program in Moodle. Idle is a much friendlier place to edit programs.
<showafterdate after="2015025 00:00">
Solution Programs
# hw4sol.py
# D. Thiebaut
# Solution program(s) for Homework 4, 2015
# The solution organizes the individual solutions
# into functions, but you didn't have to do that for
# solving the problems.
# Just comment out the appropriate line in the the main()
# function to energize the solution for a give problem..
# solution for Problem 1
def problem1():
fName = input( "Your first name? " )
lName = input( "Your last name? " )
full = fName[0] + lName
full = full.lower()
print( full)
# solution for Problem 2
def problem2():
fName = input( "Your first name? " )
lName = input( "Your last name? " )
full = fName[0] + lName + "zzzzzzzz"
full = full[0:8].lower()
print( full)
# solution for Problem 3
def problem3():
fName = input( "Your first name? " )
lName = input( "Your last name? " )
full = fName[0] + lName + "20152015"
full = full[0:8].lower()
print( full)
# solution for Problem 4
def problem4():
fName = input( "Your first name? " )
lName = input( "Your last name? " )
name = fName + lName
noChars = len( name )
for i in range( 1, noChars+1 ):
noSpaces = noChars - i
spaces = '.' * noSpaces
print( spaces + name[0:i] )
# solution for Problem 5
def problem5():
# define the list of months as a list of strings
monthNames = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
# get the date from the user
date = input( "Enter date: " )
# extract the different variables from the date
month = date[0:2]
day = date[2:4]
year = date[4: ]
dayInt = int( day )
monthIndex = int( month ) - 1
monthStr = monthNames[ monthIndex ][0:3]
# display result
print( dayInt, monthStr, year )
# solution for Problem 6
def problem6():
# define the list of months as a string of 3-char names
monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec"
# get the date from the user
date = input( "Enter date: " )
# extract the different variables from the date
month = date[0:2]
day = date[2:4]
year = date[4: ]
dayInt = int( day )
monthIndex = int( month ) - 1
monthStr = monthNames[ monthIndex*3 : monthIndex*3+3]
# display result
print( dayInt, monthStr, year )
# 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.
# Calls all the functions that solve the different problems.
# Comment out the lines you want to actually activate the
# function you are interested in.
def main():
#problem1()
#problem2()
#problem3()
#problem4()
#problem5()
#problem6()
problem7()
main()
</showafterdate>