Fácil 0 resueltos 0% de aceptación

Fizz Buzz

Build a sequence using divisibility by 3 and 5.

Lo que aprenderás

  • Test divisibility by 3 and 5
  • Order if/else branches correctly
  • Build a list of results in a loop

Problema

Ver traducción

For each integer from 1 through limit, use Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both. Otherwise use the number written as text. Return the entries in order instead of printing them.

Implement fizz_buzz(limit).

Input

limit: The last integer to include.

Output

Return a list of strings, one for each integer from 1 through limit.

Restricciones

0 ≤ limit ≤ 10,000. A limit of 0 produces an empty list.

Ejemplos

Ejemplo 1
Entrada:
limit = 5

Salida esperada:
expected = ["1", "2", "Fizz", "4", "Buzz"]
Entrada:
long long limit = 5;

Salida esperada:
std::vector<std::string> expected = {"1", "2", "Fizz", "4", "Buzz"};
Entrada:
long limit = 5;

Salida esperada:
string[] expected = new string[] {"1", "2", "Fizz", "4", "Buzz"};

Explicación

3 is divisible by 3 and 5 is divisible by 5. Other entries keep their number.

Ejemplo 2
Entrada:
limit = 0

Salida esperada:
expected = []
Entrada:
long long limit = 0;

Salida esperada:
std::vector<std::string> expected = {};
Entrada:
long limit = 0;

Salida esperada:
string[] expected = new string[] {};

Explicación

There are no integers from 1 through 0, so the list is empty.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Each position depends only on the current integer’s divisibility by 3 and 5.

Pista 2

Build the result list in order by checking both divisibility conditions for every number.

Pista 3

Handle the lower bound carefully: if the limit is 0, the answer is an empty list.

Complejidad esperada

O(n) time, O(n) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.