Skip to content

Instantly share code, notes, and snippets.

@11xor6
Last active August 29, 2015 14:01
Show Gist options
  • Save 11xor6/c6eb0f6448f83492807e to your computer and use it in GitHub Desktop.
Save 11xor6/c6eb0f6448f83492807e to your computer and use it in GitHub Desktop.
BraveNewTalent -- Lead Engineer Technical Test -- Nik Hodgkinson
users = [{"id": 40, "name": "Joe Bloggs", "posts": 4},
{"id": 567, "name": "Jenny Smith", "posts": 3},
{"id": 3, "name": "Frank Jones", "posts": 54},
{"id": 46, "name": "Samantha Wills", "posts": 0},
{"id": 6789, "name": "Ahmed Joseph Naran", "posts": 15}]
def userTransform(user):
name = user["name"].rsplit(" ", 1)
return {"first_name": name[0], "last_name": name[1], "posts": user["posts"]}
filtered_users = filter(lambda entry: entry["posts"] != 0, users)
sorted_users = sorted(filtered_users, key=lambda entry: entry["posts"], reverse=True)
transformed = [userTransform(user) for user in sorted_users]
for user in transformed:
print str(user)
# Results:
#
# {'first_name': 'Frank', 'last_name': 'Jones', 'posts': 54}
# {'first_name': 'Ahmed Joseph', 'last_name': 'Naran', 'posts': 15}
# {'first_name': 'Joe', 'last_name': 'Bloggs', 'posts': 4}
# {'first_name': 'Jenny', 'last_name': 'Smith', 'posts': 3}
// As far as I know this should work in all modern browsers >= IE7. As there is
// no good way to print something out in JavaScrpt I've collected the output and
// returned it from the function.
var x = [
{ first_name: "Frank", last_name: "Jones", posts: 54 },
{ first_name: "Ahmed Joseph", last_name: "Naran", posts: 15},
{ first_name: "Joe", last_name: "Bloggs", posts: 4},
{ first_name: "Jenny", last_name: "Smith", posts: 3 }
];
function reformat(x) {
var output = "";
for (var i = 0; i < x.length; i++) {
posts = (x[i].posts > 1) ? " posts" : "post";
output += x[i].last_name + ", " + x[i].first_name + " - " + x[i].posts + posts + "\n";
}
return output;
}
// Results:
//
// Jones, Frank - 54 posts
// Naran, Ahmed Joseph - 15 posts
// Bloggs, Joe - 4 posts
// Smith, Jenny - 3 posts
function unsubscribe(req) {
var email = req.query.email || req.body.email || undefined,
defer = when.defer();
if (typeof email == 'undefined') {
defer.reject({ error : 'No email was specified.' });
} else {
when(client.request('nurture/unsubscribe', { email: email }, { method: 'POST' })).then(
function unsubscribed(response) {
defer.resolve(response);
},
function failed(response) {
defer.reject(response);
}
}
return defer.promise;
}
/*
This function is designed to take an incoming unsubscribe request and proxy that request into a
call on another service. This happens asynchronously and we don't want to block, thus we utilize
a promise as a placeholder. A promise is just as it sounds, it's a declaration or IOU that a
value (or error) will be available at a later time when work that has to be done completes.
To step through the function: we start by retrieving the email address from the request and create
a deferred value promise (lines 2&3). If we don't have an email address we immediately set the
promise to signal an error (lines 4&5), otherwise we make an async call to our external
service (line 7) tying the result (both failure and success) to our promise (lines 8-12).
To tie our async request to the promise we utilize callbacks (code passed as an argument; lines 8 & 11)
and within create a closure (a reference to a non-local variable; lines 9&12). Finally after
having completed the setup, we return the promise (line 16). The calling code that receives o
ur promise may then try to retrieve the value immediately (which could block) or it could save
it for later and only handle it when the promise is ready.
*/
SELECT
ct._name AS topic_name,
count(ci.id) AS topic_count
FROM content_topic ct
LEFT JOIN content_item_topic cit ON (ct.id = cit.topic_id)
LEFT JOIN content_item ci ON (ci.id = cit.item_id)
WHERE ci.created >= (localtimestamp - interval '1 month')
GROUP BY ct.name
ORDER BY topic_count DESC;
server {
server_name local.bnt;
rewrite / $scheme://www.local.bnt$request_uri? permanent;
}
server {
server_name www.local.bnt www.local.test;
set $srv_base "/srv/public.bravenewtalent.com/";
access_log /var/log/nginx/www.local.bnt.access.log main;
error_log /var/log/nginx/www.local.bnt.error.log;
sendfile off;
ssi on;
root $srv_base/www;
index index.html;
location /api/0/content {
proxy_pass http://127.0.0.1:10200;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home {
proxy_pass http://127.0.0.1:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# As a side note, it is not specified exactly how the reverse proxied service should be called.
# Currently it passes the entire path, but could be configured differently. Additionally the
# proxy config settings should be moved to another file and included to avoid duplicating config.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment