-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxmin.py
More file actions
84 lines (73 loc) · 3.26 KB
/
maxmin.py
File metadata and controls
84 lines (73 loc) · 3.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def min_element(*args):
'''
@Brief: Method is to find minimum element from the given input
Description: min_element() is a generic function with variable arguments.
It finds minimum element from different containers.
Examples :
1) min_element([1,2,3,4,5]) -> 1
2) min_element([1,2,3,4],{-1,-2,3,4}) -> -1
3) min_element([1,2,3,-10,200], -1000) -> -1000
Practical scenarios :
1) Using two differnet library APIs and one returns list and other returns a set,
today we don't have any ways to find min element from both container
2) Program has multiple lists and need to find min from all the lists,
today to find min all list objects has to get merged and then min() will be performed
'''
if not isinstance(args[0], (int,float,list,set,tuple)):
return
foundMin = False
for arg in args:
if isinstance(arg, (int,float)):
lowest = arg
else:
lowest = min(arg)
if foundMin == False:
minimum = lowest
foundMin = True
elif lowest < minimum:
minimum = lowest
return minimum
def max_element(*args):
'''
@Brief: Method is to find maximum element from the given input
Description: max_element() is a generic function with variable arguments.
It finds maximum element from different containers.
Examples :
1) max_element([1,2,3,4,5]) -> 5
2) max_element([1,2,3,4],{-1,-2,3,4}) -> 4
3) max_element([1,2,3,-10,200], 100) -> 200
Practical scenarios :
1) Using two differnet library APIs and one returns list and other returns a set,
today we don't have any ways to find max element from both container
2) Program has multiple lists and need to find max from all the lists,
today to find max all list objects has to get merged and then max() will be performed
'''
if not isinstance(args[0], (int,float,list,set)):
return
foundMax = False
for arg in args:
if isinstance(arg, (int,float)):
highest = arg
else:
highest = max(arg)
if foundMax == False:
maximum = highest
foundMax = True
elif highest > maximum:
maximum = highest
return maximum
def minmax_element(*args):
'''
@Brief: Method is to find minmax elements from the given input container
Description: minmax_element() is a generic function with variable arguments.
It finds minimum and maximum element from different/multiple containers and
returns a tuple (min,max).
Examples :
1) minmax_element([1,2,3,4,5]) -> (1,5)
2) minmax_element([1,2,3,4],{-1,-2,3,4}) -> (-1,4)
3) minmax_element([1,2,3,-10,200], 100) -> (-10,200)
Practical scenarios :
1) It is very usefull when application needs max and min element. Python does not have a
single function which queries multiple containers and get min and max element from it.
'''
return (min_element(*args),max_element(*args))