आसान 0 हल किए गए 0% स्वीकृति

Count Log Levels

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

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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

सीमाएँ

0 ≤ number of lines ≤ 10,000

Each line has at most 200 characters

उदाहरण

उदाहरण 1
इनपुट:
lines = ["INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"]

अपेक्षित आउटपुट:
expected = [2, 1, 1]
इनपुट:
std::vector<std::string> lines = {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

अपेक्षित आउटपुट:
std::vector<long long> expected = {2, 1, 1};
इनपुट:
string[] lines = new string[] {"INFO server started", "ERROR disk full", "INFO user login", "WARNING slow query"};

अपेक्षित आउटपुट:
long[] expected = new long[] {2, 1, 1};

व्याख्या

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

उदाहरण 2
इनपुट:
lines = ["info lowercase", "DEBUG details", "ERROR"]

अपेक्षित आउटपुट:
expected = [0, 0, 1]
इनपुट:
std::vector<std::string> lines = {"info lowercase", "DEBUG details", "ERROR"};

अपेक्षित आउटपुट:
std::vector<long long> expected = {0, 0, 1};
इनपुट:
string[] lines = new string[] {"info lowercase", "DEBUG details", "ERROR"};

अपेक्षित आउटपुट:
long[] expected = new long[] {0, 0, 1};

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Only the first word of each line matters.

संकेत 2

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

संकेत 3

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

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।