Code with Ramu

One destination for mastering interview coding

Learn, practice, and ace every interview coding question

Python Interview Coding Questions – Part1

For All Experiences

def is_palindrome(name)->bool:
#Remove spaces and convert to lower case
#isalnum() checks if the character is alphanumeric (letter or number).
 
 name = ''.join(c.lower() for c in name if c.isalnum())
 res = name[::-1]
 return name == res

if __name__ == "__main__":
    name = "ramr"
    print(is_palindrome(name))
def uniquevowels(name)->int:

 count = 0
 vowels = "aeiou"
 distinctalpha = ''.join(set(name))
 for c in distinctalpha:
     if c in vowels:
         count+=1
     else:
         continue
 return count

if name == "main":
    name = "ramarao"
    print(uniquevowels(name))

#Else

def countdistinctvowels(name) -> int:
    vowels = "aeiou"
    #{} - This is a set comprehension in Python (like list comprehension, but creates a set).
    found = {c for c in name.lower() if c in vowels}
    return len(found)

def removeduplicates(s: str) -> str:
    return ''.join(set(s))

print(removeduplicates("programming"))

Remove only consecutive duplicates

from itertools import groupby

def removeconsecutiveduplicates(s: str) -> str:
    return ''.join(key for key,  in groupby(s))

if name == "main":
    print(removeconsecutiveduplicates("aaabbcddddeeeffg"))

 Print only unique elements 

def getonlyunique(name) -> str:
    return ''.join([key for key in name if name.count(key)==1])

def Findmaxdigitinnumber(num) -> int:
    return max(int(d) for d in num)

if name == "main":
    print(Findmaxdigitinnumber("1254698"))
import math
return (math.factorial(5))

#Recursive
 if num==0 or num==1:
 return 1
 else:
 return result * factorialnum(n-1)
 
#Interative
res = 1
 for i in range(2,n+1):
    res*=i
 return res
def Findmaxdigitinnumber(num) -> int:
    return sum (int(d) for d in num)

if name == "main":
    print(Findmaxdigitinnumber("1254698"))

def productofdigits(num) -> int:
result = 1
for in num:
result*=int()
return result

if name == “main”:
print(practice(“56”))

def practice(num) -> int:
    a,b = 0,1
    series = []
    for i in range(int(num)):
        series.append(a)
        a,b=b,a+b
    return series

if name == "main":
    print(practice("10"))

def practice(num) -> bool:
    #Never use < =, correct way is <=
    if num <= 1:
        return false
    for i in range(2,num):
        if num % i == 0:
            return False
    return True
if name == "main":
    print(practice(11))
def practice(num) -> bool:
    #join() glues the list of characters back into a single string. Here '' means no delimiter
    return int(''.join(sorted(str(num))))

if name == "main":
    print(practice(641723))
def practice(num) -> bool:
    
    #Replaces All
    #return num.replace("cherry","Rowdy")
    #Replaces only first Occurance
    return num.replace("cherry", "Rowdy",1)

if name == "main":
    print(practice("I Love cherry, My daughter is cherry"))
def practice(name) -> str:
    return ' '.join(word.capitalize() for word in name.split(','))
if name == "main":
    print(practice("I,Love,cherry,My,daughter,is,cherry"))
from collections import Counter
def practice(name):

    #return  Counter(name)

# Return only unique characters
    for ch in set(name):
        print(f"{ch} -->  : {name.count(ch)}")

if name == "main":
    print(practice("rraammu"))
text = "Hello World from Python"
print(text.replace(" ", "")) 

#Remove leading & trailing spaces only
text = "   Hello World   "
print(text.strip()) 

#Remove all whitespace (tabs, newlines, etc.)
import re
text = "Hello   World\tPython\n"
print(re.sub(r"\s+", "", text))
def isanagram(s1, s2):
    return sorted(s1) == sorted(s2)

print(isanagram("listen", "silent"))  # True
print(isanagram("hello", "world"))

#Using collections.Counter (better for big strings)
from collections import Counter

def isanagram(s1, s2):
    return Counter(s1) == Counter(s2)

print(isanagram("listen", "silent"))  # True
print(isanagram("triangle", "integral"))  # True

sentence = “Hello world from Python”
print(len(sentence.split()))

import re
sentence = “Hello world\tfrom\nPython”
words = re.findall(r”\b\w+\b”, sentence)
print(len(words))

s = "12345"
num = int(s)
print(num, type(num))