Einfach 0 gelöst 0% Erfolgsquote

Count Log Levels

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

Das lernst du

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

Problem

Übersetzung anzeigen

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

Einschränkungen

0 ≤ number of lines ≤ 10,000

Each line has at most 200 characters

Beispiele

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

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

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

Erwartete Ausgabe:
long[] expected = new long[] {2, 1, 1};

Erläuterung

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

Beispiel 2
Eingabe:
lines = ["info lowercase", "DEBUG details", "ERROR"]

Erwartete Ausgabe:
expected = [0, 0, 1]
Eingabe:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};

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

Erwartete Ausgabe:
long[] expected = new long[] {0, 0, 1};

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Only the first word of each line matters.

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.