Git Product home page Git Product logo

array_rearrange_challenge's Introduction

Array Re-arrange Challenge

Description

Write a function that accepts two arguments, arr and offset:

  • arr and offset are both arrays containing N elements where N is greater than 0
  • offset contains only integer values

The function should return an array where each non nil value in arr is shifted by the amount given at the same index in offset.

arr = [1, nil, nil]
offset = [1, 0, 0]
# shift arr[0] to arr[1]
# ignore offset[1] since arr[1] was initially nil
# ignore offset[2] since arr[2] is nil
result = [nil, 1, nil]

Two values cannot be shifted into the same index nor can a value be shifted to an index that is already occupied by a non nil value that will not be moved.

arr = [1, nil, 2]
offset = [1, 0, -1]
# offset[0] and offset[2] cancel out because they would move items to the same index
result = [1, nil, 2]

arr = [1, 2, nil]
offset = [1, -1, 0]
# offset[0] and offset[1] are allowed to swap since both items will move
result = [2, 1, nil]

See the test cases in spec/rearrange_spec.rb for a more comprehensive list of examples.

Running the tests

bundle
rspec -fd

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.