In this Hex Color Code problem, You are given N lines of CSS code. Your task is to print all valid Hex Color Codes, in order of their occurrence from top to bottom.


HackerRank Hex Color Code solution in python


Problem solution in Python 2 programming.

import re
reg1="{[^}]*}"
reg2="""(#(?:[0-9A-Fa-f]){3}|#(?:[0-9A-Fa-f]){6})[^0-9a-fA-F]"""
n=input()
s=""
for i in range(n): s+=raw_input()
x=re.findall(reg1,s)
for l in x:
    y=re.findall(reg2,l)
    #print l
    for z in y: print z


Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
N=int(input())

for i in range(0,N):
    s=input()

    x=s.split()

    if len(x)>1  and  '{' not in x:
        x=re.findall(r'#[a-fA-F0-9]{3,6}',s)
        [print(i) for  i in x]


Problem solution in pypy programming.

import re, sys

N = int(raw_input())
for _ in range(N):
    line = raw_input().strip()
    codes = [j for j in re.findall('[\s:](#[a-f0-9]{6}|#[a-f0-9]{3})[\s:;,)]', line, re.I)]
    for code in codes:
        print code


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT

import re

in_block = False
for x in range(int(input())):
    line = input()
    if '{' in line:
        in_block = True
        continue
    if '}' in line:
        in_block = False
        continue
        
    if in_block == True:
        m = re.findall(r'(#[a-fA-F0-9]{3,6})', line)
        if m:
            print(*m, sep='\n')