STEM

Python teaser/puzzle 45588-1338

This is a quick Python coding puzzle or brain teaser for educational and/or entertainment purposes. The challenge is quite simple but is useful for learning or refreshing your coding skills while exercising some logical thinking.

Objective

Write a function that returns the maximum difference between any two elements from a list of numbers. If the list contains fewer than two elements, the function should return 0. This should work for both integers and floats.

For example, given the list 2, 7, 1, 9, 5, 11, 3 the maximum difference is 10, which is obtained by subtracting 1 from 11.

Click Run on the widget below to run the solution.

Try to solve it yourself first. If you’re stumped, click here for the solution.

Be sure to check out my Patreon page for downloads with more advanced apps, solutions, tips on Python, Excel, etc. here.

For more brain teasers and puzzles, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top