Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Not only is 2kB an insignificant amount of memory to use for this, but also you already have that memory available. On most systems, the default stack size for the main thread is 2MB. Unless the rest of your program is using 99.9% of its available stack, it costs almost nothing to dedicate 2kB of stack to make this array, and pop it from the stack after this function finishes.
As for heap allocation, I'm not very familiar with Rust's allocator, but it's quite possible that creating a hash map for this will in some cases be forced to take 4kB, since most operating systems think about memory in 4kB chunks.
This is no real assembly, just follow the description: why should we care about the registers size? And without any precision it is clear that you are not expected to operate on unsigned integers. About the rewording, this is at most a suggestion, not an issue.
The array is indisputably the best solution. What's the better alternative, using a hashmap? No allocator code has to be called at all to do this, just decrement the stack pointer and zero the elements.
Rating or ranking?
Nop, it was a clever insight to see its a slice of
u8
so we can trivially allocate2kb
in the stack. If for some reason you're worried this is too much, we can always reduce tou32
and assert the slice is no bigger than that. However, modern CPUs easily have32kb
available per executionwhy not ;)
Spongebob
Stack allocations are faster than heap allocations
@docgunthrop Thanks!
@kazk: Thanks, the kata has been updated with the code from your most recent fork. Example tests, description, and the initial test setup have also been updated.
Sure I'm fine with that.
Regarding the inefficient RNG functions, I'm aware of the shortcomings, but it seemed to have very little impact on real-world performance with respect to the test constraints of the kata. I preferred to keep the RNG functions segregated from the non-RNG code and the runtime cost seemed negligible.
Yes, it's already used elsewhere in the
exec_rng_tests
function. I may have had some non-obvious reason for using the exclusive range in the RNG code but nothing comes to mind ATM.Thanks, I forked your solution to suggest few things: https://www.codewars.com/kumite/606cd9869bd117ad047aabf3?sel=606cdd479bd117003392c52e
explorers
and in the returned pairsI'd like you to have your future Rust translations reviewed by Rust users (we have few very active ones). You're the author of the kata, so it's not a requirement enforced by the system, but this is one of the main reasons why we have unidiomatic translations.
I didn't include in the fork, but the following is inefficient:
These functions creates thread local generator for each call and these are called many times. Also,
gen_range
can take inclusive rangex..=y
. I'd inline these, or at least change them to take the generator.@kazk: Updated the type signatures for the Rust translation as shown below. If you encounter any issues, feel free to let me know.
not sure how I overlooked that 😅
Go's type system is not expressive enough, so we don't have a choice. The standard library uses
-1
when some index is not found, so[2]int{-1, -1}
is not that awkward. C# doesn't have sum types either.For Rust, I think it's actually a distraction because it's awkward. We never use
-1
like this. I also tried replacing(-1, -1)
withNone
in your solution, and I don't see howOption
can be a barrier. It's cleaner withNone
since there's no need tolet null: (i32, i32) = (-1, -1);
,if v != null
,vec![(-1,-1); 4]
, etc.Thanks, please update.
I'd also suggest using a slice (
&[T]
) instead ofVec<T>
(as far as I can see, it's unnecessary to passVec<T>
), so:u32
can beu8
(since6 <= n <= 40
) orusize
(can be more convenient).The kata expects a tuple
[-1,-1]
for C# and Go translations.Yes, as was using
[-1,-1]
instead of having to deal withOption
s. It was intended to remove some barrier so the user can focus on the core task. Same reason for C# and Go mentioned earlier.If the request to change the expected output still stands, I don't mind making the update.
Loading more items...