C++ Tlapohualli

This C# program allows you to convert integer numbers (base 10) to their Nahuatl translation (which is base 20).

Feel free to use it and test it. It is quite fun.

 

#include "stdafx.h"

 

#define MAX_DIVIDER  160000
#define MAX_NUMBER   (MAX_DIVIDER * 20) - 1

 

void printNumberName(int number, bool wasZero)
{
    switch(number)
    {
        case 160000:
            printf("xochiti ");
            break;
        case 8000:
            printf("xiquipilli ");
            break;
        case 400:
            printf("cenzontli ");
            break;
        case 20:
            printf("cempohualli ");
            break;
        case 15:
            printf("caxtollin ");
            break;
        case 10:
            printf("mahtlahtli ");
            break;
        case 9:
            printf("chicnahui ");
            break;
        case 8:
            printf("chicuei ");
            break;
        case 7:
            printf("chicome ");
            break;
        case 6:
            printf("chicuace ");
            break;
        case 5:
            printf("macuilli ");
            break;
        case 4:
            printf("nahui ");
            break;
        case 3:
            printf("yei ");
            break;
        case 2:
            printf("ome ");
            break;
        case 1:
            printf("ce ");
            break;
        case 0:
            if (wasZero)
            {
                printf("amitla ");
            }
            break;
        default:
            break;
    }
}

 

void separate(int number, int divider, bool wasZero)
{
    int multiples;
    int residue;

 

    // Base case for all number between 20 - 3,199,999
    if (number >= 20 && number <= MAX_NUMBER)
    {
        multiples = number / divider;
        residue = number % divider;
        
        // avoid prepending the multiplier "ce <number>"
        if (multiples > 1)
        {
            separate(multiples, divider / 20, wasZero);
        }
        // Print the actual number if it is a multiple of the divider
        if (multiples > 0)
        {
            printNumberName(divider, wasZero);
            // Only print huan if the current multiplier could be printed and there is a residue
            if (residue > 0)
            {
                printf("huan ");
            }
        }
        // Process the rest of the number
        if (residue > 0)
        {
            separate(residue, divider / 20, wasZero);
        }

 

    }
    // Special case for 15 - 19
    else if (number >= 15)
    {
        residue = number % 15;
        printNumberName(15, wasZero);
        if (residue > 0)
        {
            printNumberName(residue, wasZero);
        }
    }
    // Special case for 10 - 14
    else if (number >= 10)
    {
        residue = number % 10;
        printNumberName(10, wasZero);
        if (residue > 0)
        {
            printNumberName(residue, wasZero);
        }
    }
    // 0 - 9 can be directly printed
    else if (number < 10)
    {
        printNumberName(number, wasZero);
    }
    
}

 

int main()
{
    int number = -1;

 

    printf("-- TLAPOHUALLI --\n");
    printf("-----------------\n");
    printf("Write a positive integer [0, n, 3,200,000) to get its Nahuatl name. Type a negative number to exit.\n");

 

    do
    {
        printf("\nNumber: ");
        scanf("%d", &number);
        if (number >= 0 && number <= MAX_NUMBER)
        {
            separate(number, MAX_DIVIDER, number == 0);
        }
    }
    while (number >= 0);

 

    return 0;
}

Double pointers as function arguments

One of the topics that confuse programmers the most about memory management is the usage of double pointers when modifying references (addresses) inside functions. This problem is often encountered when trying to modify non-random-access data structures, like linked lists, queues, stacks, trees, graphs, etc.

Continue reading

Depth First Search

DFS is an algorithm used to visit all the nodes in a graph or tree, by visiting them branch by branch to their maximum depth.

Continue reading

Breadth First Search

BFS is an algorithm used to visit all the nodes in a graph or tree, visiting them by levels of distance from the origin node.
Continue reading

How to fix “UnboundLocalError: local variable ‘full_path’ referenced before assignment” in Django 1.4

In Django 1.4, whenever I execute

python manage.py syncdb

This error appears after the creation of all the tables:

Continue reading

Familias verbales

En el Náhuatl existen 6 familias de verbos, agrupados por terminaciones cuyo uso y transformaciones son similares:

Continue reading

Django 1.4 AJAX debug style error

In Django 1.4, whenever I wanted to show server errors when making AJAX requests, I couldn’t see the formatted error page, only the response text. This is something new in Django 1.4, because previous versions didn’t have this problem.

Continue reading

Show object history as a popup in Django Admin

If you wish to create a button that opens an object’s history inside a popup without the main bar and the breadcrumb, you have to override the history_view function of your ModelAdmin like this:

Continue reading