Last active
October 13, 2017 20:47
-
-
Save Bovojon/63ad35fcab730a972857448b114d539a to your computer and use it in GitHub Desktop.
Zillow University Test on HackerRank - Converting Hexadecimal values to 32-bit integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Jon | |
Zillow University Test | |
''' | |
def css_string_to_color(colorString): | |
if (colorString[0]!='#'): | |
print("Error") | |
else: | |
if correctLength(colorString): | |
print("Calculating:\n") | |
hexa = colorString[1:] | |
if len(hexa) == 3: | |
double(hexa) | |
conv(hexa) | |
else: | |
print("Error") | |
def correctLength(colorString): | |
if (len(colorString) == 4) or (len(colorString) == 7): | |
return True | |
else: | |
return False | |
def conv(hexa): | |
list_of_alpha = ['a', 'b', 'c', 'd', 'e', 'f'] | |
add_list = [] | |
length = len(hexa) | |
for i in range(length): | |
char = hexa[i] | |
lower_char = char.lower() | |
power = length - i - 1 | |
if lower_char in list_of_alpha: | |
for alpha in range(len(list_of_alpha)): | |
if lower_char == list_of_alpha[alpha]: | |
integer_char = alpha + 10 | |
else: | |
integer_char = int(hexa[i]) | |
decimal_version = integer_char*(16**power) | |
print "\n" | |
print(char, integer_char, power, decimal_version) | |
print "\n" | |
add_list.append(decimal_version) | |
decimal = sum(add_list) | |
print(add_list) | |
return decimal | |
def double(hexa): | |
str1 = '' | |
for i in hexa: | |
str1 = str1+i | |
str1 = str1+i | |
print(str1) | |
if __name__ == '__main__': | |
css_string_to_color("#800080") | |
css_string_to_color("oops") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment