Skip to content

Comments

submitting assignment 2#2

Open
dianayangs wants to merge 3 commits intomainfrom
assignment-2
Open

submitting assignment 2#2
dianayangs wants to merge 3 commits intomainfrom
assignment-2

Conversation

@dianayangs
Copy link
Owner

What changes are you trying to make? (e.g. Adding or removing code, refactoring existing code, adding reports)

changes made are to complete assignment

What did you learn from the changes you have made?

defining functions are much simpler than I would assume

Was there another approach you were thinking about making? If so, what approach(es) were you thinking of?

Thought I needed more complex code

Were there any challenges? If so, what issue(s) did you face? How did you overcome it?

challenges in trusting myself. Also almost submitted a pull request to the main DSI branch OOPS

How were these changes tested?

ran the checks, got expected results

A reference to a related issue in your repository (if applicable)

Checklist

  • [X ] I can confirm that my changes are working as intended

Copy link

@tianyi21 tianyi21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Diana,

⚠️ You implemented the function to read and display a file.
✅ You completed the patient_summary() function to summarize a file.
✅ You implemented the detect_problems() function to check the file.

⚠️ In your function to read and display the file content, you included a print() statement and a for loop of print(). All given files contain 60 rows of data. In your version, could you comment on how many times print() statement are called? Is there a better way to read and print the content? You may follow the hint given after # YOUR CODE HERE: ....

Please revise your current submission to address the ⚠️ issue(s).

Thanks,
Tianyi [LS]

Copy link
Owner Author

@dianayangs dianayangs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed comments from Tianyi

Copy link

@tianyi21 tianyi21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the update. In your updated version, you removed the first print statement. In this case, will the first line of the file be printed?

You can use the commented lines as a hint, or alternatively, consider how many lines are in each file and how many print statements are needed to display the file content.

@dianayangs
Copy link
Owner Author

Thank you for the update. In your updated version, you removed the first print statement. In this case, will the first line of the file be printed?

You can use the commented lines as a hint, or alternatively, consider how many lines are in each file and how many print statements are needed to display the file content.

Hi Tianyi, I think I fixed it. Can you explain why my original code would print >60 lines? I was following an example in 08_reading_and_writing_files

Copy link

@tianyi21 tianyi21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Diana,

Thank you for updating your submission.

✅ Your version is correct! In the meantime, as we seen in the live learning, we may directly read the file with Python built-in/native IO function as follows:

with open(all_paths[0], 'r') as f:
    # YOUR CODE HERE: Use the readline() or readlines() method to read the .csv file into a variable
    # first = f.readline() # <- not needed here
    contents = f.readlines() # <- use native file IO, read everything into contents

    # YOUR CODE HERE: Iterate through the variable using a for loop and print each row for inspection
    # contents = csv.reader(f) # <- use native file IO with f above
    for row in contents: # <- the same way as how you print the content
        print(row)

In this case, we don't have to involve the csv package. However, in more complicated scenarios, please feel free to use any package.

🎉 Your A2 is complete!
🎊 All your Python assessments are now complete. Thank you for your participation, and I wish you all the best for your future!

Thanks,
Tianyi [LS]

@dianayangs
Copy link
Owner Author

I see, can you comment:

  1. If I am asked to read everything into the variable contents, why does the hint say to use readline()?
  2. is the ideal chunk of code you are looking for something like this:

with open(all_paths[0], 'r') as f:
contents = f.readlines()

for rows in contents:
print(row)

@tianyi21
Copy link

tianyi21 commented Oct 29, 2025

I see, can you comment:

  1. If I am asked to read everything into the variable contents, why does the hint say to use readline()?
  2. is the ideal chunk of code you are looking for something like this:

with open(all_paths[0], 'r') as f: contents = f.readlines()

for rows in contents: print(row)

For (1): you may use either the readline() or readlines() method to the read file. readline() reads one line at a time and readlines() reads everything altogether.

  • If you use readline(), one of the possible ways to display the content is:
with open(all_paths[0], 'r') as f:
    while True:                      # infinity loop to keep reading
        line = f.readline()          # read the line
        if line == "":               # if the line is empty, we have reached the end of the file (EOF), so break to leave the loop
            break
        print(line)                  # print the line if not empty
        ### < do more processing here if needed >
  • If you use readlines(), it becomes cleaner:
with open(all_paths[0], 'r') as f:
    lines = f.readlines()            # read everything into lines
    for line in lines:               # iterate through lines to print line
        print(line)
        ### < do more processing here if needed >

In this assignment, the files we are working with are quite small so either should be fine. In practice, if the file size is very large, using readline() may have some memory advantages since readlines() reads everything at once.

These are the file IO with Python built-in methods. For more efficient file handling, there are other packages available such as csv, pandas, and h5ad.

For (2): in this assignment, we are not looking for the best/optimal/cleanest code. As long as your version does what it's required, we will accept it.

@tianyi21
Copy link

tianyi21 commented Oct 29, 2025

I see, can you comment:

  1. If I am asked to read everything into the variable contents, why does the hint say to use readline()?
  2. is the ideal chunk of code you are looking for something like this:

with open(all_paths[0], 'r') as f: contents = f.readlines()

for rows in contents: print(row)

Thank you for the update. In your updated version, you removed the first print statement. In this case, will the first line of the file be printed?
You can use the commented lines as a hint, or alternatively, consider how many lines are in each file and how many print statements are needed to display the file content.

Hi Tianyi, I think I fixed it. Can you explain why my original code would print >60 lines? I was following an example in 08_reading_and_writing_files

Regarding total number of print, in your previous version, the print function ran in total 61 times right after the readline(). Even if the file has ended, readline() won't stop or throw an error by itself. We have to check whether we have reached the end of file by ourselves through if f.readline() == "": .... When the file has ended, f.readline() will return an empty string, i.e., "". This is equivalent to print an empty string as print("").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants