Thursday, May 13, 2021

Tricky Python Interview Questions and Answers Code - (Level 1)




1. Compress the String as below using Python code  : 


Sample Input -

1222311

Sample Output -

(1, 1) (3, 2) (1, 3) (2, 1)

Explanation -

First, the character  occurs only once. It is replaced by . Then the character  occurs three times, and it is replaced by  and so on. Also, note the single space within each compression and between the compressions.

Answer 

Python Code -


from itertools import groupby
for i, n in groupby(input()):
    a = list(n) 
    print("(", len(a), ", ", i, ")", end = " ", sep = "")
    

Note : Refer the below link for groupby functionality if you are not familiar -

https://www.geeksforgeeks.org/python-pandas-dataframe-groupby/

2. Solve the below word order problem using Python code -

Sample Input

4
bcdef
abcdefg
bcde
bcdef

Sample Output

3
2 1 1

Explanation

There are  distinct words. Here, "bcdef" appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are "bcdef""abcdefg" and "bcde" which corresponds to the output.

Answer-

Python Code -


from collections import Counter
d = Counter(input() for _ in range(int(input())))
print(len(d))
print(*d.values())

Note : Refer the below link for counter functionality if you are not familiar -

https://www.guru99.com/python-counter-collections-example.html

3.  Solve the below Words Score problem using Python code -


Sample Input 0

2
hacker book

Sample Output 0

4

Explanation 0

There are two words in the input: hacker and book. The score of the word hacker is  because it contains an even number of vowels, i.e.  vowels, and the score of book is  for the same reason. Thus the total score is .

Sample Input 1

3
programming is awesome

Sample Output 1

4

Explanation 1

There are  words in the input: programmingis and awesome. The score of programming is  since it contains  vowels, an odd number of vowels. The score of is is also  because it has an odd number of vowels. The score of awesome is  since it contains  vowels, an even number of vowels. Thus, the total score is .

Answer -

Python Code 

def score_words(words):
    return sum(sum(char in 'aeiouy' for char in word) % 2 or 2 for word in words)

4.  Solve the below hacker rank problem using python -

You and Fredrick are good friends. Yesterday, Fredrick received  credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help!

A valid credit card from ABCD Bank has the following characteristics:

► It must start with a  or .
► It must contain exactly  digits.
► It must only consist of digits (-).
► It may have digits in groups of , separated by one hyphen "-".
► It must NOT use any other separator like ' ' , '_', etc.
► It must NOT have  or more consecutive repeated digits.


Sample Input

6
4123456789123456
5123-4567-8912-3456
61234-567-8912-3456
4123356789123456
5133-3367-8912-3456
5123 - 3567 - 8912 - 3456

Sample Output

Valid
Valid
Invalid
Valid
Invalid
Invalid

Explanation

4123456789123456 : Valid
5123-4567-8912-3456 : Valid
61234--8912-3456 : Invalid, because the card number is not divided into equal groups of .
4123356789123456 : Valid
51-67-8912-3456 : Invalid, consecutive digits  is repeating  times.
5123456789123456 : Invalid, because space '  ' and - are used as separators.

Answer - Python code :

import re
for _ in range(int(input())):
    s = input()
    if re.match(r"^[456]([\d]{15}|[\d]{3}(-[\d]{4}){3})$", s) and not re.search(r"([\d])\1\1\1", s.replace("-", "")):
        print("Valid")
    else:
        print("Invalid")
            


5.







Tricky Python Interview Questions and Answers Code - (Level 1)

1. Compress the String as below using Python code  :  Sample Input - 1222311 Sample Output - (1, 1) (3, 2) (1, 3) (2, 1) Explanation - First...