6 kyu
HTML dynamic color string generation
322 of 2,386Javatlacati
Loading description...
Puzzles
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
My solution shouldn't have passed, but just got a lucky run when I was looking for test feedback
Please describe your solution, specify the language, and also pinpoint what additional fixed or sample tests or random test validation should be added to invalidate your solution.
Python. I mistyped the padding. But if you never need the padding, which can seemingly happen, it will pass without discovering the problem. This is hard to test because of the random element, with the problem coming from "inside" the function and not being passed in, aside from just running more and more to have a higher chance of catching everything.
This comment has been hidden.
The method should be made
static
in the initial solution in C++.Fixed.
It is not explained anywhere what the input parameter is for in C++.
Tests should import the user's solution AND use that import.
for what language?
Python again.
it was then already resolved it, reissue when you have new details
Are you joking? You've approved a fork which reverted the fix.
Edit: fixed it myself.
BTW I never approved any fork reverting changes
The description is very poor in my point of view.
Using Python 3.8
you can suggest another description that adjusts more to your point of view.
I have all tests passed, but the aBunchInARowTest is failed and i have no idea where the Problem is... Can anyone help me for this task?
Many thanks to the community!
stack trace:
java.lang.StringIndexOutOfBoundsException: begin 0, end 8, length 7 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319) at java.base/java.lang.String.substring(String.java:1874) at GenerateColorRGB.generateColor(GenerateColorRGB.java:13) at GenerateColorRGBTest.aBunchInARowTest(GenerateColorRGBTest.java:52) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40) at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80) at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229) at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) at io.qualified.junit5.RunTests.main(RunTests.java:25)
it means you have a problem with your indexes
This comment has been hidden.
Since random number generators are typically generated with a seed, and if you omit the seed, it will be based on something like the current system clock, what can often happen is that different Random instances created in quick succession, like you're doing here, might have the same seed and thus emit the same values (note: this is based on how .NET works, but I imagine Java might suffer from a similar issue). You should be asking yourself, however: "Why am I using multiple random number generators". A single one, called 3 times in a row, will do the trick just fine. That's how random works.
Yes, I had tried that too: Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()); Result: duplicates
Try logging your colors/results to the console and inspect them. The problem might become obvious (this is a guess, but generally the best first approach at debugging)
This comment has been hidden.
Please send your answer
I solved it recently, no need!
still please send the code that printed that error and mark it as spoiler.
Tests should import the user's solution AND use that import.
what language?
Python.
kindly check again
python: not handled yet. See here and the messages in the same thread.
I will close the issue once the pending kumite is merged. Thanks!
Done !
I think my solution should have been rejected as the length of the hexadecimal number does not garanted to be 6 digits which is not valid on the web. Am I misunderstanding something?
hexadecimal length should be seven characters including '#' and a combination of numbers and letters or each lonely which language ?
python:
=> the tests are relying on the imports of the user
Why 6000 assertions!??? => this is bad because most browsers do not like that much data to handle. => to reduce to something more managable, or if that many of tests are needed, they should be done manually "under the hood", with one single explicit assertion per batch of tests.
Correct me please, if I am wrong somehow It runs without
random not defined error now
, I think it was because of mutation or something like thatassertions of main problem description in Java was 10000 X 3 10000 random cases with 3 different tests for each one
so I did the same in python, forgive my language I am self learner I don't know what single explicit per batch means in coding so if you please explain.
random
: you just forgot to actually import the random module in the test cases section, that's all. Currently, the use of random.seed is relying on the import done in the user's solutionThanks for your great contribution Done.
nope... you still have 6000 displayed assertions!
I put some
break
here and there for specific reasons. I currently see none of them in the tests...kindly check my pending translation it gives 18 tests. I did all breaks exactly as you suggested
the kumite pending results
oh, you did that in a fork? I thought you actually updated the tests. This explains that...
But in that case, you shouldn't have closed the issue (since nothing was ipdated yet. . I took a look. Seems ok excpet for one thing:
if not all([check1 ,check2, check3]):
instead ofif any([check1 ,check2, check3]) == False:
(which won't do what you wan't). Or even simpler:if check1+check2+check3 != 3:
Thanks for your guide, I learned alot of stuff in just one day. I added this
if check1+check2+check3 != 3:
also in the fork.I will close the issue once it merged
This comment has been hidden.
HTML colors are hexadecimal because each color goes from 0 to 256, you are only considering decimal
Letters should only go from A to F. This goes both to upper and lower case. It seems you are calling every possibe letter in the alphabet.
This comment has been hidden.
This kata ccan be transelated to python also check this: python translationn
Hi,
See issue above, plz.
not good yet. ;p
see here
For This solution in ruby:
Should match hexadecimal color format
Should have the symbol and 6 digits
Same happened for Javascript (node v10.x)
fixed, your solution generates invalid values such as #1b973
It's funny how easy 6kyus were 5 years ago :)
Prolog solves this kata with real ease—in theory, at least. What I've done is implement a perfectly ordinary DCG (definite clause grammar), but it just doesn't run quickly enough.
So, then, I'm going to have to go away and figure out how to make my code run faster. As the hard part of Prolog is optimization—and, in particular, actually knowing what's going on with Prolog under the hood when it runs your program!—I'm more than happy to work on honing this important skill.
Javatlacati, do you have any idea how many colours it is that
random_test
is asking for 😃? Do I have to find a way to produce them specifically in a random order, as all I've done is create a grammar that generates HTML colour strings in a very Prolog-y way...?yes, for prolog I'm checking about 300 random colors
Ah, well. It turned out to be easy peasy, in the end 🙂.
An interesting diversion is to ask: How, in Prolog, do you manipulate an arbitrary DCG grammar to produce random output strings? Well, that's the rabbit hole I went down... 😃
This is very minor issue:
using namespace std;
is required and after removing this line testing code fail with compilation error. This is not good, because e.g. I never useusing namespace std;
. Better to let decide use this or not. Thanks.In C++ tests raise an error:
have you tried making it static? just saying :smile:
So easy.... :D Well thanks... Actually I have no idea what it does (almost all I know about C/C++ is solving katas on CW...), but usually the initial code states properly that... Shouldn't it be the same here?
The description says nothing about what we have to do with the randomnumber parameter in C++. In Javascript there's no parameter, and no need of one to solve the kata.
Well, first of all i need you to understand that this Kata is derived from a real world problem that arised at some point at one of my first works, the idea was to generate a random color for the charts dynamically (usually from 5 to 20), so you want it to be able to generate colors in the whole possible range to avoid them to be all the same color, you can also have some very similar colors. Second, it's really hard to port those criterii to some Kata and since I've found couple solutions that were limited a very limited set (e.g. randomize 0 and 1 to fill the possible colors), I designed the exercise to have a way to control the random seed for testability purposes. Some languages provide a way to modify the global seed like ruby, but for some you have to pass the reference to your RNG, I'm aware that such thing might be a security and design flaw, but the idea here is just to train you on randomness while using strings. I'm not a native english speaker and didn't wanted to confuse more on the description. If you want to provide a better description for that feel free to suggest or modify the Kata. If you have design suggestions for the excercise we can also discuse them
And how does writing a wall of text about seeding RNGs fix the issue? There's still zero explanation as to how C++ version is supposed to work.
@Javatlacati you did not solve the issue. In the description you must state what the input and the output are.
@Farekkusu it helps you understand that actually all the languages behave the same, and also I don't know why you expect a Kata to tell you the whole explanation, it's not stated anywhere. You can suggest improvements for that, however if you don't get the description or the excercise it's not really an issue for anyone. I such case you don't open an issue but a question.
I get the description, just it is incomplete for c++. See for example here : The description should clearly state all requirements the user is expected to fulfill to complete the kata. Unless the kata is a puzzle, users should not be given test cases that enforce requirements not mentioned in the description. The description should explicitly, unambiguously state which inputs have to be handled, which inputs do not have to be considered, and how to handle exceptional situations.
actually @akar-0 I don't have to, and nobody can force me change the description without my consent specially because I'm not getting paid for this. This is a community and for that I allow on all my Katas to be edited by anyone. If you don't have something explicitly written or you can get to understand how some code works or don't want to spend time on that it's not a Kata issue, it's not a deffective problem. So don't pretend the excercise has an issue or is less useful because of a description added years ago for a non native english speaker like me when you are part of this community and you don't appreciate my effort or tried to help or rewrite the description.
Not getting paid does not prevent you from respecting the katas requirements. This is and remains a kata issue. I don't understand why you pretend I don't appreciate your effort. Raising an issue is not a personal offense against you, it's a note that aims to point out and help correct the kata's defaults. Many katas have.
To be clear: I did appreciate several of your katas and upvoted them. True, you're not getting paid for your work here, like everyone else. And, also true, nothing compels you to solve issues of an approved kata. There are many unsolved issues in many katas, that's the normal process of maintenance of a collaborative web like this one. Just please don't mark issues as solved when they are not. Anyhow, thanks to Farekkusu for solving the issues in C++ version.
In Java, getting a failed test saying "there are duplicates", followed by one of the randomly generated strings. The question does not say that they all need to be unique, only that they be valid. Add that to description or change this test.
Same in Ruby, in 2021! One subtlety to add though, as a suggestion: perhaps the 'Randomizer' test could be adjusted to accomodate a more reasonable number of duplicates. It is fairly shocking how many perfectly random coin-tosses come up consecutively heads or tails, and while pseudo-random numbers generated by computers are not perfect, they are actually pretty good nowadays! I see my solution got through on about the sixth or seventh attempt, so it was deemed random enough, but not every time. Not ideal, but not the end of the world either. I would suggest an adjustment.
they don't need to be unique actually, the solution space is something like 255 * 255 * 255 different colors length and I'm just testing a couple thousands using the standard rng. For a real life scenario you'd probably want that your solution ca be able to generate all the possible colors and not just a subset. Doing so and verifying for such Hamiltonian path are also NP complete problems. Some people are ready for that challenge but not all of them. Your solution as validated here just has to be testable in terms of reproducibility, the pseudo rng for the tests actually allows (and most likely also generates) duplicates it only checks that something like the 10% of the whole possible colors are different, and that for me is a very reasonable number of duplicates allowed.
Thanks for the response. I agree that your threshold is not unreasonable, but it would be nice if it was stated in the problem. It does feel kinda of bad to have to run the code a few times until you pass based on how your Random Generator calls go. Or perhaps you could edit the error message from the test to says "There are > 10% duplicates" or something. Ideally, I'd love for you to add the 10% threshold in the challenge text and the error message.
If you get the errors of compile like "main.cpp:57:40: error: call to non-static member function without an object argument" Do the method 'generateColor()' as static. It helped for me
have you tried to make the dunction a static one? ;)
I'm getting the following errors in C++ :
I can pass the sample test by changing:
to:
I am getting all sorts of error about undefined 'regex' and not using std when pushing attempt.
please share your error exactly as appear in the console please
c++, Really nice Kata but no assertion for output length. I removed length validation from my solution but i was still able to pass the Kata.
This comment has been hidden.
C++ Seem to have done everyhting correctly. If I use randomNumber for generation I fail aBunchInARowTest because of some duplicate. If srand(randomNumber) and rand function together then I fail regex test. Dont know what is causing the problem.For regex says expected true and actual 0. Tried different combinations but keep failing either regex or the bunch test. Can you please help me out?
This tests is to ensure that we can get a hamiltonian path on the set of possible colors hence... for this tests you should have no duplicates for about 10000 colors I can't tests more due to codewars limitations.
Is there perhaps an error in the aBunchInARowTest for Java? When I "attempt" the kata all tests pass except this one which returns: #baf46 expected:true but was: false
Now, I am not sure how the test class has been written but #baf46 looks like invalid output to me so shouldn't it be false?
This comment has been hidden.
The random as parameter was intended to generate all of the possible colors by changing the seed sequentially, due to codewars time and memory restrictions test can check that you generate only about 10 000 different colors. I don't really think that your cheat can pass the full range of colors (but will do the test on my free time anyway). If it can it means that you've created a hamiltonian path i. e. a generator that doesn't repeat combinations in the search space.
This comment has been hidden.
This comment has been hidden.
If some one creates his own random number generator for this tests I can't really say that has a bad solution. For a more complicated Kata we can test that colors have different tonality when retrieved since it will be more pleasant to the eye specially if used to generate the colors of a chart. For now I think that test the RNG would deviate the attention from the original problem tht is RGB string generation.
I would argue that providing a Random via parameter and pointing out that you have to use that Random would not break the limits of this kata, especially not since it's a clear instruction. And any changes to the tests are unperceivable to any user implementing the Kata proper (aka, RNG-based) anyways.
But in the end it's your call.
This comment has been hidden.
for wich language you have seen this?
JavaScript
This comment has been hidden.
It only check for 1 result though.
What if I forget to leftpad the answer and so it has a 1/16 chance of failing?
This comment has been hidden.
The method "generateColor(int randomNumber)" has parameter "randomNumber". Need more information about that parameter.
It's a random number passed as parameter. How can I help you about that?
The description needs an explanation of what the randomNumber argument is for. Most solutions seem to be calling rand() themselves, which leaves randomNumber with nothing to do.
Also, the word "static" is missing from the function outline provided. I guess it's OK to make people figure this out for themselves, but maybe it wasn't intentional.
Actually.. I had a very ambitious test htat you should generate 90% of all the colors with your solution and that was the reason of passing the random. Since codewars is a little bit restricted on the Java solutions timeout it is only limited to test only 10 000 different colors of the 16 777 215 possible.
What is
int randomNumber
in the C++ version?it's supposed to be our random number generator, but has the advantage that we can send just any value so we can fully test values to have some different unique results.
Its computationally hard for the server, but if you want you can test for the whole colors possible.
Actually right now I'm taking a 1000 random sample from that huge universe just to gran you can get unique solutions because as you may have notticced one guy had a miss by one error on his code producin one more color than the whole possible set hence having one duplicate color on it's color generator on the Java version.
I'm comparing with the Java version. I get the idea with the random number generator there but here it's just a number. Is it supposed to be a seed?
I Only know
rand% max_num + start_num
please feel free to fork :PRandom numbers in C++: https://www.codewars.com/kumite/575d5659fcee864674000397?sel=575d5659fcee864674000397
That class with one static function in C++ looks so Java-ish... And
rand
-- so C-ish...Thet's becaise I'm
Java-Tlacati
wich means "the one that makes you a Java Man" :smile:Since it's still in beta you can collaborate to make it more "C plus sign plus sign"-ish