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.
Two pointer approach: It would be a lot faster if the array was sorted to begin with, but the solution still performs much better than a brute force approach using a nested for loop. I ran a script with various implementations of this function in Python (including using a HashMap / dictionary) implementation which came back with the following results:
Two Sum Performance Comparison
Small Arrays (Size: 500, Cases: 10)
Medium Arrays (Size: 2500, Cases: 10)
Large Arrays (Size: 15000, Cases: 5)
Extra Large Arrays (Size: 50000, Cases: 3)
Note: Two Pointer + Sort is the solution I came up with above. Clearly in this case, a Hash Map implementation is the best choice. Even with a list containing 50,000 elements, the HashMap is able to conjure up a solution in less than 5ms on average. The divergence between the HashMap and two pointer solutions begins to grow larger as the sorting method (which for practical purposes has a time complexity of O(n log(n)), has to deal with increasingly large arrays.
The use of the ternary operator to define the base case (return s.toFixed(2) when n is 0) is quite clever. Not the most readable solution, but definitely a clever one!
In retrospect, toFixed would have been the correct method to use as it only deals with decimals, not the number of digits in the number.
My goal for this one was to keep the code clean and modular, without using the filter function.
I didn't think to define templates like this. Clear, clean code. Great!
I'm glad you already got it, because now I'm thoroughly confused.
Thank you @RileyHunter
I meant what mathematical dissertation/application that would represent:
L = [a₀, a₁, a₂, ..., aₙ₋₁]
Now I got it and solved it.
@Spaceman perhaps a different analogy would help - think of the boundaries of the window as existing "between" the indices of the array elements. So for the array
we have valid boundaries like so:
Then, to construct a window of a given length we place our left boundary, move forward
length
steps and place the right boundary. To construct the next window we move the left boundary forward byoffset
.So for
length = 2, offset = 1
we do:and for
length = 1
we do:and it then follows that for the empty window, we do:
Even when the set of remaining indices is empty, the empty set is still a valid subset because the empty set is a subset of all sets (including itself).
I don't understand what you're asking.
That's a weird set of thought. Mathematically, what is this equivalent to?
OK, approved then. Thank you!
Hi, the fork looks good to me.
You are putting the window on
[2,3]
, then on[3]
, then on[]
( because offset1
).So no. Because you can take
0
elements from an empty list.Encoding the list as a cons-list maybe makes this easier to visualise: in a list of length
2
, there are3
constructors (Cons 2 (Cons 3 Nil)
). An array doesn't have constructors, it's just mapped memory, but even then there's one more value: maybe its length, maybe a trailingNUL
, or something.I'm trying to understand why if the function is passed a window length of 0, and an offset of 1, the output is expected to be an array of n+1 elements, where n is the length of the array. So for window(0,1, [2, 3]), the expected output would be [[], [], []].
Shouldn't the output correspond to the number of elements in the array?
Loading more items...