Can you solve this Python interview challenge?

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

I wish the interview questions were that easy, it’s never that easy at least by my experience

RG-ikkw
Автор

def longest_even(text):
even_words = [x for x in text.split() if len(x)%2 == 0]
if not even_words: return "00"
return max(even_words, key=len)

trainjumper
Автор

Look man I was just looking up snake videos not a different language 😂

DamainHering
Автор

If i had the logics and skill to do that, it wouldn't be hard

kralekis
Автор

Pythonic and somewhat efficient:

return max([len(x) for x in text.split() if len(x) % 2 == 0, default="00")

Sam-qkgy
Автор

“A powerful python code student.” Tell him to apply with the avengers, they need help.

Sliced_Strife
Автор

That is just as difficult as the questions on the final exam in an intro to programming class I just finished.

I can't imagine job interview questions being so simple. Could it be true?

AudreyGraceB
Автор

Here is a short and easy implementation in python:

def longest_even_word1(string):
string +=" 00"
return sorted([x for x in string.strip().split(" ") if not len(x)%2], key=lambda x:len(x))[-1]

logic : we add " 00" to the given string in order to default the output when no even length word is found. The output array is sorted in ascending order of lengths...so we return only the last index!

dragonova
Автор

Add "00" to the string as a word.
Then you don't have to check for the no even's present. It will see 00 as an even-word and if it's the longest, then you've got it.

PhrontDoor
Автор

i think you should handle also the symbols "?, !."

improveyourself
Автор

Python programmers really be thinking they're pros after solving shit like that

guilhermewxs
Автор

Should punctuation be considered? The "." is included as part of the word in strings[2]. There seems to be potential for a bug.

aju-dtn
Автор

That's very easy. Every developer can solve that without thinking.

orrinjonesjr
Автор

I feel like it's more pythonic and also more efficient to use builtins. Here's an example of taking advantage of list comprehension to streamline this code:

def longest_even(text):
evens = sorted([(len(word), word) for word in text.split() if len(word) % 2 == 0])
return evens[-1][1] if len(evens) else "00"

The first line keeps only the even words, and forms a list of the form (word length, word). Then we can sort this list, which will default to sorting by word length first and then word after. Finally we can simply take the last element if the list has any elements at all, and otherwise we can return 00

ishaanivaturi
Автор

Practice in codewards. You can answer these easily once you have done a few hundred in code wars. Questions like these start coming after the first (initial beginner) rank.

prome
Автор

😂 i m very naive in coding still i don't find it difficult

ug
Автор

def longest_even(sentence):
return max([x if len(x)%2 == 0 else "00" for x in sentence.split(" ") ], key=len)

ShivKumar-fpvk
Автор

was he powerful as a student, like a good student, or was he powerful like arnold, or was he powerful because he could do magic - what does a powerful python student even mean? wth!!!?

wizardmaster
Автор

Well, if it's a sentence, how about to clean it from the punctuation symbols first?

alistair.crompton
Автор

return max(text.split(), key=lambda x: (len(x) & 1 == 0, -len(x)), default="00")

hallooww