-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3408 from marchdz/soluciones
Reto #19 - Kotlin
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/kotlin/marchdz.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
fun analyzeText(text: String) { | ||
val sentenceCount = text.split(".", ":", ";", "!", "?") | ||
.filter { it.isNotBlank() }.size | ||
val words = text.split(" ").filter { it.isNotBlank() }.toMutableList() | ||
val wordsCount = words.size | ||
|
||
for ((index, word) in words.withIndex()) { | ||
words[index] = word.replace(Regex("[,.:;!¡¿?]"), "") | ||
} | ||
|
||
val wordsAverageLength = if (wordsCount > 0) words.sumOf(String::length) / wordsCount else 0 | ||
val longestWord = if (words.isNotEmpty()) words.maxBy(String::length) else "" | ||
|
||
println(""" | ||
======================================= | ||
Número total de palabras: $wordsCount | ||
Longitud media de las palabras: $wordsAverageLength | ||
Número de oraciones del texto: $sentenceCount | ||
Palabra más larga: $longestWord | ||
======================================= | ||
""".trimIndent()) | ||
} | ||
|
||
fun main() { | ||
analyzeText("Esto es una prueba; siempre hay que probar. ¿Ha funcionado? ¡Seguro!") | ||
analyzeText("t") | ||
analyzeText("") | ||
} |