Given a strings list with denomination suffixes, the task is to write a Python program to convert the string to its actual values, substituting for denomination actual values.
Input : test_list = ["25Cr", "7M", "24B", "9L", "2Tr", "17K"] Output : [250000000.0, 7000000.0, 24000000000.0, 900000.0, 2000000000000.0, 17000.0] Explanation : Suffix replaced as per Symbol notations with numerical figure.
Input : test_list = ["25Cr", "7M", "24B"] Output : [250000000.0, 7000000.0, 24000000000.0] Explanation : Suffix replaced as per Symbol notations with numerical figure.
Method 1: Using float() + dictionary + loop
Approach:
In this, we construct a dictionary of all denominations with its original values and then convert the value to float and perform multiplication with the denomination’s actual value.
Python3
# Python3 code to demonstrate working of# Convert Suffix denomination to Values# using float() + dictionary + loop# Initializing listtest_list = ["25Cr", "7M", "24B", "9L", "2Tr", "17K"]# Printing original listprint("The original list is : " + str(test_list))# Initializing values dictionaryval_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000}# Empty listres = []for ele in test_list: for key in val_dict: if key in ele: # Conversion of dictionary keys to values val = float(ele.replace(key, "")) * val_dict[key] res.append(val)# Printing resultprint("The resolved dictionary values : " + str(res)) |
Output:
The original list is : [’25Cr’, ‘7M’, ’24B’, ‘9L’, ‘2Tr’, ’17K’] The resolved dictionary values : [250000000.0, 7000000.0, 24000000000.0, 900000.0, 2000000000000.0, 17000.0]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 2: Using re.findall() + dictionary comprehension
Approach:
- Import the required modules.
- Define the input list and the dictionary with denomination values.
- Use regular expressions to extract the numbers and the suffix denominations from each element of the input list.
- Use a dictionary comprehension to convert the suffix denominations to their respective values and multiply them with the corresponding numbers.
- Print the final result.
Python3
import re# Initializing listtest_list = ["25Cr", "7M", "24B", "9L", "2Tr", "17K"]print("The original list is : " + str(test_list))# Initializing values dictionaryval_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000}# Extracting numbers and suffix denominations using regular expressionspattern = r'(\d+)(\w+)'result = [re.findall(pattern, s)[0] for s in test_list]# Converting suffix denominations to their respective values # and multiply with numbersresult = [float(num) * val_dict[suffix] for num, suffix in result]# Printing resultprint("The resolved dictionary values : " + str(result)) |
The original list is : ['25Cr', '7M', '24B', '9L', '2Tr', '17K'] The resolved dictionary values : [250000000.0, 7000000.0, 24000000000.0, 900000.0, 2000000000000.0, 17000.0]
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list, for storing the intermediate result list.
Method 3: using regular expressions + loop
Python3
import re# Initializing listtest_list = ["25Cr", "7M", "24B", "9L", "2Tr", "17K"]# Printing original listprint("The original list is: " + str(test_list))# Initializing values dictionaryval_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000}# Empty list res = []for ele in test_list: match = re.findall(r'(\d+)([A-Za-z]+)', ele) if match: val, suffix = match[0] if suffix in val_dict: res.append(float(val) * val_dict[suffix])# Printing resultprint("The resolved dictionary values: " + str(res)) |
The original list is: ['25Cr', '7M', '24B', '9L', '2Tr', '17K'] The resolved dictionary values: [250000000.0, 7000000.0, 24000000000.0, 900000.0, 2000000000000.0, 17000.0]
Time complexity: O(n), where n is the number of elements in the test_list.
Auxiliary space: O(1), as it does not depend on the input size but on the fixed number of elements in the value dictionary.
