0
[SOLVED] Hex Color Code Generator - Python
I rewrote this several times, each with a reducing amount of code. I think this version is over 50% shorter than my first attempt! Has anyone got a more streamlined way of doing it? Obviously I am not seeking answers telling me to use the hex function! ;-) https://code.sololearn.com/c5pgFG7420bD/?ref=app
7 ответов
+ 9
Is it more streamlined? I'm not sure, but I know it has less lines. :x
hexlookup = "0123456789abcdef"
return '#'+''.join([hexlookup[i//16]+hexlookup[i % 16] for i in RGB])
+ 1
Thanks Hatsy Rei ... I like that
0
def func(n):
s = ''
h = '0123456789abcdef'
while n > 0:
#↓↓↓don't understant how it work
s = h[n % 16]+s
#↑↑↑I will be glad if you explain
n = n // 16
return s
print("#{0}{1}{2}".format(func(int(input())),func(int(input())),func(int(input()))))
0
Only passing 2 out of 5 cases, am I missing something?
rgb = []
for i in range(0, 3):
rgb.append(int(input()))
from math import floor
def convert_to_byte(val):
pre_div = val/16
if pre_div < 10:
mod_rem = val % 16
return f"{round(pre_div)}{mod_rem}"
elif pre_div >= 10:
mod_rem = val % 16
ch_init = ""
test_val = int(pre_div)
if test_val == 10:
ch_init = "a"
elif test_val == 11:
ch_init = "b"
elif test_val == 12:
ch_init = "c"
elif test_val == 13:
ch_init = "d"
elif test_val == 14:
ch_init = "e"
elif test_val == 15:
ch_init = "f"
return f"{ch_init}{mod_rem}"
elif pre_div % 16 == 0:
return str(int(pre_div) * 10)
hex_col = []
for j in rgb:
hex_col.append(convert_to_byte(j))
hex_str = "".join(hex_col)
res = f"#{hex_str}"
print(res)
0
I tried with dictionary.
https://code.sololearn.com/cnctIAzr7J0I/?ref=app
0
def hexa():
a= int(input())
out=[]
d={10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f'}
while a//16 > 0:
h = a%16
a = a//16
if h > 9:
h= d.get(h)
out.insert(0,str(h))
if a > 9:
out.insert(0,d.get(a%16))
else:
out.insert(0,str(a))
return(''.join(out))
y='#'
for i in range(3):
y += hexa()
print(y)
- 1
red = int(input())
blue = int(input())
green = int(input())
def hexadd(x, y, z):
a = (hex(x))[2::]
b = (hex(y))[2::]
c = (hex(z))[2::]
return ("#" + a + b + c)
print(hexadd(red, blue, green))