Code Monkey home page Code Monkey logo

teepubliccodechallenge's Introduction

TeePublic Coding Challenge

To solve this problem, I proccessed the json file of products into a Hash Map where the keys are possible input combinations, and the values are themselves Hash Maps whose keys are the remaining options that point to Sets that hold the corresponding option values.

e.g

{
  "tshirt" => {
              "gender"=> <Set: {"male", "female"}>, 
              "color"=> <Set: {"red", "green", "navy", "white", "black"}>, 
              "size"=> <Set: {"small", "medium", "large", "extra-large", "2x-large"}>
              },
  "tshirt+male" => {
                "color"=> <Set: {"red", "green", "navy", "white", "black"}>, 
                "size"=> <Set: {"small", "medium", "large", "extra-large", "2x-large"}>
                }, ...
}

Obtaining the remaining options for a given input is then just an O(1) Hash Map lookup.

How to view

The program output can be viewed in the terminal

How to Run

The cmd app can be run in the terminal from the root directory using:

  • ruby lib/product_parser.rb product_type option1 option2 ... OR
  • lib/product_parser.rb product_type option1 option2

Testing

I used Rspec for testing. I've inlcuded several example inputs and a few edges cases, as well as printing out the actual output of each test case.

Tests can be run by navigating to the root directory and typing:
- bundle exec rspec spec/product_parser_spec.rb

Making sure to run bundle install first

Time Complexity Analysis

Proccessing the json file is a one time operation after which extracting the remaining options for a given input combination is O(1)

If 'n' is the number of products in the list, and 'O' is the product type with the maximum number of option categories, the processing takes O(n * 2^O) time.

The 2^O comes from the possible combinations of option inputs for a given product type. For each product, the number of combinations is:
equation
which has an upper bound of 2^O

Depending on the real-world constraints, the number of option categories could be considered constant. For example, in the data set given, O = 3.

Adding a product to the Hash Map is O(2^O)

If the number of product option categories for a single product type exceeds ~ 20 (which seems unlikely), the processing could begin to take a while, and a better solution may be required. Even so, the processing of the JSON file is a one time operation after which the Hash Map is free to be accessed in O(1) time to return the remaining options.

teepubliccodechallenge's People

Contributors

ccoombsesmail avatar

Watchers

James Cloos avatar  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.