From 6bc2562045b45aea4637f7e465d08f8ab5ac785a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vi=CC=81ctor=20Andre=CC=81s?= <102297884+vandresca@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:03:19 +0200 Subject: [PATCH 1/2] Reto #0 - Fizzbuzz --- .../php/vandresca.php" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" diff --git "a/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" "b/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" new file mode 100644 index 0000000000..5403ea03c6 --- /dev/null +++ "b/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" @@ -0,0 +1,26 @@ + Date: Fri, 11 Aug 2023 11:24:28 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Mejoras=20Reto=C2=A0#0=20-=20Fizzbuzz?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../php/vandresca.php" | 69 ++++++++++++++++--- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git "a/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" "b/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" index 5403ea03c6..54cc51cc25 100644 --- "a/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" +++ "b/Retos/Reto #0 - EL FAMOSO FIZZ BUZZ [F\303\241cil]/php/vandresca.php" @@ -9,18 +9,67 @@ * - Múltiplos de 3 y de 5 a la vez por la palabra "fizzbuzz". */ + interface IRule { + public function check(int $number): bool; + public function getValue(): string; +} -for($i=1; $i<=100; $i++){ - println(fizz_buzz($i)); +class FizzRule implements IRule{ + public function check(int $number):bool{ + return $number%3 == 0; + } + + public function getValue():string{ + return "fizz"; + } } -function fizz_buzz($number){ - if($number%15==0) return "fizzbuzz"; - if($number%5==0) return "buzz"; - if($number%3==0) return "fizz"; - return $number; +class BuzzRule implements IRule{ + public function check(int $number):bool{ + return $number%5 == 0; + } + + public function getValue():string{ + return "buzz"; + } +} + +// Clase principal que genera y muestra los resultados de FizzBuzz +class FizzBuzz { + private $rules = []; + + + public function __construct(array $rules) { + $this->rules = $rules; + } + + public function generate(int $n): array { + $result = []; + for ($i = 1; $i <= $n; $i++) { + $value = ''; + foreach ($this->rules as $rule) { + if ($rule->check($i)) { + $value .= $rule->getValue(); + } + } + + $result[] = ($value !== '') ? $value : (string)$i; + } + return $result; + } + + // Muestra los resultados en la consola + public function printResult(int $n): void { + $result = $this->generate($n); + foreach ($result as $item) { + echo $item . "\n"; + } + } } -function println($text){ - echo $text."\n"; -} \ No newline at end of file + +echo "Reto #0: EL FAMOSO \"FIZZ BUZZ\"\n"; +$fizzBuzz = new FizzBuzz([new FizzRule(), new BuzzRule()]); +$fizzBuzz->printResult(100); + +?> \ No newline at end of file