5 kyu
Prime number decompositions
322 of 1,596user3482173
Loading description...
Fundamentals
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.
The behavior of the code in case of exceptions is completely not described. The task is a puzzle. And a waste of time.
python new test framework + random tests + snake_case (with backward compatibility) are required. updated in this fork
Just noticed a small typo that should be corrected, "exemple" should be example
Description should be language-agnostic
There is a problem with tests for JS. I am getting the following warning:
assertSimilar is deprecated, use assertDeepEquals
Could please, someone fix this?
If n=2, the function should respectively return [2], [[2],[1]], [2].
Why would the list
UniquePrimeFactorsWithCount
include 1? Shouldnt the answer be:[2],[[2,1]],[2]
e.g. Any prime number the answer should similarly be:
[n_prime],[[1,n_prime]],[n,prime]
1 is the count of 2s, and the answer is only
[[2],[1]]
for that function.Fun kata, but description needs to be more clear! Thanks.
Ruby 3.0 should be enabled.
literally had to use list of prime numbers up to 10000 idk how to do it in other ways
This comment has been hidden.
but where are the normal tests???
very fun but the description needs to be more clear about what to do with the errors. for example errors should return [] [[],[]] []
Description contradicts itself, saying anything
<= 0
should result in an error, and then proceeding to say[]
,[[], []]
, and[]
should be returned for 0. EDIT: I realize now that the expected result for 0 is the expected result for all errors. This should be made more explicit, I'm not sure how I managed to solve without realizing this but it was not apparent to me upon reading the description.This comment has been hidden.
Java: one function name is incorrect:
getUniquePrimeFactorsWithProducts
in the description becomesgetPrimeFactorPotencies
in the code. Either correct the java version or put a note in the description, in the related part.Done.Change function name will break the solution.It will not be accpted.
So frustrating. I'm doing Java.
What is the expected return value for
getPrimeFactorPotencies(long n)
withn = 0
?I'm returning
new Long[]{}
and test fails with null pointer exception.worked just well for me. Are you sure that the null pointer exception isn't coming from somewhere else/for another reason?
Thank you for pointing it out. You were right.
Turns out it was coming from somewhere else.
there is a typo: exemple => example
Ruby, Pyhton:
snake_case
same for Java about the random tests
Looks like more an exercize on handling errors and corner cases. Nothing to do with decomposition in prime numbers which is trivial. I got all errors in non math issues.
totally agree! The prime number factoring is easy, the nitpicky 'edge' cases are silly (why return [] for 'a' ?????? ! and [1] for 1 - when 1 is not a prime number). The error handling is more difficult than the stated prime number problem.
I got totally lost trying to handle what was, I assume, it was hard to tell what the input was, complex numbers. Frustrating waste of time. There are other prime number factoring katas here that make much more sense.
------- added later .... Well, my bad on the complex numbers. It was me generating them. But, even with the problem solved, the error handling involved in producing a solution was not fun and it's a shame because the prime number related bits of this kata are nicely presented.
The kata is on prime numbers yet asks to return 1 if n = 1.
1 is not a prime number, and providing n = 1 should result in an error.
Well, it's up to the kata itself to decide how to handle edge cases.
getUniquePrimeFactorsWithCount(n) - should this not keep the prime numbers in the same order, such that when n = 100, we get the result [[2, 2], [2, 5]]
I can't get it to work. First, I was using ArrayList in my solution, then test[2] failed with a java heap size message. Then I was using ordinary Arrays. Because one does not know in advance how big it had to be, I was using a logarithmic estimate on the size and afterwards copied it in an array of correct length. Then I got an "unknown test error" or something on test[2], while the other tests were passing. Ok, so I made an extra loop in advance to get the size of the Array before creating it. But then it takes too long on test[2] and the process terminates. I'm out of ideas.
Oh maybe it is because I forgot the edging cases and so on.
This comment has been hidden.
i am submitting the code for java. The third kata test case is throwing this :::: getUniquePrimeFactorsWithCount: Expected 2-dimensional array with size 2 in first dimension expected:<2> but was:<0>
when i printed the n, the value was 0. so the answer as the author has mentioned should be Long[][] {} . is there something wrong with the test case.
if anyone could help please it would be very much appreciated.
This comment has been hidden.
Seems like it's an issue of misunderstanding ~~
This comment has been hidden.
I had some problems understanding the error handling too.
Errors, if:
The three functions should respectively return [], [[],[]] and []. So the first function should return [], the second one should return [[],[]], and the last []. I hope this is going to help you.
Thanks for the help(I finished), turns out my problem was that I was calling functions within a function so when I returned [] it wasnt passed on but stayed in the local memory of the outer function.
An interesting kata which has us working with the prime gem rather than asking us to pretend it doesn't exist. Thanks!
This comment has been hidden.
This comment has been hidden.
When I submit the code, it test the number 10 and the prime factors for 10 are: 2 and 5 but it throw this error
[[1, 5], [1, 2]] should equal [[2, 5], [1, 1]]
I don't understand why
EDIT: sorry, now I understand why
why?
You should have the factors in the first array, and the number of each in the second So one 2 and one 5 are written as an array containin the 2 and 5, and a second array containing the number of them; one of each so it's [[2,5],[1,1]]
And I was wondering why my code was wrong.... Thanks
And now it has been translated into both Ruby and Python.
I would still add some edge case with larger number and some random test to raise up difficulty a bit, but I hope that now with 3 option this kata will pass to the approved status soon enough :)
Nice variation on the primes theme and quite challenging with all the edge cases, but never frustrating; thanks were_cat!
Gladly marked as ready, just let me know if you could enjoy some Ruby and/or Python translations :)
This comment has been hidden.
I don't say 1 is prime, I say "The result for n=1 is arbitrary and has been chosen to return a usefull result".
In that case, the "useful result" you must expect to test against is:
[], [[],[]], [[]]
Have to agree with kgashok. Having it return arrays with 1s cuts against the logic of this kata
Just to reiterate what abekim said, you should take some time and clean up the descriptions for the other functions. It's not clear at all what those cases are asking for.
Some other notes:
You should probably think about giving these functions more informative names. I'm not sure the best names, but even verbose names like this would work:
getAllPrimeFactors(n)
getUniquePrimeFactorsWithCounts(n)
getUniquePrimeFactorProducts(n)
You should think about having the invalid case for
factorise2
be[[],[]]
. This follows the law of least suprise: the result is in the same format if it's correct or incorrect.Even better, have the failed case in all three examples throw an error. I'd even go so far as to having the case for
1
throw an error. In the real world, this puts the onus on the developer to determine how they want to handle edge cases, rather than returning an empty set.There's no reason to convert the test values to strings, this just obfuscates the results. Rather, use
Test.assertSimilar()
. This renders the values better when they fail (such as[[2,5],[3,3]]
), which is a lot easier to read than the flattened array (2,5,3,3
).You need to add descriptions to your tests. In fact, I recommend reading up on the describe() and it() methods to help you organize your tests. Something like this:
Cleaning everything up in this sort of manner will significantly help the solver without them resorting to peppering their code with
console.log()
values. This is what having a good test suite is all about, anyway.The biggest benefit of writing good, well-organized tests here on CodeWars is it will (hopefully) make you a better developer in the real world. :-)
This pretty much sums up what I would have suggested. The suggestion for edge cases is something I would like to see more in katas - it really helps to constantly be aware of unusual cases in real life!
Thanks for this comment.
Problems adressed:
Names of the function changed.
Invalid case for the secund function changed to the more intuitive: [[],[]].
Test.assertSimilar() used in the test cases.
Descriptions added to the test.
Edges cases (0,1,2) added to the test. 2 is normal. 0 and 1 chosen arbitrarily to be usefull and intuitive. (I would like your opinion on that)
Problems not adressed:
Throwing an error to the failled cases. (I am thinking about it but I would like more return on the edge cases 0 and 1 to see how to handle them. I also need some time to find about best practices for eeror throwing code.)
Edit: And some help on the comment would be good too. How do you add lists to your comment ? code ?
@Were_Cat this is much better. It's far cleaner to implement this way, I was able to delete ~10 lines of code and some abstraction.
All comments (actually, just about all input text, including your Kata's description) on CodeWars use Github flavored Markdown. You should definitely read through both MD the basics and the Github differences pages.
Lists are created by putting a list marker on the beginning of the line, one of these:
There's a lot more you can do with Markdown, including bold,
inline code
, separators:Headers,
And links.
Code for everything above:
Description improved, test cases described. Feed-backs and evaluations welcome !
This comment has been hidden.
One more thing, your descriptions of
factorise2
andfactorise3
are lacking. I took a lucky guess and assumed you wanted an array of multiples of all the same prime factors forfactorise3
, but even now, I don't know if that's what you intended for the readers. You might want to add a bit more explanation for both functions.Here's a suggestion for
factorise2
:factorise2(n)
returns an array of arrays, where the first is an array of unique prime factors ofn
in the ascending order and the second an array of how many times each unique prime factor occurs. Ex.factorise2(1000)
returns[[2,5],[3,3]]
.FYI: if you haven't figured it out, the code currently expects an empty array (
[]
) even for factorise2, rather than[[],[]]
, which is silly.Ah, that's one I didn't expect. Thanks for the reply!