Create a Word Auto-Completer in C using the Trie Data Structure

So first of all a few words about our basic structure, the trie…

In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that correspond to keys of interest.

For example, in our case, our project involves alphabets with every inner node of the trie data structure representing a letter which means that each node has a unique path from the root (can have up to 26 children as the number of the Latin alphabet) that symbolizes a word.


In our project, in order to create our fancy auto-completer we will need three structures. Firstly we need the structure of the

inner nodes that store a character that represents the letter of this node, an array of 26 letters that will navigate through the next letter. Moreover to make our program more accurate we will use an array that stores the top leafs (top words) under this inner node and a leaf pointer that is going to show us that the path from the root until this inner node reflects a word. The leaf structure contains the frequency of this word (which is the parent of the leaf) and a pointer to the last letter of the word.


Java EE DAO Pattern

Java EE really makes our life easy when dealing with Database Management. Here is a basic DAO design pattern that I use in most of my projects:

public abstract class AbstractDao, ID extends Serializable> {

protected abstract EntityManager getEntityManager();

private Class entity;

public AbstractDao(Class entity) {
this.entity = entity;

public T find(ID id) {
return getEntityManager().find(entity, id);

public List findAll() {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()

return getEntityManager().createQuery(cq).getResultList();

public List findByPage(int pageNum, int pageSize) {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()

int firstResult = pageNum * pageSize;
return getEntityManager().createQuery(cq).setFirstResult(firstResult)

public long count() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Long.class);
Root personRoot = cq.from(entity);;

List list = getEntityManager().createQuery(cq).setMaxResults(1)

long personsNumber = 0l;
if (!list.isEmpty()) {
personsNumber = list.get(0);
return personsNumber;

public T save(T entity) {
try {
if (entity.getId() == null) {
} else {
entity = getEntityManager().merge(entity);
} catch (Exception e) {
return entity;

public T saveAndFlush(T entity) {
try {
entity = save(entity);
} catch (Exception e) {
return entity;

public void flush() {

public List saveAllAndFlush(Collection collection) {
List list = null;
try {
list = collection.parallelStream()
} catch (Exception e) {
return list;

public boolean exists(ID id) {
return find(id) != null;

protected abstract Logger getLogger();

VBA: Read localstorage variable from Internet Explorer Object using temporary textfield

Here is a toy solution to access localstorage variables if they are not accessible directly.

Imagine we have test HTML webpage with HTML:

// Put the object into storage
localStorage.setItem('testObject', 'testString');

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');


Mozilla has disallowed javascript: execution via the address bar

If you try running javascript code via the address bar (not not from a bookmark) it no longer works as mozilla has disabled it for 2 years now.

Trying the following:


Causes the following error to appear in the error console:

Error: uncaught exception: ReferenceError: alert is not defined


Quickly find all php short-tags (<?= )

I found a webapplication running on linux where the previous developer had been using  shortcodes <? instead of the universally enabled <?php ( see for more info) .

Unfortunately this caused the php code to not function unless shortcuts are enabled.


Toy Example App that downloads Google Play APKs using Java and APKLeecher service

In the toy example below everything is done with string manipulation to extract the actual APK URL and proceed with the download.
The following can be improved by using a DOM parser and searching for the specific elements of interest.


How to avoid git conflicts when working with a team?

A new team member started working on the same project as me the last week.
After checking their first commit, I noticed they mixed refactoring with eclipse auto-formatting. What made this an even worse  transgression was that they have a custom maximum character width defined in their editor.  I am not against aggressively formatting or refactoring code – except when it is related to critical production code.

GIT diff unfortunately cannot handle these kinds of source refactoring changes and lists the whole file as changed. See:

After reading I came across the following gem on stackoverflow (How to avoid git conflicts when working with a team? from User Christopher) which I quote/steal from.

Ask your team three questions:

  1. Do we enforce whitespace conventions?automatically finalize parameters.
  2. Do we generate textual build artifacts? For example, do we minify js, generate css rules from .sass or .scss files, or build xml configurations on the fly? Do we check them in?[…]

These three things cause the vast majority of our collective conflict pain:

Versioning Pain


Displaying base64 image data from DB using PHP

This is based on a question from stackoverflow that I answered.

Imagine we store within a database image data as the following:


After retrieving the data from MySQL how do we display it?

Convert base64 data to image:

In this case you need to use the header function to tell the browser you will send an image.
Following, you take the $data variable that you set from a MySQL query, and use base64_decode to display.

header("Content-type: image/gif");
$data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo base64_decode($data);

Clients request .php to display image:

In this case you want to encode the image data directly into a PHP generated page you would do the following:

echo '<img src="data:image/gif;base64,' . $data . '" />';

There is a downside to this in that the browser does not cache an image that is used on multiple separate pages (since each page contains the binary data within the HTML document).The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.



Retrieve images of chemical structures using Excel VBA

The below is from an answer I posted on stackoverflow.

You can retrieve the chemical structure of an image using the following:

Sub Run()
getImage ("iron")
End Sub

Public Function getImage(ByVal name As String) As String
  Dim imgURL As String
  Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
  XMLhttp.setTimeouts 1000, 1000, 1000, 1000
  imgURL = "" + name + "/image"

  XMLhttp.Open "GET", imgURL, False

  If XMLhttp.Status = 200 Then
   'It exists so get the image
    Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 250, 250
  End If
End Function

This can further simplified to simply only use

Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 300, 300

Instead of downloading the image the twice, and simply using an error handler to catch when image not found.



JQuery Calendar and PHP: Handling Inserts

1) Getting Current Time in Javascript

The ISO 8601 date format can be utilized to convert the date on the browser side to a format that include timestamp information. Quoting from here:

Note that the “T” appears literally in the string, to indicate the beginning of the time element. Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator (“Z”). Used in ATOM RSS feeds.

function fnISO() {
// Only works in Firefox using ECMAScript 5
var now = new Date().toISOString();

Result: 2009-08-06T23:36:31.390Z