-5

I have been asked to find the min and max of this list without using the min and max function in Python. I am not sure how to do that. Please help.

AList = 1,2,3,4
miradulo
  • 28,857
  • 6
  • 80
  • 93
rekezelman
  • 19
  • 1
  • 1
  • 3
    It would probably be most helpful for you if you share the code you've already tried, and we point out what mistakes you've made. Generally, we avoid writing answers to homework problems from nothing. – Patrick Haugh Jun 17 '18 at 22:40

2 Answers2

0

One way would be using a for loop:

def get_min_max(numbers):
    if not all([type(number) in [int, float] for number in numbers]):
        raise TypeError('All items in numbers must be int or float.')

    min = None
    max = None
    for number in numbers:
        min = number if min is None or number < min else min
        max = number if max is None or number > max else max

    return min, max
Renato Byrro
  • 3,578
  • 19
  • 34
-1

As @figbeam suggested, you can sort and retrieve the first and final value as such:

AList = [1,2,3,4]

sorted_list = sorted(AList)
minValue = sorted_list[0]
maxValue = sorted_list[-1]

print (minValue)
print (maxValue)

There are additional ways in case that example doesn't do it for you (such as making a copy of the list, then iterating through and reassigning the min (or max) based on the next entry.

Darican
  • 29
  • 5
  • Why make a copy? AnywayI think if the assignment doesn't allow for `min` and `max` it probably doesn't allow for `sorted` which is much less trivial... – juanpa.arrivillaga Jun 17 '18 at 23:36
  • Agree. Depends on whether sorted doesn't work. And I'd make a copy just to iterate the original versus the copy and do my comparisons that way. Probably not logical but it works (done it before. No idea why I did) – Darican Jun 19 '18 at 00:10