6-01: Model Solution: What if the largest number is smaller than 0?

Open Beginner friendly
#52 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
content

Research direction

Inspect the model solution for exercise 6-01 and reproduce the behavior with a list containing only negative numbers. Update the solution so it returns the largest input number rather than 0, then verify that it still handles positive and mixed-number inputs.

Written by the indexing model from the issue text.

Description

The model solution assigns 0 as the biggest number but the question didn't specify that the question will always have at least one non-negative number.

What if the list only have negative numbers?
In that case instead of returning the largest number, it will return 0.

def largest():
    with open("numbers.txt") as file:
        start = True
        biggest = 0
        for number in file:
            if start or int(number) > biggest:
                biggest = int(number)
                start = False
        return biggest

A simple solution would be to store the first number as the largest number instead. I am adding my solution but I believe this could be shortened:

def largest():
    with open("numbers.txt") as new_file:

        flag = True

        for line in new_file:
            line = int(line.replace("\n",""))

            #store the first value then skip all else
            if flag:
                largest_number = line
            flag = False

            if largest_number<line:
                largest_number = line
    return largest_number

if __name__ == "__main__":
    largest()
Dominant language
JavaScript
Stars
77
Forks
30
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rage/programming-26

All issues in rage/programming-26

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.