-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.py
More file actions
57 lines (37 loc) · 1.1 KB
/
range.py
File metadata and controls
57 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Given list of numbers, return smallest & largest number as a tuple.
For example::
>>> find_range([3, 4, 2, 5, 10])
(2, 10)
>>> find_range([43, 3, 44, 20, 2, 1, 100])
(1, 100)
For an empty list, it should return `None` as both smallest and largest::
>>> find_range([])
(None, None)
Make sure it works with a list of one item, which is both smallest and
largest::
>>> find_range([7])
(7, 7)
"""
def find_range(nums):
"""Given list of numbers, return smallest & largest number as a tuple."""
# Simple solution leveraging built-in min/max:
# if not nums:
# return (None, None)
# return (min(nums), max(nums))
# Alternatively...without using min/max or sort...
if not nums:
return (None, None)
min = nums[0]
max = nums[0]
for num in nums:
if num > max:
max = num
elif num < min:
min = num
else:
continue
return (min, max)
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. HOORAY!\n"