+ 4

Oneliner question (Recurring Character)

I have recently made a code that gets the first recurring character in a string: https://code.sololearn.com/c65Q3VDt07Hl/?ref=app It raises an exception due to the method I used (using list indices), though I want it to display "None". While I can easy fix this using a try-except block, I want to keep it a oneliner. Any suggestions?

2nd Sep 2019, 10:06 AM
Trigger
Trigger - avatar
18 Antworten
+ 2
HonFu How about this to reduce character count? print((lambda x=input(): ([i for i in x if x.count(i)>1] or [None])[0])())
3rd Sep 2019, 7:41 AM
Roneel
Roneel - avatar
+ 4
Short and simple: x=input();print(next((i for i in x if x.count(i)>1),None))
2nd Sep 2019, 12:58 PM
Diego
Diego - avatar
+ 3
Sorry, my version didn't work exactly as you wanted. This one works: print((lambda x=input(): ([i for i in x if x.count(i)>1] or [None, None])[0])())
2nd Sep 2019, 10:33 AM
HonFu
HonFu - avatar
+ 3
Cbr✔[ Exams ] Thomas Williams It's a oneliner to me. Most of the times anything that isn't a function is not added to the lines/characters count. At least that's the way golf challenges work on SL.
2nd Sep 2019, 1:20 PM
Diego
Diego - avatar
+ 3
Roneel thats actually a very good solution👍🏼 Thanks😁
3rd Sep 2019, 7:49 AM
Trigger
Trigger - avatar
+ 2
HonFu I get the 'or [None, None]' now! If list is empty, then it is replaced by [None]... Smart.
2nd Sep 2019, 10:44 AM
Théophile
Théophile - avatar
+ 2
HonFu Nice👍🏼 Thanks man, really appreciate it! Cbr✔[ Exams ] Nice code, but just checked it out and it didnt do the "None" thing. Thanks for the reply, though Thank you, guys👍🏼 Ill keep this thread for a day and then delete it
2nd Sep 2019, 10:52 AM
Trigger
Trigger - avatar
+ 2
Why delete? It's a programming-related question!
2nd Sep 2019, 11:58 AM
HonFu
HonFu - avatar
+ 2
Yeah, absolutely! Other people may want to learn from it as well. And it would be a pity if the very people who actually ask something programming-related, edit themselves out of here. 😂
2nd Sep 2019, 12:13 PM
HonFu
HonFu - avatar
+ 2
Lol that's True HonFu
2nd Sep 2019, 12:25 PM
Trigger
Trigger - avatar
+ 2
Ha - obviously! 🤦‍♂️ Thanks for pointing that out, Roneel !
3rd Sep 2019, 7:46 AM
HonFu
HonFu - avatar
+ 1
print((lambda x=input(): [i for i in x if x.count(i)>1][0] if len([i for i in x if x.count(i)>1]) > 0 else None)()) Here is my answer, with ternary operator!
2nd Sep 2019, 10:14 AM
Théophile
Théophile - avatar
+ 1
Théophile oh. I was hoping there would be a simpler way to do it. Yeah, I used that one in a beta-version. Was hoping someone would have a better solution Thanks man👍🏼
2nd Sep 2019, 10:17 AM
Trigger
Trigger - avatar
+ 1
I don't know if a simpler way exists too. I'll see!
2nd Sep 2019, 10:19 AM
Théophile
Théophile - avatar
+ 1
print((lambda x: x[0] if len(x) > 0 else None)((lambda x=input(): [i for i in x if x.count(i)>1])()))
2nd Sep 2019, 10:24 AM
Théophile
Théophile - avatar
+ 1
Cbr✔[ Exams ] he wants a one liner code. In yours, you can't replace string by input. But nice code, btw!
2nd Sep 2019, 10:29 AM
Théophile
Théophile - avatar
+ 1
HonFu I was going to delete it since its been answered. Jn retrospect, I should probably keep it around
2nd Sep 2019, 12:11 PM
Trigger
Trigger - avatar
+ 1
Oh. Cool👍🏼 Diego. Not oneliner, but good code anyway
2nd Sep 2019, 1:13 PM
Trigger
Trigger - avatar