Write a python function that takes a string as an argument. This string will be a simple valid expression containing only characters “a”, “b”, “c” and “+”. Some examples of the input string: “a+b+c”, “a”, “a+a+b”.
Your function will have to return a string which will be a simple version of the input string. For example, if the input is “a+a+b”, output will be “2*a+b”,
s = input().split('+')
d = {"a":0,"b":0,"c":0}
for i in s:
d[i] += 1
a = ""
for key,val in d.items():
if val > 1:
a = a+ str(val) +'*' +key + '+'
else:
a = a + key + "+"
a = a[0:len(a)-1]
print(a)
Comments
Leave a comment