Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Count Log Levels

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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

Ràng buộc

0 ≤ number of lines ≤ 10,000

Each line has at most 200 characters

Ví dụ

Ví dụ 1
Đầu vào:
lines = ["INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"]

Kết quả mong đợi:
expected = [2, 1, 1]
Đầu vào:
std::vector<std::string> lines = {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Kết quả mong đợi:
std::vector<long long> expected = {2, 1, 1};
Đầu vào:
string[] lines = new string[] {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

Kết quả mong đợi:
long[] expected = new long[] {2, 1, 1};

Giải thích

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

Ví dụ 2
Đầu vào:
lines = ["info lowercase", "DEBUG details", "ERROR"]

Kết quả mong đợi:
expected = [0, 0, 1]
Đầu vào:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};

Kết quả mong đợi:
std::vector<long long> expected = {0, 0, 1};
Đầu vào:
string[] lines = new string[] {"info lowercase", "DEBUG details", "ERROR"};

Kết quả mong đợi:
long[] expected = new long[] {0, 0, 1};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Only the first word of each line matters.

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.