Giter Club home page Giter Club logo

blob's Introduction

Blob Boundary

I provided three solutions to find the top, bottom, left, and right boundaries. Each solution tracks the number of cells read. They also contain a store of all cells visited to ensure no cell is read twice. I am aware there is no practical optimization with the store, but am simply minimizing the number of matrix cells visited as the instructions suggest. All code is in javascript.

The constructors take an argument of an array of 10 arrays. Each of the 10 arrays has length 10. I assume all data is valid and do not include validations or ways to handle error messages.

Run solution 1 with node solution1Runner.js

Run solution 2 with node solution2Runner.js

Run solution 3 with node solution3Runner.js

Solution 1 and 2:

Solutions one and two make no assumptions about the shape of the blob. Instead, they individually seek out the upper boundary, left boundary, right boundary, and lower boundary. The only difference is that solution one traverses the column outside the current boundary, while solution two traverses the current boundary, only checking outside the boundary when the current boundary is filled. Solution two relies on the 'savings' in the store - this is necessary given by definition if the boundary is a certain column some of those cells have already been visited and to double count all of them would be inefficient.

Solution 3:

Solution three is the optimal solution given an understanding of the typical shape of a blob. This solution traverses the outside of the blob accounting for its circular nature. The assumption is that a blob would not have any filled cell where the only connection to the rest of the blob is the cell it came from. Said another way, there would be no lone cell jutting out from the rest of the blob. If that is not a reasonable assumption I would use solution one or two instead. Matrix2 on the Solution3Runner.js page is an example of where this algorithm would not work. The reason why is that once we have visited a cell we do not want to visit it again, and if there is a jut we would have to allow for re-visiting the same cell thus making the algorithm less efficient.

This solution has one search function that redirects to different methods to determine which cell to visit next. This is based on previous direction which the search function takes as an argument. We start at the top of the blob and generally travel down and left first. Once we are starting to go right we hit the bottom of the circle and eventually move in the direction of up and right. I incorporate a test of whether we are in between the two side bounds in this solution so we know we have visited the edges of the circle and are now generally heading up. This part is unecessary since we found the upper bound first. The goal is to always hit the boundaries, and thus I am ordering the steps in each method with that in mind. Based on the example matrix, cell count would be 46.

I note that if the goal was to optimize this specific example matrix solution I could change the order of one function, and have the next search be right if previous direction is right, which would result in a cell count of 31. However, I did not write the solution with this specific example in mind. If I make that change I would be simultaneously allowing for many false positives where the bottom boundary would be inaccurate.

This could always be futher optimized by adding more conditionals. For example, when previous direction is right we could check if above the lower boundary. If so that means we have started to move up and thus could check up and right before down. Ultimately, I decided adding too many of these conditionals would harm elegance and clarity and thus would not be worth the incremental gains.

Time Spent

I spent ~4 hours on this total including brainstorming, starting with simple iteration, and then optimizing and writing the README. If I was to spend more time I would have created simple unit tests.

blob's People

Contributors

craigschiff avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.