Git Product home page Git Product logo

nearest-neighbour's Introduction

Nearest Neighbour

A simple nearest neighbour solution in TypeScript.

Table of contents

Problem statement

Write a program which:

  • reads the description of the bitmap from the standard input
  • for each pixel, computes the distance to the nearest white
  • writes the results to the standard output

Considerations

A rectangular bitmap with only black and white pixels. Atleast ONE pixel is white. All black pixels are represented by 0 and whites by 1.

  • Size: n * m
  • Size constraints:: 1<=n <=182, 1<=m<=182
  • Pixel: p(i,j)
  • Distance formula: d(p1,p2) = |i1-i1|+|j1-j2|

Other considerations

Since the problem does not explicitly mention how to find the nearest neighbour, this program addresses two solutions based on the example output provided.

Solution 1

The first solution scans within the same row and tries to find the closest white pixel, like so:

solution one

Solution 2

The second solution scans in all four directions and tries to find the closest white pixel, like so:

solution two

Commands

  • yarn dev:one - Run the first solution in dev mode
  • yarn dev:two - Run the second solution in dev mode
  • yarn build - Build both solutions
  • yarn start:one < path/to/file.txt - Run the production build using solution one
  • yarn start:two < path/to/file.txt - Run the production build using solution two
  • yarn lint - Lint all files and check for errors
  • yarn test - Run all test suites
  • yarn test:generate - Generate a random new input file in the generated folder

Inputs

All the various input files are located tests/inputs folder. You can optionally generate a random file and run it against any solution

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.