Skip to content

Commit

Permalink
Merge pull request #6414 from chartypes/main
Browse files Browse the repository at this point in the history
Reto #19 - Python
  • Loading branch information
Roswell468 authored Mar 15, 2024
2 parents b24c462 + 12ec15e commit d9820d1
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/python/chartypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''/*
* Crea un programa que analice texto y obtenga:
* - Número total de palabras.
* - Longitud media de las palabras.
* - Número de oraciones del texto (cada vez que aparecen un punto).
* - Encuentre la palabra más larga.
*
* Todo esto utilizando un único bucle.
*/'''

from functools import reduce

def text_analyzer(text:str) -> dict:
data:dict = {}
words:int = 0
largest_word:str = ''

data['total_senteces'] = 0

# How many senteces are
if '.' in text:
sentences:list[str] = text.split('.')
data['total_senteces'] = len(sentences)

# How many words are
for sentence in sentences:
correct_sentence:str = sentence

sentence = sentence.strip()
if not ' ' in sentence[0]:
correct_sentence = ' '+ sentence

words +=correct_sentence.count(' ')

# The largest word
cheking_word = reduce(lambda x,y: x if len(x)>len(y) else y ,correct_sentence.split(' '))
if len(cheking_word) >= len(largest_word):
largest_word = cheking_word

data['largest_word'] = largest_word
data['total_words'] = words

return data

# Tests
print(text_analyzer('hola como estas.Esto es una oracion'))
print(text_analyzer('hola como estas. Esto es una oracion'))
print(text_analyzer(' hola jejejejeje como estas. Esto es una oracion'))
print(text_analyzer(' hola csadfsdfsoomooooolalaallla estas. Esto es una oracion '))

0 comments on commit d9820d1

Please sign in to comment.