forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/php/kodenook.php
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,41 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
/** | ||
* The `stats` function takes a string as input and returns a string containing various statistics | ||
* about the input text, including the word count, the largest word, the sentence count, and the | ||
* average number of letters per word. | ||
* | ||
* @param string txt The parameter `txt` is a string that represents a text. | ||
* | ||
* @return string a string that contains the following information: | ||
*/ | ||
function stats(string $txt): string | ||
{ | ||
$txt = explode(' ', $txt); | ||
$largest = ''; | ||
$sentences = 0; | ||
$letter_counts = []; | ||
|
||
foreach ($txt as $value) { | ||
if (count(str_split($value)) > count(str_split($largest))) { | ||
$largest = $value; | ||
} | ||
if (str_ends_with($value, '.')) { | ||
$sentences++; | ||
} | ||
$value = str_replace('.', '', $value); // Corregir aquí | ||
array_push($letter_counts, count(str_split($value))); | ||
} | ||
|
||
$response = 'Words count: ' . count($txt) . PHP_EOL; | ||
$response .= 'Largest word: ' . $largest . PHP_EOL; | ||
$response .= 'Sentences count: ' . $sentences . PHP_EOL; | ||
$response .= 'Average letter: ' . array_sum($letter_counts) / count($letter_counts) . PHP_EOL; | ||
|
||
return $response; | ||
} | ||
|
||
$sentence = 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Minus laudantium numquam quam ex velit consequatur in repellendus praesentium ab? Esse dolor voluptates recusandae at, odit necessitatibus provident sint placeat. Recusandae.'; | ||
echo stats($sentence); |