+ 1
It's a sign - I passed but not sure why
Had a hard time breaking this up, could use some advice on why. I know there is some edge case problems here right away. The code works but there has to be a less shoddy way to lay this out https://code.sololearn.com/cO8SwNJbg2dw/?ref=app
11 Respostas
+ 2
Oh..
If you remember, that in Python, we can use negative values for indexing.
So that means when we want to reverse a text, we can just use text[::-1].
If we want to check whether the text is a palindrome, we can just do something like:
if text[::-1] == text:
Also, I think it'll be more readable and easier if you put the text into a list, or using list comprehension in Python, (using for loops)
+ 2
I passed that code coach using only one line tho, thanks to negative indexing.
+ 1
I've been away for quite a while, and yes that's way easier than iterating through letters at once.
+ 1
ren[paused]
You can see the following code, if you are confused:
+ 1
n = []
for x in range(0,4):
text = input()
if text[::-1] == text: n.append(text)
print("Open" if len (n)>=1 else "Trash")
+ 1
I didn't know you can resolve like that from the result of a list comprehension that's pretty neat
+ 1
If you want 1 liner:
print("Open" if len([x for x in [input() for x in range(0,4)] if x == x[::-1]])>=1 else "Trash")
Yeah..
+ 1
That last one is a little easier for me w the c style for loop ( I haven't don't python in a long time, so I have to revisit)
+ 1
Actually, "for-else" is useful for search for certain specific elements. So a compact solution could be:
for word in [input() for i in range(4)]:
if word == word[::-1]:
print('Open')
break
else:
print('Trash')
Link: https://www.sololearn.com/en/compiler-playground/cg8VaUhRH85w
0
I can see how that'd work, I'm really into list comprehensions- I also want to make things readable for others.
0
Same, I started to leave Python for a while after learning Web Development(HTML, CSS, JS)