Tuesday, September 10, 2019

IslandT: Find the maximum gap between the successive numbers in its sorted form from a Python list

Given a Python list consists of plus or minus numbers, we need to sort that list then find the maximum gap between the successive numbers in that list regarding of its sign.

For example,

maxGap ({-7,-42,-809,-14,-12}) ==> return (767)

Explanation:

The Maximum Gap after sorting the array is 767, The difference between | -809- (-42) | = 767.
def max_gap(numbers):

    numbers.sort()
    gap = []

    while(len(numbers) > 1):
        first = numbers.pop(0)
        second = numbers[0]
        gap.append(abs(second-first))
    
    return max(gap)
  1. Sort the list in ascending order.
  2. Create a full gap list in the while loop.
  3. Return the maximum different.

The above question is from Codewars, which is the best website for you to train your Python and other programming skill.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...