6 kyu
Strongest even number in an interval
173 of 1,219alexmate
Loading description...
Algorithms
Mathematics
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.
Quite a difficult kata to be honest.
Unfortunately, we had to postpone this task for some time due to the lack of solution options. Perhaps I’ll come back to it when ideas for “simplifying” the algorithm appear.
This comment has been hidden.
I failed "random tests" and "more tests" with "nosuchelement exception"(i use Collections and Map). I have no idea what's wrong, because i checked this code in IDE in interval 1 to 2 billions and it works fine. What could happen?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
proper assertion messages for C (solves this issue)
Approved.
performance
tag should be addedC translation does not/ambiguously show actual and expected values in case of failure:
random tests:
Resolved by new fork.
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )This comment has been hidden.
so my code works for the tests, but fails for the attempt, seems like my code is timing out? lol damn seems like the test values are prety high...
I just use python to express the algorithm ,however,it turns to be time-consuming.T^T
COBOL translation.
approved
can u give a little hint where to search for a optimization or what concepts are usefull? Im a beginner and have no ideas what to try or what to research..
Try to tackle the bit representation of the numbers and find a pattern that tells you how the strongness of the number increases or decreases.
OP solved it, closing
This is not 6 kyu. :D Really nice challenge!
Yes but somehow it ended being 5kyu.
Nice Kata! slightly tougher than 6
Yeah, I proposed it be 5 kyu, but in the ended up being 6kyu.
This comment has been hidden.
nice :o
Very difficult if you choose the wrong path (as i did) and straightforward if you take the right one.
wtf! Execution Timed Out (16000 ms)
Your code is too slow.
Nice kata, thank you!
It was nice to see a broad range of ways in which this (at least in C) had been solved too.
should be 5 kyu.
Can't be changed.
This comment has been hidden.
48 -> 24 -> 12 -> 6 -> 3 Strength 5 56 -> 28 -> 14 -> 7 Strength 4
A little typo in the description:
"Note that programs must run within the allot[T]ed server time"
..
Its more complicate than it looks like just because the time out thing. Don't try to do 2 loops or a recursive function; it will compile and run but wont be enough. I wasted a lot of time with that.
It works with 2 loops ;)
What does it mean to "successfully" divide by 2 "before we reach an odd number"? Dividing by 2 evenly? Dividing by 2 such that the quotient is an even number? Both?
It means dividing by 2 evenly. The "before we reach an odd number" gives it away. I should use the word "evenly" in description so it's more clear.
please help me ? why i got 12000 ms server error
In short, your solution (algorithm) is inefficient such that it does not pass all the test cases within the 12000ms window in which all code is allowed to run on the Codewars server(s). More about that here --> https://en.wikipedia.org/wiki/Time_complexity
This comment has been hidden.
This comment has been hidden.
Hint: binary ~~ OP long gone, closing
This comment has been hidden.
OP solved it, closing
I passed the sample tests but when I press attempt, it shows Response received but no data was written to STDOUT or STDERR. I'm coding in Python. Help!
All my code versions passed the test cases. but they all seem to time out, Dont understand this kata.
That is beauty of algorithms. Everytime you do loop you add n time to your execute time. if you make loop in loop it is n^2 time and so on. You need to find better way to make this program a little bit faster.
Ruby translation
The Ruby styleguide suggests to use
snake_case
for function names (notcamelCase
)Mistake in the question.
for the input [5, 10] return 8 (5, 7, 9 have strongness 0; 6, 10 have strongness 1; 8 has strongness 2)
8 has strongness 3 i guess because 8 / 2 = 4 4 / 2 = 2 2 / 2 = 1 So the strongness should be 3.
Fixed.
This comment has been hidden.
Not an issue. If you need help, use
question
instead.n
andm
go as high as billions in size, you can solve the task only by working with numbers themselves.Haskell translation
Already approved.
JavaScript translation is ready.
Please see comments directly on translation.
Done by @alexmate.
Doing the C version.
Pressed attmpt for the first time, and the codes times out - as expected. However, I am trying to print out which parameters I am getting - but none shows (as it times out), and nothing is shown by the calling code.
Not a kata issue. ( There are 11 solves in C, so it can be done. )
Yes, I know it can be done - one of those 11 is mine.
The problem remains the same. The calling/testing procedure ought to print out what the parameters are, as the function timing out will not show even if printed to the console prior to going into the code that takes too long to execute.
Have you tried
fprintf(stderr, "%d %d\n", n, m);
? You probably get a code 137 "Max Buffer Size Reached (1.5 MiB)" long before it times out (if you do something like that inside a loop), but in either case it still prints something like:
That stderr is a lifesaver. stdout gets randomly interspersed with the colored nested test result sections :(STDERR
1 2
Execution Timed Out (12000 ms)
This comment has been hidden.
You're printing too much.
Thanks for the reply... But this happens even when I am not printing anything?!
Really? What language are you using? I'll test it.
Happens on all versions of Python. I can give a pastebin link to the code if that helps.
Sure
This comment has been hidden.
I just tried your code: it looks like you're consuming all the memory you're allowed to use.
Too many strings instances created, I guess
That's really strange. Your code works, even for the biggest test cases. It may be a codewars issue.
Edit: Ah, ok B4B's reply makes sense.
Thanks for trying my code... So error 137 means MemoryLimitExceeded?
I am not sure how it could happen in my case as I am discarding many numbers that are not needed... I can't even print the case for which this is happening, so I may have to try totally different approach. :/
@Avanta: I have tried my code on huge numbers that would normally cause memory limits on Python... like n, m being 1, 9223372036854775807 and it works in an instant.
I am really confused now with this error 137... Can't think of a case which would cause MLE.
Yeah, thats what I tried too at first. Then I realised:
Something like
n = 1
,m = 2129680158
works instantly. However,n = 1151592177
,m = 2129680158
times out. I haven't really looked at how your code works. But it seems to take longer for a highern
?thing is, I guess you're limited by the amount of memory CW is allowing you to use, not the amount of memory python is capable to use on your personnal station.
This comment has been hidden.
The reason you're getting a memory error is because you're converting a large
range
into a list. However, this is actually unnecessary. You can index arange
object like you would with a list.Once you change that, you no longer get the memory error, but your code times out instead.
Yeah, figured this out. Removed those bits from my code now... Thanks for the help! :)
No problem :)
Interesting and challenging kata! But i think that solution for this kata is too hard for 6 kyu. Should this kata be 5 kyu?
Yes, I think this kata should be 5 kyu.
Meh
ranks can't be changed ~~
C translation is ready.
Beautiful kata! I found it a little challenging for a 6 kyu
Kindly add
optimization
orpuzzle
tagAdded
I have code which passes the sample test, but doesn't work with the other tests - it kicks me out with exit code 136 and no further explanation. It doesn't even show "more tests". Does anyone know what exit code 136 means?
I primarily code in Python, so there might be an error somewhere.
This comment has been hidden.
Found the problem! For some reason, one of the values in my function kept overflowing. Changing it from int to long made the program work.
Can someone please explain how the three examples have those return values? I understand what is being said in the instructions, but I don't see how those examples return what they do. I'm sorry if this is a dumb question.
The "strongness" of a number is calculated like this:
You simply calculate this "strongness" value for every number in the range, and return the number with the highest "strongness":
Examples
for the input [1, 2] return 2 (1 has strongness 0, 2 has strongness 1)
for the input [5, 10] return 8 (5, 7, 9 have strongness 0; 6, 10 have strongness 1; 8 has strongness 2)
for the input [48, 56] return 48
Elixir translation awaiting approval.
Can you please re-fork the kata and re-submit the translation?
I can't approve the translation right now, Codewars won't let me because of the merging problem with the Description part of the kata.
@alexmate: Try this one.
Thank you, approved.
How many random test cases are there? I'm able to manage ~70 of them within those 16s before the code times out. Probably my code is too inefficient, but im interested if i need just a bit more optimizing or a lot more.
Thank you.
// Edit: Talking about Java here.
you need another approach, yes. There are 100 random tests, but if the problem is approched the correct way, they re solved in no time, actually.
OP solved it, closing
This comment has been hidden.
Hi, thank you for the feedback.
Yes, I did look how to implement random tests and I've implemented them.
Now, the naive approach will not pass the tests.
This comment has been hidden.
This comment has been hidden.
Ah, thank you again.
What methods should I make private in general in the test code?
the solution ;)
btw, you'll need another fixed edge case of kind I suggested, but with
m
exactly equal to the output (to check the correctness of the boundaries).sure you have enough tests for C++?
I made the solution private in Java and C#, also added another fixed test case and 100 random test cases like you mentioned.
What do you mean about the test cases for C++? Evey language has the same test cases.
I mean that C++ is (generally) more performant, so it might need more tests. No idea if that's a problem or not here, tho.
Oh, I think it's not a problem.
Thank you for the suggestions.
Also, what do you think about the rank of the kata? Is 5 kyu good enough?
doesn't sound unreasonnable to me ;)
Hey, how is the rang of the kata decided?
I think this kata should be 5yu
I agree with that. But thanks to some PUs who are ranking the number of chars more than the complexity of the way to come up with the solution, averages were far lower than this when I approved it, unfortunately... (errr if I'm the one who approved it...? Not sure anymore... But the answer will stay the same)
edit: the ranks aren't modifiable anymore, after approval (not by users, at least).
So is there a way a codewars moderator could change the rank? This isn't a 6 kyu kata at all.
nope, only an admin can do that
edit: additionnal info about "local definitions":
mod
written next to their pseudo, when readin a message in the discourse (=> kazk/jhoffner, afaik)Where can I ask an admin for rank change?
cw's github repo, I guess. You can try, but...
At least in C++, the "random" tests aren't random.
Right, I have to learn how to write random tests.
Thanks for the feedback.
implemented