DEV Community

Cover image for Python challenge_10
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python challenge_10

Min-maxing

level of challenge 3/10

Min-maxing

  • Define a function named largest_difference
  • that takes a list of numbers as its only parameter.
  • Your function should compute and return the difference
  • between the largest and smallest number in the list.

For example:

  • the call largest_difference([1, 2, 3])
  • should return 2 because 3 - 1 is 2.

You may assume that no numbers are smaller or larger than -100 and 100.

Hint:

  • Split the problem up into sub problems:
  • First find the smallest number.
  • Then find the largest number.
  • Then compute their difference and return it.
  • To find the smallest number you can use the min() built-in.
  • Alternatively you can set smallest = 100 and loop over each number in the input list. Whenever you see a smaller one, set smallest to it.
# My solution

def largest_difference(numbers):
 sm_number = numbers
 la_number = numbers
 smaller = min(sm_number)
 largest = max(la_number)
 return largest - smaller

print(largest_difference([1, 2, 3, 5, 6]))

Enter fullscreen mode Exit fullscreen mode
# short solution

def largest_difference(numbers):
  return max(numbers) - min(numbers)

Enter fullscreen mode Exit fullscreen mode
# Another solution

def largest_difference(numbers):
 smallest = 100
 for n in numbers:
  if n < smallest:
   smallest = n

 largest = -100
 for n in numbers:
  if n > largest:
    largest = n

 difference = largest - smallest
 return difference

Enter fullscreen mode Exit fullscreen mode

Latest comments (4)

Collapse
 
elnaznasiri profile image
elnaz-nasiri

My solution is:

def largest_difference(list1):
list1.sort()
minList = list1[0]
maxList = list1[-1]
return maxList - minList

print(largest_difference([5,8,9,12,1]))

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thanks for sharing you can test your solution here: pythonprinciples.com/challenges/

Collapse
 
cyzy666 profile image
Cyzy666

I think another "short" solution would be to use the sorted() function. Like this:

def largest_difference(nums):
    return sorted(nums)[-1] - sorted(nums)[0]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thanks for sharing you can test your solution here: pythonprinciples.com/challenges/