Facile 0 résolus 0 % de réussite

Fizz Buzz

Build a sequence using divisibility by 3 and 5.

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

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

Exemples

Exemple 1
Entrée :
limit = 5

Sortie attendue :
expected = ["1", "2", "Fizz", "4", "Buzz"]
Entrée :
long long limit = 5;

Sortie attendue :
std::vector<std::string> expected = {"1", "2", "Fizz", "4", "Buzz"};
Entrée :
long limit = 5;

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

Explication

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

Exemple 2
Entrée :
limit = 0

Sortie attendue :
expected = []
Entrée :
long long limit = 0;

Sortie attendue :
std::vector<std::string> expected = {};
Entrée :
long limit = 0;

Sortie attendue :
string[] expected = new string[] {};

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.