E = [('c', 1), ('a', 1), ('t', 2), ('l', 1), ('e', 1)]
def check_freq(x):
decoded_string = ''
for element in x:
decoded_string = int(element[1]) * element[0]
print(decoded_string)
check_freq(E)
The function takes an encoded string and returns the decoded version in this case we get 'cattle', I just wonder if there is a more efficient way of doing this or some tips that can help me achieve better efficiency
CodePudding user response:
Do you just expect your code to be shorter like this:
E = [('c', 1), ('a', 1), ('t', 1), ('t', 1), ('l', 1), ('e', 1)]
def check_freq(lst):
return ''.join([tpl[1] * tpl[0] for tpl in lst])
print(check_freq(E))
CodePudding user response:
With some imports you could do this:
from operator import mul
from itertools import starmap
check_freq = lambda x: "".join(starmap(mul, x))
E = [('c', 1), ('a', 1), ('t', 1), ('t', 1), ('l', 1), ('e', 1)]
print(check_freq(E)) # cattle