We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Regex and Parsing
- Hex Color Code
- Discussions
Hex Color Code
Hex Color Code
Sort by
recency
|
401 Discussions
|
Please Login in order to post a comment
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…
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…
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.
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 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)
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)