Home > Enterprise >  {-truncated-} in hackerrank
{-truncated-} in hackerrank

Time:08-16

I tried "Validating UID Challenge" in hackerrank and one of these test cases is: YD780V5355{-truncated-} in input and the output is Valid{-truncated-} at test case 3. So what is {-truncated-} really mean in this situation? The length of the string is only ten. Here's my code:

def checker(string):
    num_of_up = 0
    num_of_di = 0
    exist = []
    if len(string) == 10: #Exact 10 chars
        if string.isalnum(): #Must be alnum
            for char in string:
                exist.append(char)
            if len(exist) == len(set(exist)): #No duppicated char
                return True
            else:
                return False
        else:
            return False
    else:
        return False
    for item in exist:
        if item.isdigit():
            num_of_di  = 1
        elif item.isupper():
            num_of_up  = 1
    if num_of_up >= 2 and num_of_di >= 3: #more than 2 uppers and 3 ditgits
        return True
    else:
        return False


UID_lst = []
for num in range(int(input())):
    result = []
    ask = input()
    UID_lst.append(ask)
    for item in UID_lst:
        if checker(item):
            result.append("Valid")
        elif not checker(item):
            result.append("Invalid")
for item in result:
    print(item)

CodePudding user response:

In HackerRank, {-truncated-} means that the input/output goes beyond 20 lines, and thus the remaining lines are not printed.

CodePudding user response:

So what is {-truncated-} really mean in this situation? The length of the string is only ten.

That is not guaranteed. The length of the string could be anything in a test. The idea is that some of the input will not adhere to the rules, so there can be strings that are (much) longer.

Remarks on your code

  • for item in UID_lst: is performed for every test, but this list is getting longer each time and the previous values in it will be iterated again with the next iteration of the outer loop. The number of outputs will be far to great this way, and guarantees that your code will fail the test. You should not have a nested loop at all. The outer loop already has the correct number of iterations. You should not need this UID_lst.

  • if checker(item) ... elif not checker(item): why would you run checker again with the same input in the elif part? If it returned False the first time, it surely will return False the second time. The elif should just be an else

  • In checker, the statement for item in exist: will never be reached. It is dead code. It is guaranteed that a return statement will have been executed before ever getting there. Notably, the return True statement in the nested if is coming too soon. At that point you can't be sure all is OK as the test for the number of digits and letters has not been done yet.

  • exist.append(char): it is not needed to create a list with the characters. You can directly pass string as argument to len and set.

  • Not a problem, but it is better to flatten your if structure. Just identify each condition that makes the input invalid and return False if so. No need for an else to accompany such an if. It is already sure that execution could only continue beyond that test when return False was not executed. Only when all tests have been executed like that (each time check for the negative condition and a related return False), then you have one return True at the very end of your function. That only gets executed when all the negative tests were ... negative.

If you fix those issues, your code will pass. Here is your code with those corrections as a spoiler.

def checker(string): if len(string) < 10: #Exact 10 chars return False if not string.isalnum(): #Must be alnum return False if len(string) != len(set(string)): #No duppicated char return False num_of_up = 0 num_of_di = 0 for item in string: if item.isdigit(): num_of_di = 1 elif item.isupper(): num_of_up = 1 if num_of_up < 2 or num_of_di < 3: #more than 2 uppers and 3 ditgits return False return True for num in range(int(input())): if checker(input()): print("Valid") else: print("Invalid")

And to make it more compact:

def checker(string): return (len(string) == 10 and string.isalnum() and len(set(string)) == 10 and sum(ch.isdigit() for ch in string) >= 3 and sum(ch.isupper() for ch in string) >= 2) for num in range(int(input())): print("Valid" if checker(input()) else "Invalid")

  • Related