-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex13.py
73 lines (52 loc) · 1.63 KB
/
ex13.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ex13.py
'''
* Implement a function known as all(iterable). This function returns True if all of
the elements of the iterable is True or the iterable is empty, else False.
* Implement a function known as any(iterable) . This function returns True if any of
the elements of the iterable is True . If the iterable is empty, return False.
* Implement the abs(x) function where x , is a number.
* Implement a function that returns the minimum and maximum numbers in given list.
Don't use the built-in function min and max.
'''
def all(iterable=[]):
for i in iterable:
if not i:
return False
return True
def any(iterable):
for i in iterable:
if i:
return True
return False
# since `abs` is a built-in function, I shall append `_` to mine as `abs_`
def abs_(n):
result = 0
try:
if n < 0:
result = n * -1
else:
result = n
except Exception:
print('Enter a number please')
finally:
return result
def sort_list(item=[]):
item_length = len(item)
if item_length > 1:
for i in range(item_length):
for j in range(item_length - 1):
if item[j] > item[j + 1]:
item[j], item[j + 1] = item[j + 1], item[j]
return item
def min_max(iterable=[]):
min_index = 0
last_index = len(iterable) - 1
result = ()
try:
sorted_iterable = sort_list(iterable)
result = (sorted_iterable[min_index],
sorted_iterable[last_index])
except Exception:
print('The iterable must not be empty')
finally:
return result