OpenAI bot vs. Dendi – The International 2017 Dota2

Dendi is one of the most famous Dota2 players, because of his abilities in playing with Pudge. Last night, as a part of The International 2017, Dendi played with an OpenAI bot.

The funny part is, he lost the 1 vs. 1 two times and then he said, “I give up!”. Apparently, the bot was so accurate.


Blocking creeps

When it comes to blocking the creeps. the bot played much better than Dendi. This is the first game when the bot blocks creeps.

In the second game, there were two lines of creeps but the bot managed to block both lines very nicely.


First blood

In the second game, the bot drew first blood in a very complex situation. Under the tower with half hp.

And the nice part of this fight is, the bot used his 3rd skill (not first or second skill) to accurately hit the target.



This is so amazing that Dota2 introduces AI this year. Probably, something more important is going on behind the scene. We have to wait and see how AI and this relationship with OpenAI would affect the experience of Dota2 in the future.

This is the official blog post from OpenAI team that explain the process of training the bot and techniques that the bot can utilize to defeat the opponent:

Character density using Haskell

In this short tutorial, I’m going to write a simple Haskell program to calculate the density of characters in a given text. For instance, in “hello” we have [('e',0.2),('h',0.2),('l',0.4),('o',0.2)].

The return format of our application would be a list of tuples

String -> [(Char, Float)]

To make the problem easier to solve, I’m going to break it into two programs. One that returns the number of repeats for each character and another program that converts that list to a percentage of repeats.

Count of repeats

freq_letter :: String -> [(Char, Int)]
freq_letter str = [ (x,c) | x <- ['a'..'z'], let c = (length.filter (==x)) str, c>0 ]

This program returns a list of tuples with the Char and Int. First item of tuple is the character in the given text and the second parameter, Int, is the count of repeats in the text. Executing this program will return:

*Main> freq_letter "hello"

Percentage of repeats

freq_letter_pc :: String -> [(Char, Float)]
freq_letter_pc str = [ (char, fromIntegral(count) / fromIntegral(length str)) | x <- freq_letter (map toLower str), let (char,count) = x]

Second part of the application uses the previous program to get the list of repeats and then divide them by the length of the given text. This way we can calculate the density of characters in the text. Executing this program returns:

*Main> freq_letter_pc "hello"

That's it.


In this tutorial, we have learnt how to calculate the density of characters using Haskell. Achieving this goal is much easier in Haskell because of list comprehension and higher order functions.

How to fix "Library not loaded: libmysqlclient.18.dylib" in Mac OS X

Well, sometimes a blog simple tip can save a day. That’s why I created this blog.

In this post, I’m going to show a simple way to fix Library not loaded: libmysqlclient.18.dylib error. It could happen when you upgrade your MySQL to the latest version (here it’s 5.7.10).

Just run following command:

sudo ln -s /usr/local/Cellar/mysql/5.7.10/lib/libmysqlclient.dylib /usr/local/lib/libmysqlclient.18.dylib


sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

The reason for that error is that your client (let’s say Python Django) is unable to find your libmysqlclient library. That way you can link that library again. everything else should work fine.

Source: New feed

Optimized solution to calculate first powers of 2

I experienced many programming contests and olympiads but this one was really different. In a programming contest on Hacker Rank, I faced with a challenging problem and it was the main reason I failed the test. That question made me nervous and stressful so I gave up. When I found the solution after the test, I laughed so loud.

The question is:

Calculate sum of first powers of 2 without using any built-in language API to calculate the power. Implement optimized solution.

Well, there are two factors. First, not using built-in functions like Math.pow and second implementing the optimized solution. So, of course a recursive function or for-loop is not the answer.


Use bitwise operations. How? Ok, let me show you a trick.

parseInt(10, '2')    == Math.pow(2, 1); //2^1 = 2
parseInt(100, '2')   == Math.pow(2, 2); //2^2 = 4
parseInt(1000, '2')  == Math.pow(2, 3); //2^3 = 8
parseInt(10000, '2') == Math.pow(2, 4); //2^4 = 16
//and so on

So, how to make 10, 100 or 1000? Using shift operations you can make it:

1 << 1 //2
1 << 2 //4
1 << 3 //8
1 << 4 //16

A short explanation of above source code is that the binary of 1 is 00000001 and when you use left shift operator, the result is 00000010, 00000100 and so on.

So the answer is to use a for-loop for N:

var sum = 0;
for (var i = 1;i <= n;i++) {
  sum += 1 << i;

 Can we make it better?

Yes. Here is the final hack.

The sum of first N powers of 2 is 2^(N + 1) - 2. For instance, n = 3:

(2 ^ (3 + 1)) - 2 = 16 - 2 = 2 + 4 + 8 = 14

And then, using bitwise operations we have:

console.log((2 << n) - 2);

Please note that the binary of 2 is 00000010.

Actually, this is the right answer.

Edit: You can find more interesting examples here:

Source: New feed

MongoDB Aggregation

MongoDB has a aggregate operator that help programmers to define complex type of queries and combine operators such as group, match and find to build a complete result set.

Unfortunately, this operator doesn’t have enough tutorials and examples so I decided to explain it in better words.

 Aggregate is a pipeline

First of all, you should know that aggregate is a pipeline of different operators. So, what does it mean?

The aggregate operator accepts an array of stages:

db.collection.aggregate( [ { <stage> }, ... ] )

Each stage contains an operator such as $match, $limitor$project. After executing,aggregate` operator starts to perform each stage one by one. It’s important to know that each stage get the result set from the previous stage and the result set for the first stage is all records of the collection.

Following diagram illustrates a sample aggregate procedure (source:



Let’s assume we have a users collection with following records:

  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28
}, {
  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45

 Example with two stages

Following aggregate operator has two stages, $sort and $limit:

  $sort: { age: -1 }
}, {
  $limit: 2
}], function (err, records) { 

First of all, the aggregate operator executes the first stage. The result set for the first stage is the whole collection records. $sort operator sorts the users collection by age field. The result of the first stage is:

  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35
}, {
  _id: '55329ec72d3c018764000002',
  name: 'Guillermo',
  role: 'admin',
  age: 28

Then, the second stage gets the sorted array of users from previous stage and eliminates a record. Finally, the result of aggregate operator will be:

  _id: '55329ec72d3c018764000003',
  name: 'Roshan',
  role: 'user',
  age: 45
}, {
  _id: '55329ec72d3c018764000001',
  name: 'Steve',
  role: 'admin',
  age: 35

You can find more operators for aggregation stages here:


Aggregate operator is a pipeline of stages. It accepts an array of stages and each stage has a operator. The first stage gets all records of the collection and after performing the operator, passes the result to the next stage.

Source: New feed

Scheduled backup for MongoDB

MongoDB is one of NoSQL pioneers and it has a great community. Nowadays, most of startups prefer to use MongoDB as the main database because of its simplicity. Configuring scheduled backup for a database is really important to keep the last updated data somewhere and restore it in case of database crash.

In this post I want to introduce a simple open-source tool that I’ve recently published to setup a minimal scheduled backup for MongoDB.


I built this tool using Node.js so first of all you need to install I will publish a binary version for all platforms soon.

You need no more dependencies but official Node.js module. In the next step we will install them using npm.


I named this tool mongodb-backup and you can clone the repository from Github:

Then go to the folder of project and run following command:

npm install

This command installs all required dependencies including AWS sdk.


This tool has a simple config.js file that defines all dependencies to run the mongodump command and upload it to the Amazon S3 storages. Here you can find a list of all dependencies:

 Go go go

Final step is to run the script. You can run the cronjob using following command:

node index.js start

And if you want to run the script as a deamon, install forever using npm install forever and then run this command:

forever start index.js start


This version of mongodb-backup works with Amazon S3 but I will publish another version soon that accepts FTP and SFTP to transfer backup files.

Source: New feed

Using `object` as the keys of another `object` in JavaScript

Sometimes you need to use objects for the keys of a dictionary (let’s say, another object).

Assume this code:

var keys = [{a: 1}, {a: 2}, {a: 3}];
var dic = {};

//add a new key to the dictionary
dic[keys[0]] = 'boo';
dic[keys[1]] = 'foo';

Above code yields this result:

console.log(dic); //Object {[object Object]: "foo"}

So the dictionary doesn’t have two items because object keys cannot be object. You can get the item with [object Object] string:

console.log(dic['[object Object]']); //returns `foo`

Ok, wait. Then how we can set objects as the keys of a dictionary?

 ECMAScript 6 – Map

Map feature in ES6 enables you to assign objects as the keys of a dictionary (or another object).

Here is the ES6 compatible of the previous example:

var keys = [{a: 1}, {a: 2}, {a: 3}];
var map = new Map();

//add a new key to the dictionary
map.set(keys[0], 'boo');
map.set(keys[1], 'foo');

And the map variable should be:

Map {Object {a: 1} => "boo", Object {a: 2} => "foo"}

Besides, you can get the item with get:

map.get(keys[0]); //returns `boo`

Or set a new value for one of items:

map.set([keys[0], 'new value');

Here you can read more about Map feature in ECMAScript 6:

Source: New feed

Block scope variable – ECMAScript 6

Since some of ECMAScript 6 features looks weird to old-fashioned JavaScripters, I’m going to write a series of blog posts to describe the main idea of some common ECMAScript 6 features.

In this posts I’d like to explain the usage of block scope variables. The keyword let enables you to create a block scope variable in ECMAScript 6:

let boo = 1;

So, what’s the difference between var boo = 1; and let boo = 1;? Assume following example:

function sample() {
  if (true) {
    var boo = 1;
  return boo;

console.log(sample()); //returns 1

In the above old-fashioned code, boo variable is defined within the sample function scope so we have the value even outside of the if statement.

With let variable you can create a variable that is available within the if statement block:

function sample() {
  if (true) {
    let boo = 1;
  return boo; //ReferenceError: boo is not defined


Awesome, isn’t it?

I will write more blog posts about ECMAScript 6 features soon.

Source: New feed

Source: New feed

Python: List and Tuple performance benchmark

Suppose you have two options to implement a solution using a programming language, what are important factors to select one of options? I do believe one of concerns for a programmer would be performance benchmark between those options.

In this short blog post I’d like to share my simple code and results for performance benchmark between Python list and tuple. Two features to create a list, but with this difference, that tuples are immutable and you can’t alter them after initializing.

Following code shows a simple usage of list and tuple to create a series of items:

# this is a list, you can alter it in next lines
l = [1, 2, 3, 4, 5]

# this is a tuple and it's immutable
t = (1, 2, 3, 4, 5) 

Please note that you can store different data types as an item for both tuple and list.

My scenario to make a performance benchmark between list and tuple is retrieving 2,000,000 random items from a list (or tuple) with 1,000,000 items.

Here is the source code for list:

import time
from random import randint

x = 1000000

demo_list = []

# add items to list
while x > 0:
    x = x - 1

start = time.clock()

# find random items from list
y = 2000000
while y > 0:
    item = demo_list[randint(0, 999999)]
    y = y - 1

# print the elapsed time
print (time.clock() - start)

Following chart illustrates the performance benchmark between list and tuple on a Mac OS X 10.9.3 and Python 2.7.5:

Benchmark between list and tuple

Elapsed times:

  • Tuple: 5.1217s
  • List: 5.2462s

And it seems tuples are a little bit faster in retrieving items.

You can download the source code for both list and tuple from my Github account:

Source: New feed

Using Redis as session store for ExpressJS + PassportJS settings

Since all other articles about using Redis as a session store for ExpressJS are out of date due to latest update of connect-redis, I’m going to write this short article to show how to use Redis as session store for ExpressJS.

In order to use Redis as session store for ExpressJS, first of all install connect-redis package using npm:

npm install connect-redis

Then, add connect-redis to your dependencies and Following code shows the content of app.js file:

var express = require('express');
var RedisStore = require('connect-redis')(express.session);

app.use(express.session({ store: new RedisStore({
  host: '',
  port: 6379
}), secret: 'hey you' }));

If you’re using PassportJS for users authentication, you can simply add two following lines to your app.js file to enable PassportJS as well:


That’s it. Now you have PassportJS for authentication and Redis as session store for ExpressJS.

Furthermore you can use following code to delete the Redis session:

exports.logout = function (req, res) {

Source: New feed