Python Interview Question - Beginner to Advance

王林
Release: 2024-08-29 06:31:35
Original
861 people have browsed it

Python Interview Question - Beginner to Advance

1. Interviewer: Write a Python code to get output as mentioned below.

Time: 15 Min

Level: Beginner

Example:

Input = "AAAABBBCCDAABBB"
Output= A4B3C2D1A2B3
Copy after login

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)
Copy after login

2. Interviewer: TBD

Time: 15 Min

Level: Beginner

Example:

Input = "TBD"
Output= TBD
Copy after login

The above is the detailed content of Python Interview Question - Beginner to Advance. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!