|
| 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) |
0 commit comments