[Challenge 01] - Level: Begginer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Challenge 01] - Level: Begginer

Make a program that identify if a phrase typed by the user is a palindrome or not. Ex of a palindrome: O LOBO AMA O BOLO (It's in Portuguese. In English it means: the wolf loves the cake) Source code (One of the possible solutions made in Python): https://code.sololearn.com/c1NLCD1RYnNl/?ref=app I hope you enjoy!

4th Jul 2021, 2:10 AM
Mateus Ferraz
Mateus Ferraz - avatar
7 Answers
+ 2
# shorter: pal = True inp = ''.join(input('Type a phrase: ').upper().split()) for i in range(len(inp)//2): if inp[i] != inp[-i-1]: pal = False break print(f'The phrase is {"" if pal else "not "}palindrome!')
4th Jul 2021, 2:29 AM
visph
visph - avatar
+ 2
Eren Ozer stop spamming ^^
4th Jul 2021, 3:00 AM
visph
visph - avatar
+ 2
I'm still learning python. I'm noob yet haha. Btw, thank you very much for your explanation!
4th Jul 2021, 3:54 AM
Mateus Ferraz
Mateus Ferraz - avatar
+ 1
visph sorry. I'm still learning about this app. I think now it's done.
4th Jul 2021, 3:44 AM
Mateus Ferraz
Mateus Ferraz - avatar
+ 1
str(input()) is pointless, as input still return a string .strip() is pointless, as you join each .split() part with empty string my solution also stop iteration as soon as a char is different ;)
4th Jul 2021, 3:44 AM
visph
visph - avatar
0
# Another Solution phrase = ''.join(str(input('Type a phrase: ')).strip().upper().split()) inverted = phrase[::-1] print(f'The inverse of {phrase} is {inverted}.') if phrase == inverted: print('This phrase is palindrome!') else: print('This phrase is not palindrome!')
4th Jul 2021, 3:34 AM
Mateus Ferraz
Mateus Ferraz - avatar
0
Mateus Ferraz less efficient: you iterate once to revert the string, and once more to compare both string ;P also, that's the second time you set and immediatly remove the best answer mark from my answer ^^
4th Jul 2021, 3:42 AM
visph
visph - avatar