Sort by

recency

|

402 Discussions

|

  • + 0 comments

    I have learn that in this case of situation, with "exactly 3 or 6" we can do {3}|{6}.

    Instead, we have to write twice the instruction, with firstly {6} (the biggest number), then {3} (the lowest number)

    Then, findall() occurences which respect this pattern (with # at the start obviously) and extend an output list to write every results at the end.

    Here the code :

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import re
    
    pattern = r"#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})"
    
    N = int(input())
    output = list()
    for _ in range(N):
        current_line = input()
        
        if ":" in current_line:
            m = re.findall(pattern, current_line)
            if m:
                output.extend(m)
    
    
    for element in output:
        print(f"#{element}")
    
  • + 0 comments

    OMG!

    HackerRank's tests for this challenge are so lax!

    The instructions say the hex numbers may be three or six digits in length. It specifically shows some examples of invalid numbers that are between those two lengths, using four digits.

    However, I see many people using patterns in their regexes like this…

    [0-9a-f]{3,6}
    

    That would match hex numbers 3, 4, 5, or 6 digits long. The HackerRank tests apparently do not include numbers with 4 or 5 digits, though, because that pattern passes all tests!

    Better patterns that meet the requirements in the instructions could be…

    ([0-9a-f]{6}|[0-9a-f]{3})
    
    ([0-9a-f]{3}){1,2}
    

    Each of those would match hex numbers 3 or 6 digits long, but not those with 4 or 5 digits.

    This shows that it's not wise to follow all the requirements for each challenge. Code for the simplest case, then test it with the "Run Code" button. When code passes that simple test case, try using the "Submit Code" button. If the code fails any of those tests, then try improving the code to follow more requirements or spend a few Hackos to see the test case and code for that.

    Is it the intention of the HackerRank admins to accept code that fails to follow the requirements? I don't know, but it seems to be working to the advantage of the users. Or is it? Maybe users end up learning the wrong lesson from the challenges instead.

  • + 0 comments

    Could the challenge get any easier‽ 😆 This was easier than the earlier challenges, yet it was worth more points, 30 instead of 20.

    Thank you, HackerRank! ❤️

    import re, sys
    
    # We don't even CARE about ignoring the first
    # line of input containing the number N.  It
    # won't match the pattern anyway.
    print('\n'.join(m[1] for m in re.finditer(
      r'[:,][^#\n]*(#([0-9A-Fa-f]{3}){1,2})',
      sys.stdin.read())))
    
  • + 1 comment

    import re p=r'[#][0-9A-Fa-f]{3,6}' s='' for i in range(int(input())): s+=input() x=list(re.findall(r'[{][^{}]+[}]',s)) l=[] for i in x: l+=list(re.findall(p,i)) for i in l: if len(i) in [4,7]: print(i)

  • + 0 comments

    import re N = int(input()) A = [] for i in range(N): A.append(input()) text = ''.join(A).replace(" ",'') #To Replace all the white spaces pattern = r'#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})(?!{)\b' # codes = re.findall(pattern, text) for code in list(codes): print(code)