1. Interviewer: Write a Python code to get output as mentioned below.
Time: 15 Min
Level: Beginner
Example:
Input = "AAAABBBCCDAABBB"
Output= A4B3C2D1A2B3
Possible Answer
# 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)
Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.
Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.
Top comments (0)