Answer to Question #281826 in Python for arif

Question #281826

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”,


1
Expert's answer
2021-12-21T17:20:43-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS