w3resource

Python: Find the largest number where commas or periods are decimal points

Python Programming Puzzles: Exercise-26 with Solution

Write a Python program to find the largest number where commas or periods are decimal points.

Input:
['100', '102,1', '101.1']
Output:
102.1

Pictorial Presentation:

Python: Find the largest number where commas or periods are decimal points .

Sample Solution-1:

Python Code:

#License: https://bit.ly/3oLErEI

def test(str_nums):
    return max(float(s.replace(",", ".")) for s in str_nums)
str_nums = ["100", "102,1", "101.1"]
print("Original list:")
print(str_nums)
print("Largest number where commas or periods are decimal points:")
print(test(str_nums))

Sample Output:

Original list:
['100', '102,1', '101.1']
Largest number where commas or periods are decimal points:
102.1

Flowchart:

Flowchart: Python - Find the largest number where commas or periods are decimal points.

Sample Solution-2:

Python Code:

#License: https://bit.ly/3oLErEI

def test(str_nums):
    numbers = []
    for s in str_nums:
        numbers.append(float(s.replace(",", ".")))
    numbers.sort()
    return numbers[-1]

str_nums = ["100", "102,1", "103.1"]
print("Original list:")
print(str_nums)
print("Largest number where commas or periods are decimal points:")
print(test(str_nums))

Sample Output:

Original list:
['100', '102,1', '103.1']
Largest number where commas or periods are decimal points:
103.1

Flowchart:

Flowchart: Python - Find the largest number where commas or periods are decimal points.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the XOR of two given strings interpreted as binary numbers.
Next: Find x that minimizes mean squared deviation from a given list of numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.