Given K delimiter seperated String Numbers, extracted maximum of it.
Examples :
Input : test_str = '54*345*23*45*234', delim = '*' Output : 345 Explanation : Maximum element between splits is 345. Input : test_str = '547*345*23*45*234', delim = '*' Output : 547 Explanation : Maximum element between splits is 547.
Method #1 : Using loop + split() + int() :
In this, we split all the numbers into individuals using split(), then convert into integer using int(), maximum is found using looping and saving max at each iteration.
Implementation :
# Python3 code to demonstrate working of
# Maximum of K separated String Numbers
# Using loop + split() + int()
# initializing string
test_str = '54:345:23:45:234'
# printing original string
print("The original string is : " + str(test_str))
# initializing K delim
delim = ':'
res = 0
# spliting strings
for ele in test_str.split(delim):
# checking for max element
if int(ele) > res :
res = int(ele)
# printing result
print("The Maximum element : " + str(res))
Method #2 : Using max() + list comprehension :
This is yet another way to solve this, maximum is computed using max() and list comprehension is used to iteration, and type conversion using int().
# Python3 code to demonstrate working of
# Maximum of K separated String Numbers
# Using max() + list comprehension
# initializing string
test_str = '54:345:23:45:234'
# printing original string
print("The original string is : " + str(test_str))
# initializing K delim
delim = ':'
# one-liner to solve the problem
res = max([int(ele) for ele in test_str.split(delim)])
# printing result
print("The Maximum element : " + str(res))