Difference between revisions of "CSC111 Homework 4 2015"

From dftwiki3
Jump to: navigation, search
 
(11 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
=Problem #1=
 
=Problem #1=
 
<br />
 
<br />
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.   
+
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.  Make sure there's a blank line before the output of the account nameThis line will be used by the grading program to spot your output.
 
<br />
 
<br />
 
;:Example
 
;:Example
Line 19: Line 19:
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababa  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababa  
 
<br />
 
<br />
Line 31: Line 32:
  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>  
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>  
+
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>
 +
 
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababazzz  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababazzz  
  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Al</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Al</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Gus</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Gus</u>  
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is:aguszzzz  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is:aguszzzz  
  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Alma</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Alma</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Verylongname</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Verylongname</u>  
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: averylon  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: averylon  
 
<br />
 
<br />
Line 47: Line 51:
 
=Problem #3=
 
=Problem #3=
 
<br />
 
<br />
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).   
+
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).  We assume the user name will always have at least 3 characters.
 
<br />
 
<br />
 
;:Example
 
;:Example
Line 53: Line 57:
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Allie</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>BABA</u>  
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababa201  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: ababa201  
  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Al</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Al</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Gus</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Gus</u>  
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: agus2015  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: agus2015  
  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Alma</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;First name? <u>Alma</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Verylongname</u>  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Last name? <u>Verylongname</u>  
 +
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: averylon  
 
  &nbsp;&nbsp;&nbsp;&nbsp;Your computer account name is: averylon  
 
<br />
 
<br />
Line 68: Line 75:
 
=Problem #4=
 
=Problem #4=
 
<br />
 
<br />
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.
+
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.  Notice that there is a blank line between the input section and the output section.
 
<br />
 
<br />
 
:;Example
 
:;Example
Line 74: Line 81:
 
First name? Maria
 
First name? Maria
 
Last name? LUCE
 
Last name? LUCE
 +
 
........M
 
........M
 
.......Ma
 
.......Ma
Line 120: Line 128:
 
:;Example
 
:;Example
 
:<source lang="text">
 
:<source lang="text">
Enter date: 02142015
+
Enter date: 02022015
  
 
2 Feb 2015
 
2 Feb 2015
Line 127: Line 135:
 
* Submit your program to HW 4 PB 6 on Moodle.
 
* Submit your program to HW 4 PB 6 on Moodle.
 
<br />
 
<br />
 +
 
=Problem #7=
 
=Problem #7=
 
<br />
 
<br />
Line 154: Line 163:
 
<br />
 
<br />
 
<br />
 
<br />
<showafterdate after="2015025 00:00">
+
<showafterdate after="20150225 00:00">
 
=Solution Programs=
 
=Solution Programs=
 
<br />
 
<br />
Line 523: Line 532:
 
chmod +x vpl_execution
 
chmod +x vpl_execution
 
</source>
 
</source>
<br />
 
===vpl_evaluate.sh===
 
<br />
 
<source lang="bash">
 
 
</source>
 
<br />
 
 
 
==Problem 1==
 
<br />
 
===vpl_run.sh===
 
<br />
 
<source lang="bash">
 
  
</source>
 
 
<br />
 
<br />
 
===vpl_evaluate.sh===
 
===vpl_evaluate.sh===
Line 830: Line 824:
  
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 +
 +
  testNo=\$((i+1))
 +
  echo "Comment :=>>- Test \$testNo"
 +
 
 
   echo "\${TEMP1[i]}" > input
 
   echo "\${TEMP1[i]}" > input
 
   echo "\${TEMP2[i]}" >> input
 
   echo "\${TEMP2[i]}" >> input
Line 838: Line 836:
 
   # =================================================
 
   # =================================================
 
   # generate user output and exptect output
 
   # generate user output and exptect output
   \$python \$prog < input > userOut
+
   \$python \$prog < input 2> errorOut 1> userOut
 
   \$python \$solutionProg < input > expectedOut
 
   \$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 userOut userOut.org
Line 888: Line 899:
 
   
 
   
 
chmod +x vpl_execution
 
chmod +x vpl_execution
 +
  
  
Line 931: Line 943:
  
 
# --- Python ----
 
# --- Python ----
python=/usr/local/bin/python3.4
+
if [[ `hostname -s` = "beowulf2" ]]; then
#python=/usr/bin/python3.3
+
  python=/usr/bin/python3.3
 +
else
 +
  python=/usr/local/bin/python3.4
 +
fi
  
 
# =================================================
 
# =================================================
Line 1,021: Line 1,036:
 
   echo "\${TEMP2[i]}" >> input
 
   echo "\${TEMP2[i]}" >> input
  
   #echo "\${TEMP1[i]}"
+
   testNo=\$((i+1))
   #echo "\${TEMP2[i]}"  
+
   echo "Comment :=>>- Test \$testNo"
 +
 
  
 
   # =================================================
 
   # =================================================
 
   # generate user output and exptect output
 
   # generate user output and exptect output
  \$python \$prog < input > userOut
 
 
   \$python \$solutionProg < input > expectedOut
 
   \$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 userOut userOut.org
Line 1,107: Line 1,136:
 
<br />
 
<br />
 
<source lang="bash">
 
<source lang="bash">
 +
 
#! /bin/bash
 
#! /bin/bash
 
# D. Thiebaut
 
# D. Thiebaut
Line 1,119: Line 1,149:
  
 
# --- Python ----
 
# --- Python ----
python=/usr/local/bin/python3.4
+
if [[ `hostname -s` = "beowulf2" ]]; then
#python=/usr/bin/python3.3
+
  python=/usr/bin/python3.3
 +
else
 +
  python=/usr/local/bin/python3.4
 +
fi
  
 
# =================================================
 
# =================================================
 
# Pick 3 random inputs to test program with
 
# Pick 3 random inputs to test program with
 
TEMP1=( "02142015"  "01311900"  "12012000" )
 
TEMP1=( "02142015"  "01311900"  "12012000" )
 +
#TEMP1=( "02142015" ) #  "01311900"  "12012000" )
  
 
# =================================================
 
# =================================================
Line 1,167: Line 1,201:
 
         #--- display test file ---
 
         #--- display test file ---
 
         echo "<|--"  
 
         echo "<|--"  
         echo "Your program tested with \$1 and \$2"
+
         echo "Your program tested with \$1"
 
         echo "--|>"
 
         echo "--|>"
  
Line 1,240: Line 1,274:
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 
   echo "\${TEMP1[i]}" > input
 
   echo "\${TEMP1[i]}" > input
 +
  #echo "input = "
 +
  #cat input
  
 +
  testNo=\$((i+1))
 +
  echo "Comment :=>>- Test \$testNo"
  
 
   # =================================================
 
   # =================================================
 
   # generate user output and exptect output
 
   # generate user output and exptect output
   \$python \$prog < input > userOut
+
   \$python \$prog < input 2> errorOut 1> userOut
 
   \$python \$solutionProg < input > expectedOut
 
   \$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
 
   # test to see if the program uses the right
Line 1,273: Line 1,324:
 
       echo "Comment :=>> --------------------------------."
 
       echo "Comment :=>> --------------------------------."
 
       echo "<|--"                                                                             
 
       echo "<|--"                                                                             
       echo "Your program tested with \${TEMP1[i]} and \${TEMP2[i]}"
+
       echo "Your program tested with \${TEMP1[i]}"
 
       echo ""
 
       echo ""
 
       cat userOut
 
       cat userOut
Line 1,298: Line 1,349:
 
   
 
   
 
chmod +x vpl_execution
 
chmod +x vpl_execution
 +
 +
 +
  
  
Line 1,328: Line 1,382:
 
<br />
 
<br />
 
<source lang="bash">
 
<source lang="bash">
 +
 
#! /bin/bash
 
#! /bin/bash
 
# D. Thiebaut
 
# D. Thiebaut
Line 1,340: Line 1,395:
  
 
# --- Python ----
 
# --- Python ----
python=/usr/local/bin/python3.4
+
if [[ `hostname -s` = "beowulf2" ]]; then
#python=/usr/bin/python3.3
+
  python=/usr/bin/python3.3
 +
else
 +
  python=/usr/local/bin/python3.4
 +
fi
 +
 
  
 
# =================================================
 
# =================================================
Line 1,461: Line 1,520:
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 
for ((i=0;i<\${#TEMP1[@]};i++)); do
 
   echo "\${TEMP1[i]}" > input
 
   echo "\${TEMP1[i]}" > input
 +
 +
  testNo=\$((i+1))
 +
  echo "Comment :=>>- Test \$testNo"
  
  
 
   # =================================================
 
   # =================================================
 
   # generate user output and exptect output
 
   # generate user output and exptect output
  \$python \$prog < input > userOut
 
 
   \$python \$solutionProg < input > expectedOut
 
   \$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
 
   # test to see if the program uses the right
Line 1,519: Line 1,594:
 
   
 
   
 
chmod +x vpl_execution
 
chmod +x vpl_execution
 +
  
  
Line 1,575: Line 1,651:
 
import sys
 
import sys
 
import random
 
import random
 +
import subprocess
  
 
#--- define what the student program is called, and what the solution
 
#--- define what the student program is called, and what the solution
Line 1,580: Line 1,657:
 
module = "hw4"
 
module = "hw4"
 
solutionModule = "hw4sol"
 
solutionModule = "hw4sol"
 +
stripOutputsBeforeCompare = True  # set to true if extra space in output is not important
  
 
def commentLong( line ):
 
def commentLong( line ):
Line 1,610: Line 1,688:
  
 
     return (foundDef, foundCall)
 
     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 ):
 
def generateInputFileWithRandomInputs( inputFileName ):
Line 1,636: Line 1,739:
 
         text += word + "\n"
 
         text += word + "\n"
 
     file.close()
 
     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
 
     return text
  
Line 1,641: Line 1,753:
 
# runs the module, passes it data from the input file on its stdin
 
# runs the module, passes it data from the input file on its stdin
 
# and get its output on stdout captured in outputFileName.
 
# 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 ):
 
def runModule( module, inputFileName, outputFileName ):
 +
    error = False
 +
 
     #--- make stdin read information from the text file
 
     #--- make stdin read information from the text file
 
     sys.stdin = open( inputFileName, "r" )
 
     sys.stdin = open( inputFileName, "r" )
Line 1,647: Line 1,763:
 
     #--- capture the stdout of the program to test into a file
 
     #--- capture the stdout of the program to test into a file
 
     saveStdOut = sys.stdout
 
     saveStdOut = sys.stdout
 +
    saveStdErr = sys.stderr
 +
 
     sys.stdout = open( outputFileName, "w" )
 
     sys.stdout = open( outputFileName, "w" )
 +
    sys.stderr = open( "errorOut", "w" )
  
 
     #--- run the student program ---
 
     #--- run the student program ---
     _module = __import__(  module  )
+
     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 ---
 
     #--- filter out junk from output of program ---
 
     sys.stdout.close()
 
     sys.stdout.close()
Line 1,662: Line 1,792:
 
     #print( text, end="" )
 
     #print( text, end="" )
 
     file.close()
 
     file.close()
     return text
+
     return False, text
  
 
def removeBlankLines( lines ):
 
def removeBlankLines( lines ):
Line 1,674: Line 1,804:
  
 
def compareUserExpected( inputLines, userOutText, expectedOutText ):
 
def compareUserExpected( inputLines, userOutText, expectedOutText ):
 +
    global stripOutputsBeforeCompare
 +
 
     userOutText = removeBlankLines( userOutText )
 
     userOutText = removeBlankLines( userOutText )
 
     expectedOutText = removeBlankLines( expectedOutText )
 
     expectedOutText = removeBlankLines( expectedOutText )
Line 1,684: Line 1,816:
 
     for i in range( len( userTextOutLines ) ):
 
     for i in range( len( userTextOutLines ) ):
 
         lineNo = i+1
 
         lineNo = i+1
         if userTextOutLines[i] != expectedOutTextLines[i]:
+
         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 )
 
             misMatchLineNumbers.append( lineNo )
 
      
 
      
Line 1,695: Line 1,834:
 
     global solutionModule
 
     global solutionModule
  
 +
   
 
     #--- check that the main module uses a main() function
 
     #--- check that the main module uses a main() function
 
     foundDef, foundCall = checkForFunctionPresence( module, "main" )
 
     foundDef, foundCall = checkForFunctionPresence( module, "main" )
Line 1,707: Line 1,847:
 
     inputLines = generateInputFileWithRandomInputs( "input" )
 
     inputLines = generateInputFileWithRandomInputs( "input" )
  
     userOutText = runModule( module, "input", "userOut" )
+
     Ok, errorMessage = checkModuleRunsOk( module, "input" )
     expectedOutText = runModule( solutionModule, "input", "expectedOut" )
+
     if not Ok:
 +
        commentLong( "- Your program crashed...\n"
 +
                    + "Your program was tested with:\n"
 +
                    + inputLines  + "\n"
 +
                    + "Error message:\n"
 +
                    + errorMessage + "\n" )
 +
        printGrade( 50 )
 +
        return
  
     #commentLong( "Your program tested with:\n" + inputLines )
+
                   
    #commentLong( "userOut = \n" + userOutText )
+
     error, userOutText        = runModule( module, "input", "userOut" )
     #commentLong( "expectedOut = \n" + expectedOutText )
+
    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,  
 
     misMatchLineNumbers = compareUserExpected( inputLines,  
Line 1,734: Line 1,890:
 
                     +"Expected output:\n"
 
                     +"Expected output:\n"
 
                     +expectedOutText + "\n"
 
                     +expectedOutText + "\n"
                     +("There are errors in Line%s " % s) +
+
                     +("There are differences in Line%s " % s) +
 
                     ", ".join( [str(k) for k in misMatchLineNumbers] )
 
                     ", ".join( [str(k) for k in misMatchLineNumbers] )
 
                     +"\n" )
 
                     +"\n" )
Line 1,745: Line 1,901:
  
 
main()
 
main()
 
  
  

Latest revision as of 18:02, 12 June 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!



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. Make sure there's a blank line before the output of the account name. This line will be used by the grading program to spot your output.

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). We assume the user name will always have at least 3 characters.

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. Notice that there is a blank line between the input section and the output section.

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: 02022015

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="20150225 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>


...