4 kyu
Largest Numeric Palindrome
159 of 653qaiken
Loading description...
Algorithms
Performance
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.
There's nothing more humbling than finishing a performance kata and then seeing all the elegant solutions ;_;
Amazing and wonderful kata.
This comment has been hidden.
This comment has been hidden.
As the IndexError suggests, you are trying to access an index which "doesn't exist": In your code, you have a part where you write:
res = ...
(I have hidden the rest, so I don't have to spoiler this comment; I hope it is clear what I mean.)
Take this entire expression, and print it to console in the line before calling that
res = ...
line:So add this print statement:
print("TEST -->>> ", ### here insert the expression you are using to find res ### )
You will see what is going wrong in your console - for input
[48, 9, 3, 0, 67]
you will reach a point where you get:TEST -->>> ['']
This then fails when you use the following code later on in your program:
str(i)[0]
<--- what happens if index 0 does not exist?I did not see this zero because in the basic test, exactly the same without a zero, thanks :D
You're welcome, glad it helped debug a bit.
I have further edited my comment now to remove all code-related info, that way it doesn't spoil for other users.
As 937 * 113 = 105881 and the largest palindromic number that can be arranged from the digits of result is: 81518
why answer not 105501?
Because there is only one 0 and one 5 there? You should use the digits of the number 105881.
thanks, I misunderstood the condition
This comment has been hidden.
I think there are errors in tests: Testing for 7, 23, 87, 32210 -> expected 5417145 to equal 8510158
7 * 23 * 32210 = 5185810 and that can be rearranged to 8510158 there is no error.
Why do you think it's an error?
7 * 23 * 87 * 32210 = 451165470, you missed 87
8510158 is greater than 5417145, isn't it?
But there is no "8"s in 451165470, or maybe I misunderstood the kata
Result should be made from nums of the product or it should any number be up to the product?
I already told you how you get the number, it's not using all the arguments. See the last example.
Guys, we read carefully: a product of any number of arguments!
Ruby 3.0 should be enabled.
Enabled in this fork
Very clever edge cases!
Nice kata but the two different tasks are not really related...
These incorrect Python solutions fail at the random tests sometimes, but pass all the fixed tests. Fixed tests for those cases should be added. https://www.codewars.com/kata/reviews/557226b6385f599aac0000e2/groups/5fcf8917646b880001ea2f8b https://www.codewars.com/kata/reviews/557226b6385f599aac0000e2/groups/5fcf880d2814a500015cde76 https://www.codewars.com/kata/reviews/557226b6385f599aac0000e2/groups/5fcf8693c8999f00014bc7fb
I keep timing out :(
My script executes in 86ms on an online Python interpreter with this input (937,113,4782,1,456,0,24,7824,13,6985). Seems quite efficient to me.
What kind of test is taking so long ? How can I find it ?
You can
print input
to debug, or read 4th note. Anw, this is not an issue.Why for 3534 the largest palindrom is 353, but not 535? 535 bigger then 353.
You can't use the same number more than once.
3534
only has one5
but535
has two5
sAs the ranking cannot be changed (4 kyu) I turned it into a performance kata. Ruby & python are done, but need some help with JS.
@Voile
?Also updated description to be more clear
JS integers have a maximum precision of
2^52
, so I'm afraid you might have to nuke the JS version or not care about it ;-)OK, but some random performance tests (including a bunch of 0s and 1s) could still be added -- see ruby/python.
Cheers
Not sure this is an issue or I am just dumb. Can someone please verify output of following (random) input: [JavaScript] Input [82730, 3117, 3, 61] results in Expected: 310070013, instead got: 9513159 and I am not sure if this test result is correct.
Many thanks
it is correct ;)
Thanks found the problem.
I think this question should have more aggressive constraints for performance.
Added performance tests to ruby and python, js is still lacking.
Thanks for the kata! I used numpy to calculate the products, which introduced roundoff errors that were hard to spot. Numpy is expecting a large data set more than large individual numbers. I switched to an obvious homegrown, (and not very efficient), function for the products, which fixed it.
I get "414 should equal 868" for the test (numeric_palindrome(48,9,3,67),868). However when I run my code on my machine I get the right answer. I have created a list called palindrome which stores all the possible palindromes and my function returns max(palindrome) which is clearly 868. Any ideas welcome.
Is it me or is it a bug?
[ 15, 125, 8 ] product = 15000 pairs = { '0': 1 } singles = ['0', '1', '5' ] highest single = 5 result = 050 = 5
✘ Expected: 8, instead got: 5
The largest palindrome comes from 15*125 = 1875 -> 8.
If you're going to make a nonsense kata, at least make it only one nonsense task.
This should be two kata.
i) find all possible product combinations of arguments ii) find largest palindromic number from this jumble of digits
Combining the two is just painful and stupid.
--
This comment has been hidden.
'1875 generates the largest palindrome number, namely 8?' Anyone can hep me understand that? Thanks!
if you look at the characters of 1875, the biggest symmetrical number in it is simply 8 because every character is unique. since 8 is the biggest character, it is also the biggest palindrome.
Still can't understand.
As 937 * 113 = 105881 and the largest palindromic number that can be arranged from this result is: 81518. Why it's not 105501? Why it's 81518?
iheshi, because 105881 doesn't have 2 '5's and '0's in it to make the number 105501. The largest palindrome possible is when you arrange it as 081518, then drop the leading 0.
IMO the descriptions are okay. Closing.
Can someone please verify output of following inputs:[Python]
It should work for random tests too: 9765332209022335679L should equal 9865443007003445689L
It should work for random tests too: 977654433211040112334456779L should equal 99888766532000200023566788899L
... and for other random test cases with argument size >5
for input 1:
Output of my code: Pastebin
As my code is generating all possible permutation but I don't think there is any permutation to generate this output (Correct me if I am wrong).I just checked your example 1. and 2. for python and my code/solution generates 9865443007003445689L and 99888766532000200023566788899L as expected, so for me the testcases are correct.
Hi, thanks for reply. It will be a great help if you can tell which factors are generating this output.
I answered your question, so you can see and compare the factors (and results) in 1. and 2.;-)
This was by far my favourite kata so far.
I thought I was so clever with my first attempt, using a simpler "traditional" approach but this kata forced me to think about the task in a completely different way to how I normally would approach it.
Excellent work!
Two things: First, the test case
[34735, 56]
should resolve to 373, but instead expects 191.Secondly, all of the random tests give me this error. ReferenceError: res is not defined at sol_get_comb at sol at Object.Test.it at Object.Test.describe
The predefined tests all passed so I don't think it's a problem with my code. Help would be appreciated!
You must use a product of at least two factors (a product of 1 factor, well.., it is not a product)
how to check the test cases ? The case failed, but can not get the case.
@serendip: Use
console.log
?Also, that looks like an obsolete kata problem, so closing.
I like this kata, and I am 99% sure I have a solution, but one of the tests is buggy. I logged its inputs: 15, 125, 8. These numbers have a product of 15000, the largest palindrome from which is 5. The test says it's expecting 8, which I just don't think is right. All of my other tests pass. Please give me my magic internet points!
Edit: I'm sorry, I didn't see that it should be looking for the largest palindrom from ANY AMOUNT of the arguments. My bad.
Not exactly "any amount" of arguments, but specifically "at least two arguments".
Add a test case for args (1, 96, 3612) which should result in 9 and NOT in 7 (as it is accepted now). This was already said by jolaf.
i believe these test cases you are recommending should come under suggestions because this kata has been approved instead of being marked as an issue, because issues deduct points from the kata and the author until they have been resolved :)
Can’t anybody else fix the kata or the test cases?
they can but I just don't think it's an issue for the kata just a suggestion..
Well, it is an issue, because the test cases don’t detect wrong results. How is this just a suggestion? It is wrong by the definition given in the description.
Just added a test case similar to that mentioned by you.
This comment has been hidden.
Just added test cases similar to those mentioned by you (Ruby version pending fixes). Edit: Now ok.
Negative numbers are not tested.
This comment has been hidden.
Just added.
As the solutions http://www.codewars.com/kata/reviews/557226b6385f599aac0000e2/groups/57113fc6b25a464f3c000008 and http://www.codewars.com/kata/reviews/557226b6385f599aac0000e2/groups/570e4f46e5c9a0b18d001323 are very very slow for args with many 0 and 1 I suggest to add a time limit in the test. I.e. add a test case numeric_palindrome(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,57,62,23)
with a time limit.
Added this (plus some other perfomance tests) to all languages: many (most?) of the existing solutions were wiped out...
This comment has been hidden.
Just added a test case similar to that mentioned by you.
This comment has been hidden.
EDIT: My solution is written in Ruby I forgot to mention that it passes for every test case when I click "Run Tests" or whatever, and then it passes all but two (the two i mentioned) when I click "Submit".
First test about 191 vs 373 was just discussed, here products of at least two numbers are considered. As for the second, it's unfortunately your solution which is wrong. It states clearly: "Expected: 8, instead got: 50", your code is giving 50 as a palindrome. So your solution is wrong disregarding how very sure you are.
I felt into the same trap as well, it's a problem with your code.
Sometimes katas here are not correct, but this is very rare. Much more often when I see "wrong kata" it turns out, I missed some detail in conditions.
I'm going to run some more tests on my solution then. Thanks for responding, I was so sure that it was correct, so I'm glad I recieved some assurance from someone else that it isn't.
Jesse, I had the same problem, until I realized that I was making a palindrome with the numbers 005. If you are adding numbers the to beginning and end of an array or string to get your palindrome, then you need to deal with cases where you have two zeros.
This comment has been hidden.
It requires the product of any amount of numbers, see above the discussion with Jolaf.
In most languages as well as in math, operations on list/set are correctly defined for list/set of one element, including e.g. javascript's
redudce
and Haskell'sproduct
.I see your point, but maybe it is better if I update the description to be extra clear and follow the author's will.
This is a good idea.
Tell me if now it is more clear and/or if you wish to suggest further :)
Sorry, but I don't see any difference. Moreover, as we can see from issue above, it's not only me =)
The kata description has stated that "palindromic number made from the product of any amount of AT LEAST 2 numeric arguments". No issue now.
That was no doubt the hardest Kata I have completed so far. Excellent job. The randomized test cases certainly kept me on my toes!
Kata approved :)
Cool kata!
However, it seems to be a bug in random tests (in Python):
Bug for cases 1 and 3 - fixed (and you helped me a lot, finding a very weird edge case in all 3 languages); now onto others.
Bugs 2 and 4: if I am not wrong, you are creating a palindrome taking only a number (34735 and 61867, respectively), which is against the rule, as you have to use the product of any amount of numbers: I am not a native speaker, but if we talk about "product" it should always regard at least 2 numbers multiplied together, right?
Well, I'm not a native speaker too, but for me "product of just one number" sounds ok. :)
However, judging by the examples in the kata description, it seems you're right.
In this case, it probably worth adding some non-random tests to target this nuance specifically, as for now my solution still passes all tests from time to time. :)
Mh, not so sure about the product of just one number thing, but as usual you make a good point: added a specific edge test case for that; thanks again :)
Cool, thanks! :) Green light!
This is still not solved. The args (1, 96, 3612) are still accepted if the result is 7. But it should only be accepted if the result is 9! Please add a test case for this.
Great kata :)!
Just translated it into both Ruby and Python with plenty of random tests and also some good edge case, if you want to approve it.
I would like to add random tests to the JS version if it is ok with you.
Awesome work! Approved. You can definitely add some random JS tests. Thank you!
This comment has been hidden.
Fixed, thanks to you for the good catch on an edge case!
[There is some issue on Python too (I can't get why, as it worked when I submitted, mh) and already working to fix it!]
Edit: no, the Python version seems ok: it was just some time out on the server side. I'm checking it all again some more time for extra caution.
Ok, I finally revised and completed it in any language currently available, boosting the checker performances on edge cases (thus giving a bit more room to solvers!).
One mandatory note for @qaiken: in order to make it work, I had to comment your previous solution and substitute it with mine, as it didn't perform well with some edge cases and was a bit more slow (not too sure about the latter, though); no offence meant to your work and to the idea of this impressive kata, of course, so I hope you can be ok with it.
The tougher part on my side was to create a random variable generator that never EVER would create product over JS precision limit (15 digits, that is) and after a lot of tests I can say that should be assured for good. I used a small trick to achieve that, but as the random parameters are shuffled, it should all work elegantly without showing up ;).
Notice that now getting 1s and 0s as parameters is more likely (they were already tested, btw, and that was nice), so managing them in an efficient way can be the key to either solve or fail this kata in the allotted time limit.
Finally, I put an extra edge case so that in dealing with a maximum palindrome you will have to make special cases for 0s in the product: if you get, say, 10000 from 2 ore more numbers multiplied together, then "00100" won't be a valid palindrome (no leading zeros in your solutions!).
Currently, I think I saw no solutions dealing with it.
All in all, this kata is now imho ranked between 2 and 3 kyu, equally for any language: having taken into account that JS is a bit less performing, has the aforementioned precision issues and that you will have less high level pre-made functions, I evened the odd a bit with smaller ranges.
Feel free to test it to your heart content and let me know about whatever I may have missed or done wrong!
Super awesome! @GiacomoSorbi: Thank you so much for your work on this. This is the first kata that I have authored so I have learned a lot about testing, edge cases, etc.
This was your FIRST kata :o?!
"Super awesome!" should be my line, then :D
Looking forward on working on your next kata and glad you appreciated my humble work :)
Reading surtich's feed, I edited the description a bit, adding more examples and some tip to make the task less ambigous. As I am not a native speaker, any further feed to improve the description is more than welcome.
Of course, provided qaiken as an author is ok with giving a bit more examples, plus the way I choose to discuss them.
Giacomo, could you please see the issue I filed above against Python random tests?
I'm on it.
@GiacomoSorbi I've added six more test cases in all the languages. However, the current Ruby solution won't pass two of the new test cases so I've commented them out. Would you please help fix them?
Meh, again.
Just replace his reference solution with one from the solution page that actually works.
Done.
I think you should add some random tests.
Added now; I would be glad if you could tell me whether everything works as expected or not :)
Thanks. I think there are some mistakes:
1.- When zero is generated, the product is zero so the solution must be zero, but:
Testing for 4, 0, 7, 24514, 9, 35879, 65, 1, 1 It should work for random inputs too - Expected: 8642209022468, instead got: 0
2.- Some tests are wrong:
Testing for 10, 5080, 32, 268, 6 It should work for random inputs too - Expected: 8604068, instead got: 60906
The product (105080322686) is 2613964800, so the correct solution must be: 60906
Other wrong examples:
Testing for 4, 990, 3352, 2, 2786 It should work for random inputs too - Expected: 8529258, instead got: 22922
Testing for 4, 990, 3352, 2, 2786 It should work for random inputs too - Expected: 8529258, instead got: 22922
Thanks. I think there are some mistakes:
1.- When zero is generated, the product is zero so the solution must be zero, but:
Testing for 4, 0, 7, 24514, 9, 35879, 65, 1, 1 It should work for random inputs too - Expected: 8642209022468, instead got: 0
2.- Some tests are wrong:
Testing for 10, 5080, 32, 268, 6 It should work for random inputs too - Expected: 8604068, instead got: 60906
The product (105080322686) is 2613964800, so the correct solution must be: 60906
The instructions are to find the 'product of any amount of numeric arguments' supplied, which means you don't have to use all of them. In the example where one of the numeric arguments was a zero, your solution should be using digits from the product of a combination of numeric arguments not including the zero [e.g. 9 * 65 = 585 (incidentally already a palindrome)].
As sayfidz said, one of the difficult point is to check for every possible amount of factors to generate the product with the digits (not necessarily all of them) which could generate the largest palindrome of them all.
So, if you get one ore more 0s, a wise idea would be to initially skim them in the beginning; same goes with 1s, but with a twist and a bit more carefully.
Your solution does not work with some numbers. For example:
Thanks for pointing that out! I fixed the solution and added more tests.
This test case is wrong:
The correct version is:
As stated above: you don't have to necessarily use ALL the factors to generate a product: you have to compute it using
product of any amount of numeric arguments
.In this case the product that generates the largest palindrome is
15*125=1875
, from which you only take 8.Hope this is more clear, now :)