Last active
August 29, 2015 14:01
-
-
Save yoaquim/d1461cde4fb9cf93a26b to your computer and use it in GitHub Desktop.
Programming Excercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Exercise: | |
Write a web based math engine. It should take the form of a running web service | |
that takes in a single parameter GET or POST* called “values”, which is a | |
JSON-formatted list of numbers. The response should be a JSON dictionary with | |
two values, the “sum” and the “product”. | |
Example input: “values” = [1, 4, 7, -2] (will be either a url param for a GET | |
or a post param for POST) | |
Example output: {“sum”: 10, “product”: -56} | |
* POST is optional | |
(Extra Challenge:) | |
Write an additional endpoint to query the history with a second parameter asking | |
how far back you want to go. | |
Should return a list of JSON objects formatted like the following: | |
[{"ip": "127.0.0.1", "timestamp": 2011-06-09T17:46:21, "values": [1, 4, 7, -2], "sum": 10, "product": -56}, | |
{"ip": "127.0.0.1", "timestamp": 2011-06-09T17:25:23, "values": [1, 4, 7, -2], "sum": 10, "product": -56}, | |
{"ip": ...}, | |
{"ip": ...}, ...] | |
*/ | |
/* | |
* Instructions on the second exercise were a bit | |
* fuzzy, but from what I understood: | |
* | |
* '/history' endpoint recieves a JSON which | |
* contains a javascript Date object (instructions | |
* weren't clear as to what type of parameter was | |
* sent), wich is used to filter history up to, and | |
* including, that point. | |
*/ | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
var history = new Array(); | |
app.use(bodyParser()); | |
app.get('/mathengine', function(request, response){ | |
response.send(200, sumProductJSON(request)); | |
}); | |
app.post('/mathengine', function(request, response){ | |
response.send(200, sumProductJSON(request)); | |
}); | |
app.get('/history', function(request, response){ | |
response.send(200, filterHistory(request)); | |
}); | |
app.post('/history', function(request, response){ | |
response.send(200, filterHistory(request)); | |
}); | |
app.listen(80, function(){ | |
console.log("Started on port 80..."); | |
}); | |
function sumProductJSON(request){ | |
var values = request.query.values; | |
if(!values){ | |
values = request.body.values; | |
} | |
var sum=0; | |
var product=1; | |
for(var i=0; i<values.length;i++){ | |
sum+=parseInt(values[i]); | |
product*=values[i]; | |
} | |
addToHistory(values, sum, product, request.connection.remoteAddress); | |
return ({ "sum":sum, "product":product}); | |
} | |
function addToHistory(values, sum, product, address){ | |
var record = { | |
"ip":address, | |
"timestamp": new Date().toISOString(), | |
"values":values, | |
"sum" : sum, | |
"product" : product | |
}; | |
history.push(record); | |
/* | |
* For the sake of simplicity and proof-of-concept, I used an | |
* array. In practice, something like Redis can be used, and | |
* just stringify the JSON object before storing it: | |
* | |
* var redis = require('redis').createClient(); | |
* redis.lpush('history', JSON.stringify(record)); | |
*/ | |
} | |
function filterHistory(request){ | |
history.reverse(); | |
var date = request.query.date; | |
if(!date){ | |
date = request.body.date; | |
} | |
var sorted = new Array(); | |
for(var i=0;i<history.length;i++){ | |
var bench = new Date(date); | |
var current = new Date(history[i].timestamp); | |
if(bench > current){ | |
break; | |
} | |
sorted.push(history[i]); | |
} | |
return sorted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment