Tutte le sfide
Facile 0 risolte 0% di accettazione

Count Log Levels

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

Cosa imparerai

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

Problema

Mostra traduzione

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].

Vincoli

0 ≤ number of lines ≤ 10,000

Each line has at most 200 characters

Esempi

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

Output atteso:
expected = [2, 1, 1]
Input:
std::vector<std::string> lines = {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Output atteso:
std::vector<long long> expected = {2, 1, 1};
Input:
string[] lines = new string[] {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Output atteso:
long[] expected = new long[] {2, 1, 1};

Spiegazione

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

Esempio 2
Input:
lines = ["info lowercase", "DEBUG details", "ERROR"]

Output atteso:
expected = [0, 0, 1]
Input:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};

Output atteso:
std::vector<long long> expected = {0, 0, 1};
Input:
string[] lines = new string[] {"info lowercase", "DEBUG details", "ERROR"};

Output atteso:
long[] expected = new long[] {0, 0, 1};

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Only the first word of each line matters.

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.