Fácil 0 resolvidos 0% de aceitação

Mad Lib Builder

Fill the bracketed blanks of a story template with words, in order.

O que você vai aprender

  • Find bracketed parts inside a string
  • Replace them in order with list items
  • Handle running out of replacement words

Problema

Ver tradução

In a word game, one player writes a story with blanks and the others call out words without seeing the story. A blank is written as a hint in square brackets, such as [noun] or [number]. Replace the first blank with the first word, the second blank with the second word, and so on, brackets and hint included. If there are fewer words than blanks, the blanks left over stay exactly as they are; extra words are not used.

Implement mad_lib_builder(template, words).

Input

template: The story. Every [ is closed by a later ], with no brackets nested inside a blank.

words: The words to put in the blanks, in order.

Output

Return the finished story.

Restrições

0 ≤ length of template ≤ 1000

0 ≤ length of words ≤ 100, each word has at most 30 characters and no square brackets

Exemplos

Exemplo 1
Entrada:
template = "My [animal] ate [number] [food] for breakfast."
words = ["hamster", "12", "pancakes"]

Saída esperada:
expected = "My hamster ate 12 pancakes for breakfast."
Entrada:
std::string template = "My [animal] ate [number] [food] for breakfast.";
std::vector<std::string> words = {"hamster", "12", "pancakes"};

Saída esperada:
std::string expected = "My hamster ate 12 pancakes for breakfast.";
Entrada:
string template = "My [animal] ate [number] [food] for breakfast.";
string[] words = new string[] {"hamster", "12", "pancakes"};

Saída esperada:
string expected = "My hamster ate 12 pancakes for breakfast.";

Explicação

Each blank takes the next word: "My hamster ate 12 pancakes for breakfast.".

Exemplo 2
Entrada:
template = "The [adjective] [noun] sang."
words = ["sleepy"]

Saída esperada:
expected = "The sleepy [noun] sang."
Entrada:
std::string template = "The [adjective] [noun] sang.";
std::vector<std::string> words = {"sleepy"};

Saída esperada:
std::string expected = "The sleepy [noun] sang.";
Entrada:
string template = "The [adjective] [noun] sang.";
string[] words = new string[] {"sleepy"};

Saída esperada:
string expected = "The sleepy [noun] sang.";

Explicação

Only one word was given, so the second blank stays: "The sleepy [noun] sang.".

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Scan the template character by character and copy normal characters straight to the result.

Dica 2

When you see `[`, find the matching `]` and treat everything in between as one blank.

Dica 3

Keep a counter of blanks seen; if it reaches the number of words, copy the blank unchanged.

Complexidade esperada

O(n + total word length) time, O(n) extra space

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.