Friday, December 6, 2019

Hex Color Code - Hacker Rank Solution

Hex Color Code - Hacker Rank Solution
This problem can be solved using regex.
We can divide this into two parts:
Part 1: Separate the code in '{ }'.
Part 2: Apply the appropriate regex to identify the hex color code in the codes of Part 1.

from __future__ import print_function
import re

css = ""
for i in range(int(raw_input())):
    css+=raw_input()
    css+='\n'

inside_brackets = re.findall(r'\{.*?\}', css, flags=re.DOTALL)
for property in inside_brackets:
    map(lambda i: print(i,sep='\n',end='\n'),(re.findall(r'#(?:[a-fA-F0-9]{3}|[a-fA-F0-9]{6})\b', property)))

No comments:

Post a Comment

Powered by Blogger.