11/21/2020

Python Explnation (Edabit // Longest word)

 Hello!

Today's question is this!

Create a function that takes a word and extends all vowels by a number num.

Write a function that finds the longest word in a sentence. If two or more words are found, return the first longest word. Characters such as apostophe, comma, period (and the like) count as part of the word (e.g. O'Connor is 8 characters long).

Examples

longest_word("Margaret's toy is a pretty doll.") ➞ "Margaret's"

longest_word("A thing of beauty is a joy forever.") ➞ "forever."
  
longest_word("Forgetfulness is by all means powerless!") ➞ "Forgetfulness"

 

OK!

This is my solved code!

def longest_word(s):
words = s.split(' ')
string = ""
for word in words:
if len(string) < len(word):
string = word
return string

Let's split the word!

Then, let's make a new string and loop.

We will check if the length of the string is smaller than the word's length.

Then, we will change the string to the word.

Lastly, we will return the string.

 

Easy!

Bye!!!

 

0 件のコメント:

Super Leo 4!

  Leo : Let's do this! Reimu : I'll kill those monsters, so you go! Leo : Yeah! I'll go to the goal!  Leo : Let's go〜! ? Wai...