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

First Invalid Age

Validate typed ages and report where the first bad entry is instead of letting the program crash.

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

  • Check that text contains only digits
  • Validate a number against a range
  • Report the position of the first bad entry

समस्या

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

A sign-up form receives ages as text. An entry is valid when, after trimming spaces at both ends, it consists only of digits (no sign) and its value is between 0 and 150 inclusive.

Find the first invalid entry so the form can point the user to it.

Implement first_invalid_age(values).

Input

values: The typed ages, in form order.

Output

Return the index of the first invalid entry, or -1 if all of them are valid.

सीमाएँ

0 ≤ number of entries ≤ 10,000

Each entry has at most 20 characters

उदाहरण

उदाहरण 1
इनपुट:
values = ["25", " 40 ", "abc", "200"]

अपेक्षित आउटपुट:
expected = 2
इनपुट:
std::vector<std::string> values = {"25", " 40 ", "abc", "200"};

अपेक्षित आउटपुट:
long long expected = 2;
इनपुट:
string[] values = new string[] {"25", " 40 ", "abc", "200"};

अपेक्षित आउटपुट:
long expected = 2;

व्याख्या

"25" and " 40 " (spaces trimmed) are valid; "abc" at index 2 is the first invalid entry.

उदाहरण 2
इनपुट:
values = ["0", "150", "007"]

अपेक्षित आउटपुट:
expected = -1
इनपुट:
std::vector<std::string> values = {"0", "150", "007"};

अपेक्षित आउटपुट:
long long expected = -1;
इनपुट:
string[] values = new string[] {"0", "150", "007"};

अपेक्षित आउटपुट:
long expected = -1;

व्याख्या

All three are digits between 0 and 150 (007 is 7), so the answer is -1.

कैसे हल करें

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

संकेत 1

Trim the entry, then check it is non-empty and made only of digits.

संकेत 2

Only after that, compare its value with 150.

संकेत 3

A minus sign makes the entry invalid, and 007 is a valid 7.

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

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

संदर्भ

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

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

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

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