6-01: Model Solution: What if the largest number is smaller than 0?
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rage/programming-26
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
rage/programming-26#74 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
rage/programming-26#68 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
rage/programming-26#62 ·
-
A typo in Part 4 Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
rage/programming-26#56 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
rage/programming-26#55 ·
All issues in rage/programming-26
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·
-
client-controller-update ta-bot-triage team-money-movement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MetaMask/metamask-mobile#36594 ·