+ 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

6th Jul 2023, 3:53 AM
ren[paused]
ren[paused] - avatar
11 Réponses
+ 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)
6th Jul 2023, 4:00 AM
Dragon RB
Dragon RB - avatar
+ 2
I passed that code coach using only one line tho, thanks to negative indexing.
6th Jul 2023, 4:35 AM
Dragon RB
Dragon RB - avatar
+ 1
I've been away for quite a while, and yes that's way easier than iterating through letters at once.
6th Jul 2023, 4:30 AM
ren[paused]
ren[paused] - avatar
+ 1
ren[paused] You can see the following code, if you are confused:
6th Jul 2023, 2:07 PM
Dragon RB
Dragon RB - avatar
+ 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")
6th Jul 2023, 2:09 PM
Dragon RB
Dragon RB - avatar
+ 1
I didn't know you can resolve like that from the result of a list comprehension that's pretty neat
6th Jul 2023, 2:10 PM
ren[paused]
ren[paused] - avatar
+ 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..
6th Jul 2023, 2:12 PM
Dragon RB
Dragon RB - avatar
+ 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)
6th Jul 2023, 2:12 PM
ren[paused]
ren[paused] - avatar
+ 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
15th Apr 2024, 11:37 AM
Monachopsis
Monachopsis - avatar
0
I can see how that'd work, I'm really into list comprehensions- I also want to make things readable for others.
6th Jul 2023, 2:00 PM
ren[paused]
ren[paused] - avatar
0
Same, I started to leave Python for a while after learning Web Development(HTML, CSS, JS)
6th Jul 2023, 2:15 PM
Dragon RB
Dragon RB - avatar