Difference between revisions of "CSC111 Homework 4 2018"
(2 intermediate revisions by the same user not shown) | |||
Line 46: | Line 46: | ||
Last name? <u>BABA</u> | Last name? <u>BABA</u> | ||
− | | + | ababa### |
First name? <u>Al</u> | First name? <u>Al</u> | ||
Last name? <u>Gus</u> | Last name? <u>Gus</u> | ||
− | | + | agus#### |
First name? <u>Alma</u> | First name? <u>Alma</u> | ||
Last name? <u>Verylongname</u> | Last name? <u>Verylongname</u> | ||
− | | + | averylon |
<br /> | <br /> | ||
* Submit your program to HW 4 PB 2 on Moodle. | * Submit your program to HW 4 PB 2 on Moodle. | ||
Line 91: | Line 91: | ||
:;Example | :;Example | ||
:<source lang="text"> | :<source lang="text"> | ||
− | + | Your first name? maria | |
− | + | Your last name? LUCE | |
........M | ........M |
Latest revision as of 18:21, 23 February 2018
D. Thiebaut (talk) 10:33, 18 February 2018 (EST)
Once again, you have to work in programming pair mode for this assignment. This assignment is due on Thursday 3/1/18, at 11:55 p.m.
Make sure you use a main() function in all your programs! You may want to go through the homework preparation page (which would have been a lab, if it hadn't been Rally Day this week) first!
Possible grades: A, A- or B. Programs that crash before outputting any information get a C.
Make sure to pay attention to blank lines in the output of the programs. The solution programs print blank lines to separate the input section from the output section. If your program is missing the blank line, the autograder may see your output as different from the solution output!
<showafterdate after="20180218" before="20180601">
Problem #1
Write a program named hw4_1.py 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, plus a number equal to the number of characters in the string just created. 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 1
First name? Allie Last name? BABA Your computer account name is: ababa5
- Example 2
First name? kathleen Last name? MCCARTNEY Your computer account name is: kmccartney10
- Submit your program to HW 4 PB 1 on Moodle.
Problem #2
Write a program called hw4_2.py 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 '#'.
- Example
First name? Allie Last name? BABA ababa###
First name? Al Last name? Gus agus####
First name? Alma Last name? Verylongname averylon
- Submit your program to HW 4 PB 2 on Moodle.
Problem #3
Write a program called hw4_3.py 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 (2018 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: agus2018
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 called hw4_4.py 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
Your first name? maria Your 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 called hw4_5.py that prompts the user to enter a date representing the beginning and end of a semester, in the form mmdd-mmddyyyy, and displays it back as dd month yyyy to dd month yyyy, where month is the first 3 letters of the name of the month, in English.
- Example
Enter date of semester: 0129-05012018 29 Jan to 1 May 2018
Note that the day for May is 1, and not 01.
- Submit your program to HW 4 PB 5 on Moodle.
Problem #6
Write a program called hw4_6.py that asks the user for several pieces of information about some famous person, and outputs the information as illustrated below.
- 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 6 on Moodle.
</showafterdate>
<showafterdate after="20181009 00:00">
Solution Programs
# hw4sol.py
# D. Thiebaut
# Solution program(s) for Homework 4, 2018
# 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( "First name? " )
lName = input( "Last name? " )
print( )
full = fName[0] + lName
full = full.lower()
print( "Your computer account name is: {0:1}{1:1}".format( full, len(full) ) )
# solution for Problem 2
def problem2():
fName = input( "First name? " )
lName = input( "Last name? " )
full = fName[0] + lName + "#########"
full = full[0:8].lower()
print()
print( full)
# solution for Problem 3
def problem3():
fName = input( "First name? " )
lName = input( "Last name? " )
full = fName[0] + lName + "20182018"
full = full[0:8].lower()
print()
print( "Your computer account name is:", full)
# solution for Problem 4
def problem4():
fName = input( "Your first name? " ).capitalize()
lName = input( "Your last name? " ).capitalize()
print()
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 of semester: " )
# extract the different variables from the date
month1 = date[0:2]
day1 = date[2:4]
month2 = date[5:7]
day2 = date[7:9]
year = date[9:]
dayInt1 = int( day1 )
monthIndex1 = int( month1 ) - 1
monthStr1 = monthNames[ monthIndex1 ][0:3]
dayInt2 = int( day2 )
monthIndex2 = int( month2 ) - 1
monthStr2 = monthNames[ monthIndex2 ][0:3]
# display result
print()
print( dayInt1, monthStr1, "to", dayInt2, monthStr2, year )
# solution for Problem 6
def problem6():
# define the list of months as a string of 3-char names
fName = input( " Please enter first name: " )
print()
lName = input( "Please enter last name: " )
print()
fName = fName[0].upper() + fName[1:].lower()
lName = lName[0].upper() + lName[1:].lower()
# get the date from the user
monthNames = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
# get the date from the user
bdate = input( "Please enter date of birth (yyyymmdd): " )
print()
ddate = input( "Please enter date of death (yyyymmdd): " )
print()
accomplishment = input( "Enter accomplishment: " )
print()
# extract the different variables from the date
year1 = bdate[0:4]
month1 = bdate[4:6]
day1 = bdate[6:8]
year2 = ddate[0:4]
month2 = ddate[4:6]
day2 = ddate[6:8]
dayInt1 = int( day1 )
monthIndex1 = int( month1 ) - 1
monthStr1 = monthNames[ monthIndex1 ]
dayInt2 = int( day2 )
monthIndex2 = int( month2 ) - 1
monthStr2 = monthNames[ monthIndex2 ]
print()
# display result
print( fName, lName, "("+monthStr1, str(dayInt1)+"," ,
year1, "--", monthStr2, str(dayInt2)+",", str(year2)+")" )
# display result
print( accomplishment )
# 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()
main()
</showafterdate>