+ 2

Please anyone tell me why the code is not passing the Test case 3 and Test case 4 in the "Hexadecimal colour code Problem"?

It's a problem in the community section named "Hex Colour code generator". https://code.sololearn.com/cDSKW39LCJRm/?ref=app

9th Jul 2023, 9:26 AM
Vaibhav Pandey
12 Answers
+ 8
Vaibhav Pandey , there is a build-in function in python that converts an int value to a hex string: r = 200 print(hex(r)) # => gives a result of 0xc8 => c8 r = 2 print(hex(r)) # => gives a result of 0x2 => 2 => 02 ignore the first 2 characters in the result string. if the string is only one character long after the previous step, we have to add a 0 (zero) as the first character.
9th Jul 2023, 9:57 AM
Lothar
Lothar - avatar
+ 5
Vaibhav Pandey , we can not see the the hidden test cases, so we can not help we you in this case. but what you can do is to shorten your code. > the code is repeated 3 times and is the same except of the variable names. try to use only one of them and run it in a loop 3 times.
9th Jul 2023, 12:59 PM
Lothar
Lothar - avatar
+ 3
Vaibhav Pandey your code is very difficult to read. You are trying to express in 50 lines what is normally a single line expression in Python. You used like 10-15 variables (with very similar names) and you have no comments in the code. For a superficial observer it takes too much effort to understand your algorithm. If you can explain your algorithm in a few sentences, maybe we can find the flaw in your logic.
9th Jul 2023, 11:04 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Vaibhav Pandey , for me it does not make any sence to dive deeper in your code. may be you can run somewhere in debug mode to watch the variables content and find the issue. but is it really worth the time and effort ???
9th Jul 2023, 11:12 AM
Lothar
Lothar - avatar
+ 3
Also another way to convert a decimal number to 2-digit hexadecimal string, is using f-string with format options. for n in range(256): print(n, f'{n:02x}') In this expression, :02x is the format mask for the value of n. x means hexadecimal conversion. 02 means to pad 0 digits from the left, so that the result is always 2 digits.
9th Jul 2023, 11:15 AM
Tibor Santa
Tibor Santa - avatar
+ 3
If the tests are hidden, then there is no way for us to know what they are. Make sure to read the task description again carefully, maybe some tests are related to exceptional cases (for example if the number is not in the range of 0-255)
9th Jul 2023, 11:33 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Lothar Thank you bro but I want to know the mistake in my code also.
9th Jul 2023, 10:00 AM
Vaibhav Pandey
+ 1
Tibor Santa Lothar Alright I am sorry bro that I didn't commented the code and wrote a 50 lines of code but I really didn't know that the hex() function existed.
9th Jul 2023, 11:30 AM
Vaibhav Pandey
+ 1
Tibor Santa Lothar And bro could you tell me what is the Test case 3 and Test case 4 so that I can cross check my code and find the error.
9th Jul 2023, 11:31 AM
Vaibhav Pandey
+ 1
That was a really heavy code on a small problem😳 Just use the hex function in Python, and it will then pass all test cases. You need to study a little bit more, how to code those conversions manually.
9th Jul 2023, 12:19 PM
Jan
Jan - avatar
+ 1
Lothar Bro I tried but it's so complicated I simply can't find a way to loop for 3 variables.
9th Jul 2023, 1:50 PM
Vaibhav Pandey
+ 1
Lothar Quantum Tibor Santa Thank you for helping me I just found a lot of errors in my code , and I am gonna del it .
9th Jul 2023, 5:41 PM
Vaibhav Pandey