Coder Social home page Coder Social logo

json_recursion's Introduction

Recursive Aggregation

Build a recursive aggregation

Given a list of inputs, e.g. the total amount of sales by an organizations store-front locations, calculate the sum of all sales according to a key-list. The key-list may contain any of the object properties (except for amount) and may contain each key at most once. The result should be an object structured according to the key-list containing the sum of the amount property of all objects falling into the given category. Some examples of key lists are:

  • ["currency", "city"]
  • ["country", "city", "currency"]
  • ["city"]

Notes:

  • ask any question you might have, conceptually, or related to code (i'm your peer in this excercise)
  • do not focus on json file-reading or command line interfaces (hard-code the necessary overhead)
  • google is your friend - the goal is simulate a realistic work-day experience

Example

This input file:

[
  {
    "country": "US",
    "city": "Boston",
    "currency": "USD",
    "amount": 100
  },
  {
    "country": "FR",
    "city": "Paris",
    "currency": "EUR",
    "amount": 20
  },
  {
    "country": "FR",
    "city": "Lyon",
    "currency": "EUR",
    "amount": 11.4
  },
  {
    "country": "ES",
    "city": "Madrid",
    "currency": "EUR",
    "amount": 8.9
  },
  {
    "country": "UK",
    "city": "London",
    "currency": "GBP",
    "amount": 12.2
  },
  {
    "country": "UK",
    "city": "London",
    "currency": "FBP",
    "amount": 10.9
  }
]

We should be able to call:

   # pseudo
   aggregate(input_data, ["currency", "country", "city"])

Which should return

{
   "EUR":{
      "ES":{
         "Madrid":{
            "amount":8.9
         }
      },
      "FR":{
         "Lyon":{
            "amount":11.4
         },
         "Paris":{
            "amount":20
         }
      }
   },
   "FBP":{
      "UK":{
         "London":{
            "amount":10.9
         }
      }
   },
   "GBP":{
      "UK":{
         "London":{
            "amount":12.2
         }
      }
   },
   "USD":{
      "US":{
         "Boston":{
            "amount":100
         }
      }
   }
}

We should also be able to call

   # pseudo
   aggregate(input_data, ["city"])

Which should return

{
   "Madrid":{
     "amount": 8.9
   },
   "Lyon":{
     "amount": 11.4
   },
   "Paris":{
     "amount": 20
   },
   "London":{
     "amount": 23.1
   },
   "Boston":{
     "amount": 100
   },
}

As visible, the result data should be nested according to the number of parameters provided.

Additional Tasks

  1. Change the code to have an additional parameter defining which property gets summed up: aggregate(input_data, ["currency", "country"], "amount")
  2. Change the code to allow an arbitrary aggregation function: e.g. aggregate(input_data, ["currency", "country"], "amount", "avg|sum|min")
  3. Write a unit test for your function

json_recursion's People

Contributors

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