Giter VIP home page Giter VIP logo

fill-the-grid's Introduction

Fill the grid

  • There is a 2D grid of any given dimension, you need to fill it in with blocks that user input.
  • The user can input 2D blocks of any dimensions
  • The input blocks need to be placed from left to right and top to bottom.
  • Note: The user does not provide positions for inputs, but only the dimensions, the program needs to place it appropriately by avoiding overlapping
    • Play with DEMO to understand the placement logic.

Specifications

  • Grid should initialize with the desired dimension, rejecting invalid dimensions like 0 or negative numbers.
  fillTheGrid.init(3, 3);
  expect(fillTheGrid.grid).toEqual([
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
  ]);
  • Placing any input larger than the grid itself should be rejected with an error message.
    • On width
    • On height
  fillTheGrid.init(3, 3);
  expect(() => fillTheGrid.place({ w: 4, h: 2 })).toThrow(
    "Not enough space in the grid"
  );
  • It should not allow invalid dimensions like 0 or negative numbers for inputs.
  • Placing any input smaller than the grid itself should fit, and the grid should be updated accordingly.
  fillTheGrid.init(3, 3);
  expect(fillTheGrid.place({ w: 1, h: 1 })).toEqual([
    [1, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
  ]);
  • Placing additional input when the grid is full should reject the input and throw an error.
  fillTheGrid.init(2, 2);
  expect(fillTheGrid.place({ w: 2, h: 2 })).toEqual([
    [1, 1],
    [1, 1],
  ]);
  expect(() => fillTheGrid.place({ w: 1, h: 1 })).toThrow(
    "Not enough space in the grid"
  );
  • Placing multiple inputs should fit in the grid as long as there is space for the inputs.
  fillTheGrid.init(3, 3);

  expect(fillTheGrid.place({ w: 1, h: 1 })).toEqual([
    [1, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
  ]);

  expect(fillTheGrid.place({ w: 2, h: 1 })).toEqual([
    [1, 1, 1],
    [0, 0, 0],
    [0, 0, 0],
  ]);

  expect(fillTheGrid.place({ w: 2, h: 2 })).toEqual([
    [1, 1, 1],
    [1, 1, 0],
    [1, 1, 0],
  ]);

  expect(fillTheGrid.place({ w: 1, h: 1 })).toEqual([
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 0],
  ]);

  expect(fillTheGrid.place({ w: 1, h: 1 })).toEqual([
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 1],
  ]);

  expect(() => fillTheGrid.place({ w: 4, h: 2 })).toThrow(
    "Not enough space in the grid"
  );

ChatGPT's recommendations

  • Scalability and performance: Consider how your implementation will perform for large grids or a large number of block placements. Are there any optimizations or considerations you need to take into account for scalability?

  • Clear documentation: Provide clear documentation for the functions and their parameters, including any assumptions or limitations.

  • Optional features: Consider if there are any optional features you might want to add

fill-the-grid's People

Contributors

dejavu1987 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.