A slightly easier way to do this might be to first extract the list of integers, and then apply max and min on it
def high_and_low(numbers):
nums = list(map(int, numbers.split()))
return min(nums), max(nums)
Last Updated : 13 Jul, 2022
Examples:
Input: 100 klh564abc365bg
Output: 564
Maximum numeric value among 100, 564
and 365 is 564.
Input: abchsd0sdhs
Output: 0
- 1) If a numeric value is present at the current index then convert it into an integer
num = num * 10 + (str[i] - '0')
564
564
564
In this method, we will find the numbers within the input string using re.findall() method. Then we will cast the number into integer using map() and then use max() to find the maximum numeric value from the list.,Step4: Convert the number into integer using map() method.,Here is the python program to find maximum numeric value from the string using regex.,Step5: Find the maximum numeric value using max()
Here is the python program to find maximum numeric value from the string using regex.
# import module for regular expression and collections import re #input string = 'ab12cd123ef23' #seperate number from string number = re.findall('\d+', string) #convert it into integer number = map(int, number) print("Max_value:", max(number))
Let us take one more example for finding max value.
# import module for regular expression and collections import re #input string = '1bc125cf200c4' #seperate number from string number = re.findall('\d+', string) #convert it into integer number = map(int, number) print("Max_value:", max(number))
The max() function returns the largest item in an iterable. It can also be used to find the largest item between two or more parameters.,If you need to find the smallest item, you can use the Python min() function.,Basically, the max() function finds the largest item between two or more objects.,To find the largest object between two or more parameters, we can use this syntax:
Example
numbers = [9, 34, 11, -4, 27] # find the maximum number max_number = max(numbers) print(max_number) # Output: 34
Example
numbers = [9, 34, 11, -4, 27] # find the maximum number max_number = max(numbers) print(max_number) # Output: 34
The max()
function has two forms:
# to find the largest item in an iterable
max(iterable, * iterables, key,
default)
# to find the largest item between two or more objects
max(arg1, arg2, * args, key)
To find the largest item in an iterable, we use this syntax:
max(iterable, * iterables, key,
default)
Output
The largest number is: 10
Example 2: the largest string in a list
languages = ["Python", "C Programming", "Java", "JavaScript"]
largest_string = max(languages);
print("The largest string is:", largest_string)
Divide and Conquer: Tournament Method,Write a recursive function accepting the array and its start and end index as parameters,Find the middle element among three numbers,How do we analyze the recursion by the master's theorem and recursion tree method?
Pseudo-Code
int[] getMinMax(int A[], int n) {
int max = A[0]
int min = A[0]
for (i = 1 to n - 1) {
if (A[i] > max)
max = A[i]
else if (A[i] < min)
min = A[i]
}
// By convention, let ans[0] = maximum and ans[1] = minimum
int ans[2] = {
max,
min
}
return ans
}
Pseudo Code
int[] findMinMax(int A[], int start, int end) {
int max;
int min;
if (start == end) {
max = A[start]
min = A[start]
} else if (start + 1 == end) {
if (A[start] < A[end]) {
max = A[end]
min = A[start]
} else {
max = A[start]
min = A[end]
}
} else {
int mid = start + (end - start) / 2
int left[] = findMinMax(A, start, mid)
int right[] = findMinMax(A, mid + 1, end)
if (left[0] > right[0])
max = left[0]
else
max = right[0]
if (left[1] < right[1])
min = left[1]
else
min = right[1]
}
// By convention, we assume ans[0] as max and ans[1] as min
int ans[2] = {
max,
min
}
return ans
}
For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation :
T(n) = 2 T(n / 2) + 2
T(2) = 1
T(1) = 0
We can solve this recurrence relation by master method / recursion tree method.
if n is a power of 2
T(n) = 3n / 2 - 2
The maximum and minimum is useful for numbers and that’s it, correct?,We can also use max and min to determine minima or maxima of strings in an alphabetic order. We can do that by simply inserting a character vector (or column or row) between the parentheses of the max and min functions.,Sure, the typical application of max and min is to columns. But sometimes it might be useful to know the maximum or minimum of a row. We can compute that with the following R code:,Let’s assume that we want to know the maximum and minimum value of the columns mpg and cyl. We can calculate that with the following R codes for max…
max(x) min(x)
x1 < -c(4, 1, -50, 20, 8) # Create example vector
max(x1) # Apply max to vector
# 20
min(x1) # Apply min to vector
# - 50
x2 < -c(x1, NA) # Create example vector with NA
x2 # Print vector to RStudio console
# 4 1 - 50 20 8 NA
max(x2) # max returns NA # NA