Giter Site home page Giter Site logo

42codechallenge's Introduction

42CodeChallenge

More information here

Solution:

The solution has been divided into 4 parts:

  • Reading the file Reading from the input file, given as argument, line by line
  • Building Global Property Map and creating row dict The Global PROPERTY_MAP is being build to define the group within that property. For the given example in the challenge the property map looks like:
    {
    	'$total': {
    		'$total': {
    			'net_sales': '200'
    		}
    	},
    	'foo': {
    		'$total': {
    			'net_sales': '400'
    		},
    		'bacon': {
    			'net_sales': '100'
    		},
    		'sauce': {
    			'net_sales': '300'
    		}
    	},
    	'bar': {
    		'bro': {
    			'net_sales': '200'
    		},
    		'$total': {
    			'net_sales': '-200'
    		},
    		'sup': {
    			'net_sales': '-400'
    		}
    	}
    }
    Similary build the dict of each row where the key is property/metric name and value is the value in that row.
    {'property0': 'bar', 'property1': '$total', 'net_sales': '-200'}
  • Sorting using the Global Property Map Defined custom compare function that returns an integer denoting whether first argument is bigger, smaller or equal to the second argument. Now for given two rows the compare function will compare the property values - if they belong to the same group and any one of the property value is $total then returing -1 to denote its smaller than the other argument/row. - if belong to same group, traverse the PROPERTY_MAP till reach the metric to compare and sort based on that metric value - otherwise if don't belong to the same group, compare the $total value of the metric to determine the order.
  • Writing to the file Writing to the output file, which is given as argument, piping the row dict

To Run

python index.py --input_file <input_file_location> --output_file <output_file_location> --metric <metric_to_compare>

Example

python index.py --input_file data.txt --output_file output.txt --metric net_sales

42codechallenge's People

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.