Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #19 - Python #6414

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '))