+2

Help me write this code in one line

https://code.sololearn.com/cDd6mYN47tsj/?ref=app so far I have tried : https://code.sololearn.com/cqXhf7nxMVbo/?ref=app

py3

2/25/2020 9:05:26 AM

pyro

16 Answers

New Answer

+8

For loops with break conditions, itertools.takewhile are great help. Try this: from itertools import takewhile print(''.join(i[0] for i in takewhile(lambda c: all(ele == c[0] for ele in c), y))) But like HonFu said, long one-line codes like this one are not advisable.

+3

Without import : lcp = lambda ws: [ws[0][:s] for s in range(min(len(w) for w in ws),-1,-1) if all(ws[0][:s] == w[:s] for w in ws[1:])][0] https://code.sololearn.com/cZUg9TTdwLjp/?ref=app I don't like oneliners, prefer to write code that is readable...

+2

print(‘b’) :))

+1

I am not sure how we are supposed to help you with that - it's task-specific, and you probably just need to accumulate some experience writing these. What kind of help did you have in mind?

+1

HonFu for i in y : if not all(ele == i[0] for ele in i) : x.append("") break else: x.append(i[0]) I want to make it more pythonic. In case of if statement : if not all(ele == i[0] for ele in i) : x.append("") break where to put "break" in the code (below) : print(' '.join(["" if not all(ele == i[0] for ele in i) else i[0] for i in y ]))

+1

Hm, I'm not sure if this is best suited for a oneliner... This would be a non-oneliner variation using break. strings = ['flyweight', 'flower', 'flamboyant'] start = '' for i in range(len(min(strings, key=len))): if len({s[i] for s in strings})>1: break start += strings[0][i] print(start)

+1

Kishalaya Saha Thanks a ton.

+1

https://code.sololearn.com/cgCd5WmYfVv2/?ref=app easy one

+1

Best practices in programming. first read and keep your mind what are the best triks and tips. Good Luck👍👍 https://www.topcoder.com/coding-best-practices/ https://code.tutsplus.com/tutorials/top-15-best-practices-for-writing-super-readable-code--net-8118 https://hackernoon.com/few-simple-rules-for-good-coding-my-15-years-experience-96cb29d4acd9 https://www.google.com/amp/s/x-team.com/blog/good-programming-practices-blog-post-wip/amp/

+1

Here is another oneliner with only 2 loops. lcp2 = lambda ws: ''.join(c for i, c in enumerate(ws[0], 1) if all(ws[0][:i] == w[:i] for w in ws)) Check my code again, I added detailed step by step interpretation of the logic how it a works. To be fair I would much prefer to write out each logical step in separate lines. That increases code maintainability so it is much more pythonic. https://code.sololearn.com/cZUg9TTdwLjp/?ref=app

0

I'm still not sure what exactly you want to do. Do you want to figure out if all words in a list start with the same letter? Can you describe your aim in words?

0

HonFu https://leetcode.com/problems/longest-common-prefix/

0

This didnt quite do it but it uses a concept you're not that might help you. Namely you can out put parts of specific strings in arrays without making all that append stuff happen strs = ['bof','bof','big'] x= len(strs)-1 i= len(strs[0]) while x>=0: if (strs[x][:i])==(strs[x-1][:i]): print (strs[x][:i]) break x-=1 i=i-1

0

I m beginner in python can anyone guide me from where to start

0

pyro the difference in my version is that my range() goes from the length of the shortest words down to zero, while yours go upwards. Then I take the first element of this list comprehension at the end.

-1

Tibor Santa I thought : lambda ws: [ws[0][:s] for s in range(min(len(w) for w in ws)) if all(ws[0][:s] == w[:s] for w in ws[1:])] would result : ['fl'] for ["flower", "flow", "flight"]. But it results : ['', 'f', 'fl'] Could you pls explain how it works? http://pythontutor.com/visualize.html#code=lcp%20%3D%20%5B%5D%0Aws%20%3D%20%5B%22flower%22,%22flow%22,%22flight%22%5D%0Afor%20s%20in%20range%28min%28len%28w%29%20for%20w%20in%20ws%29%29%3A%0A%20%20if%20all%28ws%5B0%5D%5B%3As%5D%20%3D%3D%20w%5B%3As%5D%20for%20w%20in%20ws%5B1%3A%5D%29%3A%0A%20%20%20%20lcp.append%28ws%5B0%5D%5B%3As%5D%29%0Aprint%28lcp%29%0A&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false