Code Monkey home page Code Monkey logo

count_islands's Introduction

count_islands

This is a repository for the recruitment task for Silent Eight Pte. Ltd.

How to use

Environment requirements

Python 3.7.3. The installation of the following modules is required for the script to run:

numpy==1.21.5
scipy==1.7.3

additionaly, to run the comparison.py file:

matplotlib==3.5.1

(this information is also contained in the requirements.txt file)

Example invocation (and output)

$ sh count_islands.sh tests/test_arrays/proper_islands.txt
5

In such case unit tests results are also outputed to STDERR.

Possible error messages

When the file path is not correct:

FileNotFoundError("Error: Invalid file path.")

When the file has invalid extention:

IOError("Error: Invalid file extention.")

When the input array has wrong shape:

ValueError

When the input array has invalid characters:

ValueError

The method chosen to count the islands

The first idea on how to count the number of islands in a inputed 2d array was to count the number of connected components in an undirected graph using the DFS (Deep first search) algorithm. Because in the task we are asked to assume that the size of the data given to us could be very large, and the DFS algorithm has a running time complexity of O(m*n), where m and n are rows and columns of the array, the second, faster idea arose - to use the scipy's ndimage image-processing library. By .label()'ing the islands in the array and coding given neighbouring-land-fields-to-count-as-island rules in the structure parameter, we can solve the same problem in linear time.

Below the plot of the comparison of running times for both methods for different data sizes. Running time comparison

So the latter has been chosen for the script. Additionally, as the Scipy method does not use recurrence, the stack overflow error handling can be omitted.

The comparison.py file creates the plot.

count_islands's People

Contributors

dahandaniel avatar

Watchers

 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.