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

Count Log Levels

Read the lines of a log file and count how many are INFO, WARNING and ERROR.

O que você vai aprender

  • Process a file line by line
  • Take the first word of a line
  • Count categories with a small tally

Problema

Ver tradução

Programs write log files with one event per line. Each line starts with a level word followed by a space and a message, for example ERROR disk full.

Given the lines of a log file, count the lines whose first word is exactly INFO, WARNING or ERROR. Lines that start with anything else (including lowercase levels) are ignored.

Implement count_log_levels(lines).

Input

lines: The lines of the file, without line breaks.

Output

Return [info_count, warning_count, error_count].

Restrições

0 ≤ number of lines ≤ 10,000

Each line has at most 200 characters

Exemplos

Exemplo 1
Entrada:
lines = ["INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"]

Saída esperada:
expected = [2, 1, 1]
Entrada:
std::vector<std::string> lines = {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Saída esperada:
std::vector<long long> expected = {2, 1, 1};
Entrada:
string[] lines = new string[] {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Saída esperada:
long[] expected = new long[] {2, 1, 1};

Explicação

Two lines start with INFO, one with WARNING and one with ERROR: [2, 1, 1].

Exemplo 2
Entrada:
lines = ["info lowercase", "DEBUG details", "ERROR"]

Saída esperada:
expected = [0, 0, 1]
Entrada:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};

Saída esperada:
std::vector<long long> expected = {0, 0, 1};
Entrada:
string[] lines = new string[] {"info lowercase", "DEBUG details", "ERROR"};

Saída esperada:
long[] expected = new long[] {0, 0, 1};

Explicação

"info" is not "INFO" and DEBUG is not counted. The last line is just the word ERROR, which counts: [0, 0, 1].

Como abordar

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

Dica 1

Only the first word of each line matters.

Dica 2

Split once at the first space and compare that word exactly.

Dica 3

Case matters: "info" is not "INFO", and a line can be just the level word.

Complexidade esperada

O(total length) time, O(1) extra space

Referências

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

Aprenda com outros

Soluções da comunidade

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