JSON tree traversal in JQuery

I have stumbled upon that there are developers who cannot do that while looking over some questions at Stackoverflow.


function enterPercents(data) {
    if (data == '[]') return;
    var parsed = jQuery.parseJSON(data);
    if (parsed) {
        processJsonTree("jsonObject", parsed);
    }
}

function processJsonTree(path, node) {
    if (typeof node != 'object') {
        alert(path + " = " + node);
    } else {
        $.each(node, function (key, item) {
            var cpath = path + "[" + key + "]";
            processJsonTree(cpath, item);
        });
    }
}

enterPercents('{"day":{"1":2,"2":3,"3":4,"4":5},"week":{"1":1,"2":1,"3":1,"4":60},"month":{"1":10,"2":10,"3":10,"4":10},"sell":{"1":0,"2":0,"3":0,"4":0}}');


Binary Heap – C++ implementation

It is relatively unlikely to find a simple implementation of this structure online, so…:

#include <iostream>
using namespace std;

#define MAX 10000
#define NONE -1

struct heap {
    int values[10000];
    int size;
};

int parent(int index) {
    return (index-1)>>1;
}

int leftChild(heap * h, int index) {
    int ch = (index<<1)+1;
    return h->size<= ch ? NONE : ch;
}

int rightChild(heap * h, int index) {
    int ch = (index<<1)+2;
    return h->size<= ch ? NONE : ch;
}

void swap(heap * h, int a, int b) {
    int tmp = h->values[a];
    h->values[a] = h->values[b];
    h->values[b] = tmp;
}

void heapifyDown(heap * h) {
    int index = 0;
    while (index != h->size-1) {
        int lIndex = leftChild(h, index);
        int rIndex = rightChild(h, index);
        if (lIndex == NONE) return;
        int childIndex = lIndex;
        if (rIndex == NONE) {
            childIndex = lIndex;
        } else if(h->values[lIndex] > h->values[rIndex]) {
            childIndex = rIndex;
        }
        swap(h,index,childIndex);
        index = childIndex;
    }
}

void heapifyUp(heap * h, int index) {
    while (index) {
        int pIndex = parent(index);
        if (h->values[index] < h->values[pIndex]) {
            swap(h,index,pIndex);
            index = pIndex;
        } else {
            break;
        }
    }
}

void insert(heap * h, int value) {
    h->values[h->size++] = value;
    heapifyUp(h, h->size-1);
}

void pop(heap * h) {
    h->values[0] = h->values[--h->size];
    heapifyDown(h);
}

int top(heap * h) {
    return h->values[0];
}

bool isEmpty(heap * h) {
    return h->size==0;
}

heap * initHeap() {
    heap * tmp = new heap[1];
    tmp->size = 0;
    return tmp;
}

void print (heap * h) {
    for (int i = 0; i < h->size; ++i) {
        cout << h->values[i] << " ";
    }
    cout << endl;
}

int main() {
    int n;
    cin >> n;
    int a;
    heap * tmp = initHeap();
    for (int i = 0; i < n; ++i) {
        cin >> a;
        insert(tmp,a);
    }
    while (!isEmpty(tmp)) {
        int smallest = top(tmp);
        pop(tmp);
        cout << smallest << " ";
    }
    return 0;
}

ADB pull/push operations

Usually, if you are an Android developer, you would like to pull and push files from/to your android emulated device. However, there are some issues you might encounter:

– When you pull or push, you might get a message that the directory is read-only
– You might not be able to find the file after you pulled it.

The problem-like way to pull and push files:
Run your adb device as a shell:
adb shell
Then just pull the file
adb pull /data/data/something/something.db C:tmpsomething.db

The problem-proof way to do it:
Open the DDMS perspective in Eclipse, find the file and download it by click the SAVE button.

“Fundamentals of Computer Programming with C#” finally came out

About a year and a half back I worked on the translation (and edit) of the famous Bulgarian programming book “Fundamentals of Computer Programming with C#” into English. Well, it is finally done and published:

So, what makes this book really good, challenging, but at the same time perfect for novices? Firstly, it covers ABSOLUTELY the fundamentals and we are not only talking about basic programming such as loops, containers, oop, etc… we are talking about real-life programming situations – how to solve problems, how to make your code readable for people who haven’t seen your work before, how to make things faster and mostly how to think like a professional software engineer.

It doesn’t only cover topics by just introducing the needed material, but also suggests a lot of fundamental problems in programming (which would usually be asked on an official interview). Of course, you may not be able to solve on them on your own. For this case, there are also published solutions + materials for further reading.

Two years and a half back I read the Java version of the same book which basically took my programming skills to a whole new level and gave me a deep understanding of programming.

There is not really which can be told more about this book, but really:

– It’s good
– It’s tested. Thousands of programmers in Bulgaria study on this book and afterwards work in the IT industry
– It’s free. You do not have to pay for it, you do not have to download it illegally

And here’s the website of the book:
http://www.introprogramming.info/english-intro-csharp-book/

JSON – the solution to most of your problems

Whether from storing projects, receiving data or sending data, JSON is probably the best solution I have ever encountered. I have worked on quite a few projects which use JSON for communication and data storage. The latter has saved me a lot of the well-known headaches when a client tells you that he wants to add some new input to the current app/website.
So, what was my solution to this in the past? Total bull shit. I just made another field in the database, added the needed variables in the code, etc. Well, that’s not cool. Will you be constantly adding new fields in your table? Personally, I just store a text field called data and store everything in JSON format.

And what about data communication/parsing? Well, the other typical standard is xml which sucks, because the normal built-in methods do not parse it often very well and it is a pain in the ass to build one yourself when you’re in a hurry to finish the project. On the other end, JSON parsers work very well. For example, in PHP (yeah, that shit) there are two key methods for working with json -> json_decode and json_encode. And you can actually decode a JSON file to an array, which makes everything easy because you just have to use the foreach iterator or a normal loop.

Also, JSON is a way to make cool games/guis. In the libgdx framework (java game 2d development framework), you can give information about the skin of your actors with JSON. And this actually save you a lot of code. You just write all of the colors, fonts, etc you will use in the json file and there you have it – a beautiful working gui with a few lines of code to load the atlas, the skin, etc.