COMM 337: Homework 2
DUE DATE: Oct 2, 2024 12:00pm
This is HW2 Python assignment. There are 13 problems. Each problem is7 points and the total is 100 points (9 for following file/print format).Instructions:
- Late submissions will not be accepted.
- Submit your Python code in UBC Canvas. DO NOT email your homework toTA or me.
- Don’t submit the output txt file. I will run your Python code to generate it.(Initial Setup) Create and download:
- Create a new python notebook for this homework and download the“last.txt” file into the same directory, where your notebook is located.
Write your Python code in your notebook file for Problems 2 to 20.
- Open “last.txt” file with “read-only” file mode.
- Find the number of characters in the file and assign it to “ans3”variable.
- Find the number of words (including both numbers and names) in thefile and assign it to “ans4” variable.
- Find the number of lines in the file and assign it to “ans5” variable.
- Count the last names starting with “L” and assign it to “ans6” variable.
- Count the last names ending with “E” and assign it to “ans7” variable.
- Count the last names with length 3 and assign it to “ans8” variable.
- Count the numbers larger than 0.1 and assign it to “ans9” variable.
- Count the numbers smaller than 0.02 and assign it “ans10”variable.
- Get the number next to your last name and assign it to “ans11”variable. If your last name doesn’t appear, “ans11” should be 0.
- Find the last name that comes last in the dictionary order andassign it to “ans12” variable.
- Find the last name that comes first in the dictionary order andassign it to “ans13” variable.
- Find the longest last name and assign it to “ans14” variable.
- Find the shortest last name and assign it to “ans15” variable.(Note: don’t worry about the weird last name in the last.txt file. I put itthere intentionally.)
- Create a new file “hw2_answers_STUDENTID_FIRST代 写COMM 337 Python code in your notebook _LAST.txt” usingopen() function with “write” mode, where FIRST and LAST should beyour first and last names. For example,hw2_answers_1234_JANE_DOE.txt will be my output file.
- Write your student id, first name, last name, and email address inthe first line of “hw2_answers_STUDENTID_FIRST_LAST.txt” file.
- Write all your answers into the“hw2_answers_STUDENTID_FIRST_LAST.txt” file. Each answer will takea line with “answerXX=YY” format. Use your ansXX variables. Don’t dohard coding by using the actual values.
- Write “Homework 2 is done!!!” in the last line of“hw2_answers_STUDENTID_FIRST_LAST.txt” file.
- Close “ hw2_answer_STUDENTID_FIRST_LAST.txt” file using close()method.
Submission:
Export your file as a .py file and submit it to Canvas. Please DO NOT submit your .ipynb file. Also, make sure you EXPORT the filefrom jupyter and not just rename the file extension. You do NOT needto submit your generated output text file.Your output text file should look like this:1234 Jane Doe [email protected]
answer3=XX
answer4=XX
answer5=XX
answer6=XX
answer7=XX
answer8=XX
answer9=XX
answer10=XX
answer11=XX
answer12=XX
answer13=XX
answer14=XX
answer15=XX
Homework 2 is done!!!
标签:code,last,Python,337,assign,XX,file,variable,your From: https://www.cnblogs.com/comp9021/p/18442738