"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Python Interview Question - Beginner to Advance

Python Interview Question - Beginner to Advance

Published on 2024-09-02
Browse:643

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

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)

2. Interviewer: TBD

Time: 15 Min

Level: Beginner

Example:

Input = "TBD"
Output= TBD
Release Statement This article is reproduced at: https://dev.to/jasper475/python-interview-question-beginner-to-advance-1omp?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3