Could this be solve with list comprehension?

This initializes the variable to the first value in the list.

Iterate over the list

Replace the value with current number. Maximum is growing in value.

At the end of iteration, maximum will be the greatest value found in the list. There may be duplicates, but this method won’t be able to tell us that.

1 Like

I think there may be a problem with my console. My code returns the correct answer (75), but when it does I get the following error:

max_num([-50, -20]) should have returned -20, and it returned 0

When the nums list is:

max_num([50, -10, 0, 75, 20]

If I return the wrong answer, I get the correct error with the correct list:

max_num([50, -10, 0, 75, 20]) should have returned 75, and it returned 50

Again, just to reiterate, if I return the right answer the console changes the list and says I’ve returned the wrong answer.

EDIT: I just viewed the Solution and it was EXACTLY the same as my own code, which gave the weird error with the different list.

That would suggest that max was initialized to 0 rather than nums[0].

Expect the SCT to run multiple tests using different lists.

How about this script:
def max_num(nums):
maximum = nums[0]
for num in nums:
if maximum > num:
continue
else:
maximum = num
return maximum

print(max_num([50, -10, 0, 75, 20]))

We won’t be able to tell. Perhaps go back to the new users section and learn how to format code samples in posts. The link is everywhere yet some of us can never find it. I know it’s there, somewhere.

It is amazing how many budding coders this somehow slips by.

1 Like

It is the most often requested thing by moderators, with exercise link requests running a close second.

c’est ce que c’est

<span lang="fr" title="it is what it is">c'est ce que c'est</span>

Why this code does not get the negative list right?
like
[-50, -20] # it gives me -50

#Write your function here

def max_num(nums):
  larger = 0
  for i in range(len(nums)-1):
    if nums[i] > nums[i + 1]:
      larger = nums[i]
  return larger


#Uncomment the line below when your function is done
print(max_num([50, -10, 0, 75, 20]))

When I run your code using [-50, -20] I get 0.
Try these as well:

print(max_num([2, 6])) # prints 0
print(max_num([1,2,3,4,5,6])) # prints 0

Using a pen and paper, go through your code, and update your variables on the paper, or you could add print statements to see the values at each step.

1 Like

Is that the correct question to be asking?

Is that the correct initial value?

1 Like

It takes me some time but I think I got it.

def max_num(nums):
    larger = 0
    for i in range(len(nums) - 1):
        if nums[i] > nums[i + 1]:
            larger = nums[i]
        else:
            larger = nums[i + 1]
    return larger


print(max_num([2, 6]))  # prints 6
print(max_num([1, 2, 3, 4, 5, 6]))  # prints 6
print(max_num([-50, -20]))  # prints -20

let me know if you have any feedback
Thanks for the help

2 Likes

Hey Josh!
I had the same issue and dealt with it this way. I created a new variable and then, by using it, I found THE SMALLEST value in the list. After, using the same variable (which is now equals to the smallest value in list) I found the largest one. Maybe it’s not the most efficient way to solve this exercise, but it works.

def max_num(nums):
  maximum = 0
  for i in range(len(nums)):
    if nums[i] <= maximum:
      maximum = nums[i]
  for i in range(len(nums)):
    if nums[i] >= maximum:
      maximum = nums[i]
  return maximum

Should it be set to zero? Or would it be better to take a value from the list?

Your code above could technically return both the minimum and the maximum in one go, assuming they are both initialized correctly.

return minimum, maximum

This is because You’re setting up var maximum as = 0 and negative numbers are always smaller than 0 so it would always return 0 when you’re comparing it with negative numbers

Is it good or i can make it more efficient? suggest me

def max_num(nums):
  for i in range(len(nums)):
    for k in range(len(nums)):
      if nums[i] < nums[k+1]:
        i = k + 1
        break
    return nums[i]

Welcome to the forums, @digital2728968217. Not sure why the return is inside the outer loop. Can you explain your logic?

>>> max_num([320, 189, 67, 54, 98, 42, 256])
Traceback (most recent call last):
  File "<pyshell#151>", line 1, in <module>
    max_num([320, 189, 67, 54, 98, 42, 256])
  File "<pyshell#148>", line 4, in max_num
    if nums[i] < nums[k+1]:
IndexError: list index out of range
>>> 

I used return inside for loop to stop first “for loop” from picking next element. But that code was not giving required result so i updated my code. please check it out and suggest me if any correction require:

def max_num(nums):
  greatest = nums[0]
  for i in range(len(nums)):
    if nums[i] > greatest:
      greatest = nums[i]
  return greatest

i think this is an another way to get greatest number but it is not that efficient.

def max_num(nums):
  for i in range(len(nums)):
    for k in range(len(nums)):
      if nums[i] >= nums[k]:
        if k == len(nums)-1:
          greatest = nums[i]
          return greatest

Since the previous post contains a working version, what is gained by adding to the complexity? Also, a return nested in the inner function is never a good sign.

In the previous post my code was checking for “if nums[i] < nums[k]”. If no number is less than nums[i] i could not able to return the value of nums[i] anywhere. if i put return outside of the loop then it will pick another nums[i].
At the end that code had too many drawbacks so i dropped it and rewrite this code to implement the same logic but with different approach.

1 Like