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 fun…


This content originally appeared on DEV Community and was authored by Jasper Rodda

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


This content originally appeared on DEV Community and was authored by Jasper Rodda


Print Share Comment Cite Upload Translate Updates
APA

Jasper Rodda | Sciencx (2024-08-28T21:19:58+00:00) Python Interview Question – Beginner to Advance. Retrieved from https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/

MLA
" » Python Interview Question – Beginner to Advance." Jasper Rodda | Sciencx - Wednesday August 28, 2024, https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/
HARVARD
Jasper Rodda | Sciencx Wednesday August 28, 2024 » Python Interview Question – Beginner to Advance., viewed ,<https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/>
VANCOUVER
Jasper Rodda | Sciencx - » Python Interview Question – Beginner to Advance. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/
CHICAGO
" » Python Interview Question – Beginner to Advance." Jasper Rodda | Sciencx - Accessed . https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/
IEEE
" » Python Interview Question – Beginner to Advance." Jasper Rodda | Sciencx [Online]. Available: https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/. [Accessed: ]
rf:citation
» Python Interview Question – Beginner to Advance | Jasper Rodda | Sciencx | https://www.scien.cx/2024/08/28/python-interview-question-beginner-to-advance/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.