Giter Club home page Giter Club logo

closure-detect's Introduction

Detecting Closures In A Bit Map

Objectives

This is a challenge (a interview question) repo. If you are considering joining WeFlex, you can fork this repo, complete the quest and submit a PullRequst back to me.

Languages Available

At this moment, this quest is available in these programming languages:

The Quest

You are working on a project dealing with ASCII texts. Each of these texts is an graphical image (Glyph), drew with ASCII characters. Characters, including white spaces, are known as Pixels, as they are very similar to pixels on LCD panels. Below is an example of an image produced by anonymous artist. It is 15 chars wide and 6 chars high.

            
  /|_.      
 /    `-.   
/   .__/    
\___|       
            

Your task in this project is to detect if its path is closed. If it is closed, we call it a Closure.

Is it a closure?

A rectangle is a closure.

+-----+
|     |
|     |
+-----+
      

A triangle is a closure.

   +
  / \
 /   \
+-----+
       

But a short line is not a closure.

  
++ 
 

Helpers

We have already wrote a helper function to read and generate data structures you may need.

Function readGlyph(fpath) reads contents of file fpath and returns an object with width, height of the glyph and a function charAt you can use to get content of a glyph at given position x, y.

var fs = require('fs');

function readGlyph (fpath) {

  var chars = fs.readFileSync(fpath);
  var glyph = {
    w:   0,
    h:   0,
    x:   0,
    y:   0,
    buf: [[]]
  };

  chars.forEach(function (ch, pos) {
    if (0x0a === ch) {
      glyph.h += 1;
      glyph.y = glyph.h - 1;
      glyph.w = (glyph.x > glyph.w) ? glyph.x : glyph.w;
      glyph.x = 0;
      glyph.buf.push([]);
      return;
    }
    glyph.buf[glyph.y][glyph.x] = ch;
    glyph.x++;
  });

  return {
    width: glyph.w,
    height: glyph.h,
    charAt: function (x, y) {
      if (x < glyph.w && y < glyph.h) {
        return glyph.buf[y][x] || 0x20;
      } else {
        throw 'Request exceeds size of glyph';
      }
    }
  };
}

module.exports = readGlyph;

Your task is to modify the function body of isClosure() in lib/is-closure.js, make it return true when the input glyph object is a Closure.

function isClosure (glyph) {
  // TODO: 
  return false;
}

module.exports = isClosure;

Running Test Cases

To help you figure out if you had walking on the right path, we have also made a few test cases. To test your code, do

make test

or you can test it with node directly

node test/test.js

closure-detect's People

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.