Interview question and Palindrome in python

Interview question and Palindrome in python

Interview question and Palindrome in python

dictionary1 = { "a":1,"b": 2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,
                "p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26}

inp = input("enter your name:").lower()
print(inp)
flag = 0
for i in inp:
    print(dictionary1[i])
    flag = flag+dictionary1[i]  # collect the value and store it in  flag variable
print(flag)

str=str(flag)                    # Convert the int into str because I need to  reverse it.
str_collect = ""                 # store it in str_collect
print("len",len(str))
for i in range(len(str)-1,-1,-1):
    print("ss",str[i])
    str_collect+=str[i]

print("str_collect",str_collect)

if str==str_collect:
    print("its palindrome")
else:
    print("not palindrome")

#======================================================================================================================#
# Step2 method we can use this method also.
dictionary2 = { "a":1,"b": 2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,
                "p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26}
inp = input("enter your name:").lower()

flag_2=0
for i in inp:
    print(dictionary2[i])
    flag_2 = flag_2+dictionary2[i]  # collect the value and store it in  flag variable
print(flag_2)

flag_3=flag_2

reminder = 0
while flag_3>0:

    store = flag_3%10
    reminder = reminder*10+store  #this code similar as a C program
    print(reminder)
    flag_3=flag_3//10
    print(flag_3)
print(reminder)

if reminder==flag_2:
    print("palindrome")
else:
    print("not palindrome")
   

0 Response to "Interview question and Palindrome in python "

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel