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.
Each boundary of two lands removes 2 perimeter vs if the land were isolated islands, so we add up perimeter pre-emptively "paying the cost" of the next row/column, and assuming no cost from previous row/column. Adds an empty row at the end to make
each_cons
comparison cleaner.Please never do this in production XD
you can destructure the array in the map block in place, so that you don't have to refer to
[0]
and[1]
arbitrarily without much understanding of what they mean.I daresay this is the solution. It is the most succinct and clear transformation of one data shape to another.
this is basically the meat of the cleanest solution without optimization for space or anything. I like this solution a lot.
No need for the
return
s in the operator methods.This comment is hidden because it contains spoiler information about the solution
instead of inject, you could use
#sum
which also more elegantly handles the 0 case, meaning you can skip the|| 0
checkYou're iterating many times over the same data. I assume this is because you're trying not to make any assumptions about the shape of the data, but the spec did say the numbers would always be in order.
I wonder if you could get the same shortcutting logic you have without manual loops by using
next
this iterates the list, searching, for each list element, making it O(n^2). Since the list is guaranteed to be sorted, you don't have to search, since the consecutive numbers will occur consecutively in the list.
very interesting, the idea of reducing for the accumulator, but not caring about the return value. I think you might be able to make a use case for how this is idomatic to the specification, but I marked clever, because I think the meaning is a little lost in the code.
what I like about this solution is that if they kept expanding the pattern, it would literally be a matter of adding one to that array.
In that case (and this one too) I'd use a range, i.e.
(0..2)
which can be mapped, as it's enumerable.sure enough. I hadn't even noticed, since I jumped to the math solution. Given the size of that test, I don't think it's possible to write code to find the answer if the order is arbitrary (that's the main difference between your commented code and the math approach)
you know... You can submit multiple solutions instead of leaving it as a comment 😂
without giving it all away, you can learn a lot about the math of this by substituting i in in each of the equations, to make them in terms solely of n.
Loading more items...