+ 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
22 Réponses
+ 10
Simple without loops
msg = input( 'enter: ' )
rev_msg = msg[ : : -1 ]
if msg == rev_msg:
print( 'NULL' )
else:
print( rev_msg[ 1 : ] )
+ 4
thanks for your code
+ 3
and the code was written by me
+ 3
Harsha S because in print you print null
+ 1
no, i understand what rev_msg[1:] is
it's list slicing
i meant why rev_msg[1:]?
+ 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"?
+ 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.
+ 1
thank you
+ 1
Yes, the code is right but your output is not dba, it's dcba
+ 1
Sabarinathan.M
i've corrected the error already 😁
+ 1
Calvin Thomas a for abcdcb
0
No problem, but do you understand how it works? copy/paste is easy, but to understand it is better.
0
yeah, why rev_msg[1:]?
0
not copied. yours was better so i took it
0
i'll Remove if you mind
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
0
it was supposed to be
if msg[i] == msg[len(msg)-msg.index(i)]:
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.
0
PARVIK PARASHAR
I need to print NULL if the input is already a palindrome
0
Harsha S What should be the output for 'abcdcb'?