jie-think / closure-detect

Ah, A Quest!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Ah, A Quest!


Languages

Language:JavaScript 98.2%Language:Makefile 1.8%