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!
7 Respostas
+ 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!')
+ 2
Eren Ozer stop spamming ^^
+ 2
I'm still learning python. I'm noob yet haha. Btw, thank you very much for your explanation!
+ 1
visph sorry. I'm still learning about this app. I think now it's done.
+ 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 ;)
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!')
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 ^^