Skip to content

Commit 49ff3eb

Browse files
committed
1. Added method for query min/max element from a container.
2. Added menthod for lower and upper bounds of a given number.
1 parent e475e9c commit 49ff3eb

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed

lowerupperbound.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
def lower_bound(container, num):
3+
'''
4+
@Brief: Method is to find lower bound of a given number 'num' in given container
5+
6+
Description: lower_bound() function returns index of the greatest value less than or equal to 'num'
7+
from given container
8+
Examples :
9+
1) lower_bound([],12) -> 0th index :
10+
Returns first index of the container
11+
2) lower_bound([10,2,3,4,7,11],1) -> 0th index :
12+
In case container does not have any number smaller than 'num' it returns 0th
13+
3) lower_bound([1,2,3,90,4,7,11],12) -> 6
14+
11 is the largest value less than 'num'(12) hence function returns 6th as container index
15+
4) lower_bound([1,2,3,90,4,7,11],11) -> 6
16+
11 is the largest value less than equal to 'num'(11) hence function returns 6th as container index
17+
18+
Practical scenarios :
19+
1) Function can be used to find smallest deviation from given input
20+
2) In case of error margine one can use the function to see how close results to expected value
21+
'''
22+
foundLowerBound = False
23+
index = 0
24+
lowerBoundIdx = 0
25+
for ele in container:
26+
if ele <= num:
27+
if not foundLowerBound:
28+
lowerBoundIdx = index
29+
foundLowerBound = True
30+
elif container[lowerBoundIdx] < ele:
31+
lowerBoundIdx = index
32+
index = index + 1
33+
return lowerBoundIdx
34+
35+
def upper_bound(container, num):
36+
'''
37+
@Brief: Method is to find upper bound of a given number 'num' in given container
38+
39+
Description: upper_bound() function returns index of the smallest value greater than or equal to 'num'
40+
from given container
41+
Examples :
42+
1) upper_bound([],12) -> 0th index :
43+
Returns first index of the container
44+
2) upper_bound([10,2,3,4,7,11],100) -> 0th index :
45+
In case container does not have any number greater than 'num' it returns 0th
46+
3) upper_bound([1,2,3,90,4,7,11],12) -> 3
47+
90 is the largest value less than 'num'(12) hence function returns 3rd as container index
48+
4) upper_bound([1,2,3,90,4,7,11],11) -> 6
49+
11 is the smallest value greater than equal to 'num'(11) hence function returns 6th as container index
50+
51+
Practical scenarios :
52+
1) Function can be used to find deviation from given input
53+
2) In case of error margine one can use the function to see how close results to expected value
54+
'''
55+
foundUpperBound = False
56+
index = 0
57+
upperBoundIdx = 0
58+
for ele in container:
59+
if ele >= num:
60+
if not foundUpperBound:
61+
upperBoundIdx = index
62+
foundUpperBound = True
63+
elif container[upperBoundIdx] > ele:
64+
upperBoundIdx = index
65+
index = index + 1
66+
return upperBoundIdx
67+
68+
def lower_upper_bound(container, num):
69+
'''
70+
@Brief: Method is to find lower and upper bound of a given number 'num' in given container
71+
72+
Description: lower_upper_bound() function returns tuple of lower_bound and upper_bound of a given number
73+
lower_bound : Greatest number smaller than 'num'
74+
upper_bound : Smallest number greater than 'num'
75+
Example:
76+
lower_upper_bound([1,2,3,90,4,7,11],12) -> (6,3)
77+
Lower bound is at 6th index and upper boud is at 3rd index
78+
79+
Practical scenarios :
80+
1) It is more useful when one needs to find out closest range of a number
81+
82+
'''
83+
foundUpperBound = False
84+
foundLowerBound = False
85+
index = 0
86+
upperBoundIdx = 0
87+
lowerBoundIdx = 0
88+
for ele in container:
89+
if ele < num:
90+
if not foundLowerBound:
91+
lowerBoundIdx = index
92+
foundLowerBound = True
93+
elif container[lowerBoundIdx] < ele:
94+
lowerBoundIdx = index
95+
elif ele > num:
96+
if not foundUpperBound:
97+
upperBoundIdx = index
98+
foundUpperBound = True
99+
elif container[upperBoundIdx] > ele :
100+
upperBoundIdx = index
101+
else:
102+
lowerBoundIdx = index
103+
upperBoundIdx = index
104+
105+
index = index + 1
106+
return (lowerBoundIdx,upperBoundIdx)

maxmin.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
def min_element(*args):
2+
'''
3+
@Brief: Method is to find minimum element from the given input
4+
5+
Description: min_element() is a generic function with variable arguments.
6+
It finds minimum element from different containers.
7+
Examples :
8+
1) min_element([1,2,3,4,5]) -> 1
9+
2) min_element([1,2,3,4],{-1,-2,3,4}) -> -1
10+
3) min_element([1,2,3,-10,200], -1000) -> -1000
11+
Practical scenarios :
12+
1) Using two differnet library APIs and one returns list and other returns a set,
13+
today we don't have any ways to find min element from both container
14+
2) Program has multiple lists and need to find min from all the lists,
15+
today to find min all list objects has to get merged and then min() will be performed
16+
'''
17+
if not isinstance(args[0], (int,float,list,set,tuple)):
18+
return
19+
20+
foundMin = False
21+
for arg in args:
22+
if isinstance(arg, (int,float)):
23+
lowest = arg
24+
else:
25+
lowest = min(arg)
26+
27+
if foundMin == False:
28+
minimum = lowest
29+
foundMin = True
30+
elif lowest < minimum:
31+
minimum = lowest
32+
33+
return minimum
34+
35+
def max_element(*args):
36+
'''
37+
@Brief: Method is to find maximum element from the given input
38+
39+
Description: max_element() is a generic function with variable arguments.
40+
It finds maximum element from different containers.
41+
Examples :
42+
1) max_element([1,2,3,4,5]) -> 5
43+
2) max_element([1,2,3,4],{-1,-2,3,4}) -> 4
44+
3) max_element([1,2,3,-10,200], 100) -> 200
45+
Practical scenarios :
46+
1) Using two differnet library APIs and one returns list and other returns a set,
47+
today we don't have any ways to find max element from both container
48+
2) Program has multiple lists and need to find max from all the lists,
49+
today to find max all list objects has to get merged and then max() will be performed
50+
'''
51+
if not isinstance(args[0], (int,float,list,set)):
52+
return
53+
54+
foundMax = False
55+
for arg in args:
56+
if isinstance(arg, (int,float)):
57+
highest = arg
58+
else:
59+
highest = max(arg)
60+
61+
if foundMax == False:
62+
maximum = highest
63+
foundMax = True
64+
elif highest > maximum:
65+
maximum = highest
66+
67+
return maximum
68+
69+
def minmax_element(*args):
70+
'''
71+
@Brief: Method is to find minmax elements from the given input container
72+
73+
Description: minmax_element() is a generic function with variable arguments.
74+
It finds minimum and maximum element from different/multiple containers and
75+
returns a tuple (min,max).
76+
Examples :
77+
1) minmax_element([1,2,3,4,5]) -> (1,5)
78+
2) minmax_element([1,2,3,4],{-1,-2,3,4}) -> (-1,4)
79+
3) minmax_element([1,2,3,-10,200], 100) -> (-10,200)
80+
Practical scenarios :
81+
1) It is very usefull when application needs max and min element. Python does not have a
82+
single function which queries multiple containers and get min and max element from it.
83+
'''
84+
return (min_element(*args),max_element(*args))

test.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
import maxmin as Mm
3+
import lowerupperbound as Lu
4+
5+
#================ Feature : max/min element : Test start =======================#
6+
'''
7+
Test case to find minimum from different containers
8+
'''
9+
# Find and print minimum element from multiple containers
10+
print(Mm.min_element([1,2,3,4],[-1,2,3,4],(9,-10)))
11+
12+
# Find minimum from multiple integer elements
13+
a,b,c,d = 1,10,-1,5
14+
print(Mm.min_element(a,b,c,d))
15+
16+
17+
'''
18+
Test case to find maximum from different containers
19+
'''
20+
# Find and print maximum element from multiple containers
21+
print(Mm.max_element([1,2,3,4],[-1,2,3,4],(9,-10)))
22+
23+
# Find maximum from multiple integer elements
24+
a,b,c,d = 1,10,-1,50
25+
print(Mm.max_element(a,b,c,d))
26+
27+
'''
28+
Test case to find minimum and maximum from different containers
29+
'''
30+
# Find and print minimum and maximum element from multiple containers
31+
print(Mm.minmax_element([1,2,3,4],[-1,2,3,4],(9,-10)))
32+
33+
# Find minimum and maximum from multiple integer elements
34+
a,b,c,d = 1,11,-7,15
35+
print(Mm.minmax_element(a,b,c,d))
36+
37+
38+
#================ Feature : lower/upper bounds : Test start =======================#
39+
40+
'''
41+
Find lower and upper bounds of a given number in a given series
42+
'''
43+
44+
#Find lower bound of a number from an empty container
45+
print(Lu.lower_bound([],12))
46+
47+
#Find lower bound of a number from an container where all numbers are greater than the number
48+
print(Lu.lower_bound([10,2,3,4,7,11],1))
49+
50+
# Find lower bound of a number when number itself is spresent in the list
51+
print(Lu.lower_bound([1,2,3,90,4,7,11],11))
52+
53+
#Find lower bound
54+
print(Lu.lower_bound([1,2,3,90,4,7,11],12))
55+
56+
57+
# Find lower and upper bound of a number in the given list where number is present
58+
print(Lu.lower_upper_bound([1,2,3,90,4,7,11],11))
59+
60+
# Find lower and upper bound of a number
61+
print(Lu.lower_upper_bound([1,2,3,90,4,70,10],11))

0 commit comments

Comments
 (0)