Why This doesn't work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why This doesn't work?

This code checks for palindrome if yes, NULL is printed if no, characters to be added to make it palindrome is printed if input = malay, output = alam but, if input = abcdc, output = dba why? https://code.sololearn.com/cc9nRCXm3yYC/?ref=app

29th Jul 2021, 3:39 PM
Harsha S
Harsha S - avatar
22 Answers
+ 10
Simple without loops msg = input( 'enter: ' ) rev_msg = msg[ : : -1 ] if msg == rev_msg: print( 'NULL' ) else: print( rev_msg[ 1 : ] )
29th Jul 2021, 4:02 PM
Ipang
+ 4
thanks for your code
29th Jul 2021, 4:04 PM
Harsha S
Harsha S - avatar
+ 3
and the code was written by me
29th Jul 2021, 4:09 PM
Harsha S
Harsha S - avatar
+ 3
Harsha S because in print you print null
30th Jul 2021, 1:04 PM
PARVIK PARASHAR
PARVIK PARASHAR - avatar
+ 1
no, i understand what rev_msg[1:] is it's list slicing i meant why rev_msg[1:]?
29th Jul 2021, 4:21 PM
Harsha S
Harsha S - avatar
+ 1
oh ok say, maam is palindrome but if i input "ma", i get "m" as output how do i make it to get output as "am"?
29th Jul 2021, 4:33 PM
Harsha S
Harsha S - avatar
+ 1
"But if I input 'ma', I get 'm' as output" Yes, because 'ma' + 'm' -> 'mam' is palindrome. To get 'am' simply don't slice, print( rev_msg ) rather than print( rev_msg[ 1 : ] ). But this way we have twin character at the center. (Edit) You can also take length of <msg> as consideration. If length of <msg> was odd, slice <rev_msg>, otherwise don't slice.
29th Jul 2021, 4:40 PM
Ipang
+ 1
thank you
30th Jul 2021, 9:52 AM
Harsha S
Harsha S - avatar
+ 1
Yes, the code is right but your output is not dba, it's dcba
30th Jul 2021, 2:00 PM
Sabarinathan.M
Sabarinathan.M - avatar
+ 1
Sabarinathan.M i've corrected the error already 😁
30th Jul 2021, 2:15 PM
Harsha S
Harsha S - avatar
+ 1
Calvin Thomas a for abcdcb
31st Jul 2021, 3:01 PM
Harsha S
Harsha S - avatar
0
No problem, but do you understand how it works? copy/paste is easy, but to understand it is better.
29th Jul 2021, 4:07 PM
Ipang
0
yeah, why rev_msg[1:]?
29th Jul 2021, 4:09 PM
Harsha S
Harsha S - avatar
0
not copied. yours was better so i took it
29th Jul 2021, 4:09 PM
Harsha S
Harsha S - avatar
0
i'll Remove if you mind
29th Jul 2021, 4:10 PM
Harsha S
Harsha S - avatar
0
No, I don't mind at all. I meant to say, you can ask, in case there's something you don't clearly understand from the code I posted. It's important to understand. rev_msg[ 1 : ] returns a slice of <rev_msg> from second character (index 1) up to the last. Example: msg = "abcdc" rev_msg = "cdcba" # reversed rev_msg[ 1 : ] = "dcba" # slice from second character
29th Jul 2021, 4:17 PM
Ipang
0
it was supposed to be if msg[i] == msg[len(msg)-msg.index(i)]:
29th Jul 2021, 4:19 PM
Harsha S
Harsha S - avatar
0
Because we want to append a reversed string <rev_msg> at the end of <msg> to make <msg> palindrome. But to achieve that, we skip the first character from <rev_msg> cause it will become the center character.
29th Jul 2021, 4:27 PM
Ipang
0
PARVIK PARASHAR I need to print NULL if the input is already a palindrome
31st Jul 2021, 3:33 AM
Harsha S
Harsha S - avatar
0
Harsha S What should be the output for 'abcdcb'?
31st Jul 2021, 2:43 PM
Calvin Thomas
Calvin Thomas - avatar