GithubHelp home page GithubHelp logo

data-structures-1-ll's Introduction

Introduction to Data Structures

Problem

  1. Create an array containing 20000 integers.
  2. Use a loop to remove the 2nd element of the array until the array only contains 1 element.
  3. Time the operation in step 2. The benchmark gem should be available on default installations of Ruby and can be used with the following syntax:
Benchmark.bm do |bm|
    puts "Title of test"
    bm.report do
        # Insert your test code here
    end
end

Optimization - Using Another Data Structure

  1. Create 2 classes - Emerald and Diamond

  2. Each instance of Emerald should hold a value (e.g. an integer), as well as variables pointing to two other Emeralds, e.g. prev and next. An Emerald should be initialized with at least 1 parameter - its value. Its two other variables can take default values of nil.

  3. Each instance of Diamond should hold just one value for now - an Emerald. A Diamond should be initialized with at least 1 parameter - an Emerald.

Now, write methods for either Diamond or Emerald such that...

  1. Emeralds can be added to a Diamond such that...

    • If a Diamond contains only 1 Emerald A, and another Emerald B is added to the Diamond, then one of A's variables, e.g. next, should refer to B. At the same time, one of B's variables, e.g. prev, should refer to A.
    • If a Diamond contains 2 Emeralds A and B such that A's next refers to B (and B's prev refers to A), and another Emerald C is added to Diamond, then B's next should now refer to C while C's prev should refer to B. Thus, the Diamond should look like a chain of Emeralds: A <-> B <-> C.
  2. An Emerald can be deleted from a Diamond without breaking the chain. For example, if a Diamond contains a chain of Emeralds A <-> B <-> C, then deleting B should result in the chain becoming A <-> C. That is, A's next should now point to C instead of B, and C's prev should now point to A instead of B.

  3. Finally, create an Emerald with a value of 1, and add it to a Diamond. With the same Diamond, add 19999 other Emeralds, each containing an integer value.

  4. Write a loop that removes the 2nd Emerald in the Diamond until only the first Emerald is left.

  5. Time the operation in step 7, and compare it to the time that the same operation required when an array was used.

What is this data structure formally called, and what are its advantages and disadvantages over an array or hash?

Further

Implement methods in either the Diamond or Emerald classes such that...

  1. You can search for the first Emerald in a Diamond that contains a given value.
  2. You can add a new Emerald after a given Emerald in a Diamond, i.e. somewhere in the middle of the chain.
  3. You can search for the last Emerald in a Diamond that contains a given value.
  4. The last Emerald in a Diamond's chain always refers to the first, and the first Emerald in turn always refers to the last.
  5. Write this exercise in Javascript. Note: You'll want to use either prototypes or classes in Javascript if you decide to do this.

data-structures-1-ll's People

Contributors

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