COMM 337 Python code in your notebook

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:

  1. Late submissions will not be accepted.
  2. Submit your Python code in UBC Canvas. DO NOT email your homework toTA or me.
  1. Don’t submit the output txt file. I will run your Python code to generate it.(Initial Setup) Create and download:
  1. 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.

  1. Open “last.txt” file with “read-only” file mode.
  2. Find the number of characters in the file and assign it to “ans3”variable.
  1. Find the number of words (including both numbers and names) in thefile and assign it to “ans4” variable.
  1. Find the number of lines in the file and assign it to “ans5” variable.
  2. Count the last names starting with “L” and assign it to “ans6” variable.
  3. Count the last names ending with “E” and assign it to “ans7” variable.
  4. Count the last names with length 3 and assign it to “ans8” variable.
  5. Count the numbers larger than 0.1 and assign it to “ans9” variable.
  6. Count the numbers smaller than 0.02 and assign it “ans10”variable.
  1. 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.
  1. Find the last name that comes last in the dictionary order andassign it to “ans12” variable.
  1. Find the last name that comes first in the dictionary order andassign it to “ans13” variable.
  1. Find the longest last name and assign it to “ans14” variable.
  1. 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.)
  1. 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.
  1. Write your student id, first name, last name, and email address inthe first line of “hw2_answers_STUDENTID_FIRST_LAST.txt” file.
  1. 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.
  1. Write “Homework 2 is done!!!” in the last line of“hw2_answers_STUDENTID_FIRST_LAST.txt” file.
  1. 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 jane.doe@sauder.ubc.ca

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!!!

posted @ 2024-10-01 10:46  n58h2r  阅读(0)  评论(0编辑  收藏  举报