Input = "AAAABBBCCDAABBB" Output= A4B3C2D1A2B3
# define a function() that takes string input and return null if string is null and returns 1st occurrences of character followed by number of occurrences and so on. # This function takes a string and returns processed string output. def str_skimmer(input_string): # Return null string if input is null. if not input_string: return "" # Store values for 2 parameters - "Reference Character and "Count" prev_char=input_string[0] op=[] count=1 for i in input_string[1:]: if (i==prev_char): count = 1 print(i, count, op) else: op.append(prev_char str(count)) prev_char=i count=1 op.append(prev_char str(count)) print(op) return ''.join(op) #O: A4B3C2D1A2B3 ip="AAAABBBCCDAABBB" op= str_skimmer(ip) print(op)
Input = "TBD" Output= TBD
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3