Question

Python Write a function named minGap that takes in a list of integers and returns the...

Python

Write a function named minGap that takes in a list of integers and returns the minimum ‘gap’ between values in the list. The gap between two adjacent values in a list is defined as the second value minus the first value. For example, suppose we have the following list: [1, 3, 6, 7, 12] The first gap is between indices 0 and 1 and its value is 3 − 1 = 2. The second gap is 6 − 3 = 3. The smallest gap is 1, which is between indices 2 and 3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def minGap(lst):
    gap = None
    for i in range(1, len(lst)):
        if gap is None or lst[i] - lst[i - 1] < gap:
            gap = lst[i] - lst[i - 1]
    return gap


print(minGap([1, 3, 6, 7, 12]))
Add a comment
Know the answer?
Add Answer to:
Python Write a function named minGap that takes in a list of integers and returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT