Similar RGB Color
We need to break the color string into its RGB components, and for each, we need to find the value in the set {00, 11, 22, …, ff} that minimizes the distance to the given component. We do this because the shorthand color will have repeated digits in each component, thus the closest one will always be among these choices.
Here is the Python solution:


In this solution:
We define a helper function
closest()
, which calculates the closest shorthand hexadecimal for a given twodigit hexadecimal number.The
divmod()
function is used to calculate the quotient and remainder of dividing the number by 0x11 (17 in decimal). If the remainder is greater than 8, we increment the quotient.We format the quotient multiplied by 0x11 back to a hexadecimal number.
In the main function, we apply the
closest()
function to every twodigit part of the color string, and then join them together. The start positions of these parts are 1, 3, and 5.
Readable solution:


Here’s what the code does:
The
closest
function takes a strings
as input, representing a part of a color (one of R, G, or B in ‘#RRGGBB’ representation). It first convertss
from hexadecimal to decimal, then it performs a division by0x11
(which is17
in decimal). The resultquotient
andremainder
are stored.If
remainder
is greater than8
, incrementquotient
by1
. This is because the shorthand for hexadecimal is formed by doubling a single digit, so we are essentially determining the closest shorthand representation to our original color part.After the adjustment, the function returns a hexadecimal representation of
quotient
multiplied by0x11
, ensuring it’s a 2digit string.The main function
similarRGB
generates a listcolor_parts
that contains the R, G, B parts of the input color, respectively. It then maps these parts through theclosest
function to get their closest shorthand representations, and joins them together with ‘#’ in front, forming a shorthand color.
Q&A
This question is relatively easy to understand once you get a clear picture of what it is asking and has no algorithmic tricks to it. However, the description could use some work to make it less confusing.
All the examples use capital letters but then the input/output need lowercase letters for some reason.
It talks about hex colors but then in the formula before the text case it uses invalid hexadecimal. It is probably done because of the squares(‘2’ is a valid hex digit) that come up in point number 3.
The formula to find a similar color is convoluted. I’m not sure if part of the problem is to figure out that you can work on each of the 3 pairs of hex digits independently and you don’t need to square the differences and subtract them from each other. But, squaring and subtracting instead of absolute valuing and summing in the description seems to add unneeded confusion. The formula in the test case should be something like the next two lines of pseudo code. absoluteValue(0x09  0x11) + absoluteValue(0xf1  0xee) + Math.absoluteValue(0x66  0x66) = 8 + 3 + 0 = 11. 11 is the lowest possible value which makes it the most similar number that can be written in shorthand.
Let’s break this down.
The problem deals with colors that are represented as hexadecimal numbers. A hexadecimal number is a number system that uses 16 symbols. We’re used to the decimal system, which is base 10 (09). Hexadecimal (often abbreviated to hex) is base 16, and uses 09 and then af to represent 1015. So, a number like ‘#09f166’ is a hexadecimal representation of a color.
Colors in digital screens are often represented in RGB (Red, Green, Blue) format. In hexadecimal, each pair of digits represents the intensity of Red, Green, or Blue. So, in ‘#09f166’, ‘09’ represents Red, ‘f1’ represents Green, and ‘66’ represents Blue. This hexadecimal format is a common way to represent colors in web development.
The question states that we can shorthand this 6 digit representation to 3 digits. That means ‘#abc’ is equivalent to ‘#aabbcc’. But we’re not looking for just any shorthand. We want to find the shorthand that is the most similar to the original color.
The ‘similarity’ between two colors is defined using a specific formula given in the problem. It involves finding the differences in the intensities of Red, Green, and Blue colors in the two color codes. Each difference is squared and then the squared differences are summed up. The lower this sum, the more similar the two colors are.
The problem is asking us to find a shorthand color that is most similar to the given color, according to the defined ‘similarity’. We can approach this problem by finding the closest shorthand for each RGB component separately.
A major point to understand here is that we can consider each pair of hexadecimal digits (each RGB component) independently. This is because the sum of the squared differences for each component will always give us the total difference.
This is how the problem is setup. The challenge lies in how to compute the closest shorthand for each component, and how to efficiently iterate over the possibilities. This involves a bit of number theory and understanding of how hexadecimal numbers work.
To simplify the calculations, instead of subtracting and squaring, we can simply find the absolute value of the differences for each color component, and then sum them up. The color with the lowest sum will be our answer.
This is a nutshell of what the problem is asking for and how to approach it. There are no algorithmic tricks, it’s more about understanding the problem and being careful with the calculations.