Click on for the latest popularity rankings of programming languages. It is not based on absolute value but on the relative change of market share.
For January 2012, Objective C showed the fastest growth. Click on the link for the the definition of the Tiobe index.
The next popular is C# followed by C and Javascript. On the other hand, based on absolute market share, the top languages are Java, C, C#, C++, Objective-C, PHP and Visual Basic. Our favorite language Python is at #8. Interesting to note that the statistical software R is at #19.
Tuesday, January 17, 2012
Saturday, January 14, 2012
Does a number string represents an integer or a floating value in C?
I got the shock of my blogging life when I cannot access an old post about the evils of the C function atof. Here is a code which return 1 for integers and 2 for floating point and 0 if not a valid integer or a valid float.
Remove the deubgging printf statements when you use it for applications.
When the program is run, it returns a code of 2 to denote a floating point number, a 1 for an integer, and a 0 if not a number.
/* author Ernesto P. Adorio UPDEPP (University of the Philippines, Extension Program in Pampanga, Clarkfield, Pampanga email ernesto. adorio@ gmail.com [remove spaces] version 0.0.1 january 14, 2012 */ #include#include int numbertype(char *s) { char *t = s; while (isspace(*t)) t++; if (*t == '\0') return 0; // number sign printf("ltrimmed [%s]", t); if (*t == '+' || *t == '-') t++; if (*t == '\0') return 0; printf("after sign [%s]", t); while (isdigit(*t)) t++; printf("after digits [%s]", t); if (*t == '\0') return 1; // an integer! // floating point?? printf ("decimal point? [%s] ", t); if (*t == '.') t++; printf ("fractional digits?"); while (isdigit(*t)) t++; if (*t == '\0') return 2; // exponent part. printf ("testing exponent part %s", t); if (*t == 'e') t++; printf ("after 'e' "); if (*t == '\0') return 0; // error! printf ("sign after e?", t); if (*t == '+' || *t == '-') t++; printf("%s", t); if (*t == '\0') return 0; // error! while (isdigit(*t)) t++; if (*t == '\0') return 2; // a floating point number! return 0; // not an integer or floating point. } int main() { printf("numtype =[%d]", numbertype("-123.34")); };
Remove the deubgging printf statements when you use it for applications.
When the program is run, it returns a code of 2 to denote a floating point number, a 1 for an integer, and a 0 if not a number.
A basic Forth language interpreter (non-interactive)
Last time we showed a simple tokenizer for a Forth interpreter. We find it easier to combine the ideas in that post to create a simple Forth engine which process a simple string.
We will expound further on this until we successfully build a compiler for a non-standard Forth language.
Have fun working on this. The current version works on simple strings but specified on the source code itself. We will introduce interactivity and expand the capabilities of this simple Forth interpreter.
We will expound further on this until we successfully build a compiler for a non-standard Forth language.
Have fun working on this. The current version works on simple strings but specified on the source code itself. We will introduce interactivity and expand the capabilities of this simple Forth interpreter.
/* file simple-forth.c author Dr. Ernesto P. Adorio UPDEPP (University of the Philippines, Extension Program in Pampanga Clarkfield, Pampanga email ernesto.adorio@gmail.com version 0.0.1 January 14, 2012 */ #includeSave the code to a file simple-forth.c, then compile using the following command line on the directory where the source file is stored. gcc simple-forth.c -std=c99 -o forth It should compile cleanly, then execute the executible by issuing ./forth When the program runs, it prints out#include #include #include #define MAXSTKLEN 32 #define MAXTOKENLEN 128 enum {L_EMIT, L_ADD, L_SUB, L_MUL, L_DIV, L_POP, L_INT32, L_CR, L_ERROR} OPCODES; const char *stdwords[] = { ".", "+", "-", "*", "/", "pop", "int32", "cr", }; int LENSTDWORDS = 8; int stack[MAXSTKLEN]; int SP = 0; /* stack pointer index */ int main(){ char tokens[] = " 123 34 + . cr 567 -3456 * . cr"; char *tokstart = tokens; char *tokend = tokstart; int opcode; /* @@@ printf("tokstart: %s\n", tokstart);*/ tokend = tokens; while (1) { tokstart = tokend; /* ignore leading white spaces */ while (isspace(*tokstart)) tokstart++; /* find terminating space of end of string */ tokend = tokstart; while (!isspace(*tokend) && *tokend != '\0') tokend++; /* string terminator */ if (*tokend != '\0') { *tokend = '\0'; tokend ++; } else { break; } /* get opcode */ opcode = -1; /*@@@ printf("token [%s]\n", tokstart); */ for (int i =0; i < LENSTDWORDS; i++) { if (strcmp(stdwords[i], tokstart) == 0){ opcode = i; break; } } if (opcode == -1){ opcode = L_INT32; } /* Evaluate opcode */ switch (opcode){ case L_EMIT: //printf("opcode L_EMIT %s \n", tokstart); printf ("%d", stack[SP--] ); break; case L_ADD: //printf("opcode L_ADD %s\n", tokstart); stack[SP-1] += stack[SP]; SP--; break; case L_SUB: //printf("opcode L_SUB %s\n", tokstart); stack[SP-1] += stack[SP]; SP--; break; case L_MUL: //printf("opcode L_MUL %s\n", tokstart); stack[SP-1] *= stack[SP]; SP--; break; case L_DIV: //printf("opcode L_DIV %s\n", tokstart); stack[SP-1] /= stack[SP]; SP--; break; case L_INT32: //printf("opcode L_INT32 %s\n",tokstart); stack[++SP]= atoi(tokstart); break; case L_POP: //printf("opcode L_POP %s \n", tokstart); SP--; break; case L_CR: // printf("opcode L_CR %s \n", tokstart); printf("\n"); } } printf("\n"); return 0; }
toto@toto-Aspire-4520:~/Blogs/my-other-life-as-programmer/forth$ ./forth 157 -1959552
Tuesday, October 18, 2011
Testing latex server by watchmath.com.
The are of a triangle is $\frac{1}{2}b*h$.
I thought this was working already! Oh, I have to redo the installation.
January 15, 2012: This is making me sick! this does not work anymore! I changed to mathjax latex server. For instructions, refer to tex.stackexchange.com
Thursday, October 13, 2011
Dennis Ritchie (September 9, 1941 – October 8, 2011: inventor of C programming language
One of my regrets in life is not having to meet in person Dennis Ritichie, the developer of the C programmng language.
I will never forget the time I have to understand pointers (I suspect my instructor did not understand much too!). C is like more a friendlier assembly language in English! C++, Java may be more "in" these days, but they owe their success to the pioneering path blazed by C.
I will never forget the time I have to understand pointers (I suspect my instructor did not understand much too!). C is like more a friendlier assembly language in English! C++, Java may be more "in" these days, but they owe their success to the pioneering path blazed by C.
Wednesday, October 12, 2011
Python: Permutations, Combinations, Cartesian Products with Itertools module
Python itertools module has built-in functions for permutations, combinations, cycles and products.
So the Python programmers can focus on building the next killer-apps :) which will dominate the whole world, instead
of reading obtuse treatises, papers, and other publications on algorithms for generating combinatorial objects.
Here we list the combinatorial capabilities of Python (versions 2.6+) in the itertools module.
Itertools has cartesian products! So instead of writing (this from the documentation)
((x,y) for x in A for y in B)
we simply write product(A,B)
Wait, how about the cartesian product of A with itself?
you can write product(A,A) or if you want more, product(A,A,A,A). The latter can be written as
product(A, repeat=4). Python programmers can be lazy as Perl programmers!
Not strictly referrring to a mathematical object. Rather it refers to the way an element in an
iterable is accessed. Instead of stopping at the last element, the access is cycled back to the first element.
Try this on the command line (not running in a script file)
You will notice that this loop never terminates, and after the 4 is printed, the next element to be printed is the first element.
Type Ctrl-C to abort the printing.
Aside from these basic combinatorial objects, Itertools provide now a repeat function! For example, a list of 100
elements with all values equal to 5: [5] * 100 can now be written as itertools.repeat(5, 100). Easier to understand for beginners.
Dont you just love Python?
Hopefully, Python will offer genuine cycle objects (where you can multiply two cycles) and derangements with basic functions such as nPr and nCr. These combinatorial features are solutions waiting for problems and applications!
TO BE CONTINUED.
So the Python programmers can focus on building the next killer-apps :) which will dominate the whole world, instead
of reading obtuse treatises, papers, and other publications on algorithms for generating combinatorial objects.
Here we list the combinatorial capabilities of Python (versions 2.6+) in the itertools module.
Permutations
itertools.permutation create a permutation object from an iterable input or string. You can list
the successive permutations by using object.next() or by creating a list (a bad idea if the size of the object is large!)
list(object). Here is an example:
x = itertools.permutation(range(7)) try: while True: print x.next() except: pass
You can also do:
x = itertools.permutation(range(7)) for i, e in enumerate(list(x)): print i+1, e
Converting to a list give rise to a dangerous situation where you can run out of memory!. but if you know that it can be safely stored,
by all means use a list conversion.
Permutations can also accept an extra parameter r so you can generate all permutations of size n of the object taken r at a time.
The number of elements in such permutations is given by n!/(n-r)!. If r is not specified, then the number of permutations is n!
which look innocous.(50! is nothing to sneeze at).
combinations
itertools.combinations creates a combinatorial object. Here ordering does not matter. As an example, the combinations of 6 elements taken 3 at a time can be listed in two groups, as follows:
import itertools seq = range(6) x = itertools.combinations(seq, 3) try: for i, e in list(enumerate(x)): f = [y for y in seq if y not in e] print i+1, list(e), f except: passThe output of permutations and combinations are tuples for fast execution of the functions, and have to be typecast to a list. Itertools also provide combinations with replacement. The best way to see the difference with the standard combinations is to see them in action from the command line:
x = combinations_with_replacement([1,2,3,4], 2) y = combinations([1,2,3,4], 2) list(x) [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)] list(y) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Cartesion products
Itertools has cartesian products! So instead of writing (this from the documentation)
((x,y) for x in A for y in B)
we simply write product(A,B)
Wait, how about the cartesian product of A with itself?
you can write product(A,A) or if you want more, product(A,A,A,A). The latter can be written as
product(A, repeat=4). Python programmers can be lazy as Perl programmers!
cycle
Not strictly referrring to a mathematical object. Rather it refers to the way an element in an
iterable is accessed. Instead of stopping at the last element, the access is cycled back to the first element.
Try this on the command line (not running in a script file)
x = cycle((1,2,3,4)) while True: print x.next()
You will notice that this loop never terminates, and after the 4 is printed, the next element to be printed is the first element.
Type Ctrl-C to abort the printing.
Aside from these basic combinatorial objects, Itertools provide now a repeat function! For example, a list of 100
elements with all values equal to 5: [5] * 100 can now be written as itertools.repeat(5, 100). Easier to understand for beginners.
Dont you just love Python?
Hopefully, Python will offer genuine cycle objects (where you can multiply two cycles) and derangements with basic functions such as nPr and nCr. These combinatorial features are solutions waiting for problems and applications!
TO BE CONTINUED.
Saturday, October 1, 2011
Programming Problems: Replace numbers in a list by an increasing sequence.
Consider X = [1, 5, 7, 1, 34]. We want to replace this by X = [1,3,2,1,4].
Consider X = [1000, 500, 700, 5000, 700]. We want to replace this by [3, 1,2,4,2].
Criteria:
1. Use Python. Sorry no java.
2. Fewest lines of code.
3. Elegant, not to use a temporary copy and not using a convoluted BRUTE force method as in the following example, yes I wrote it but that was a temporary solution. I refuse to give a hint, but Python has a powerful discrete math function :)
Consider X = [1000, 500, 700, 5000, 700]. We want to replace this by [3, 1,2,4,2].
Criteria:
1. Use Python. Sorry no java.
2. Fewest lines of code.
3. Elegant, not to use a temporary copy and not using a convoluted BRUTE force method as in the following example, yes I wrote it but that was a temporary solution. I refuse to give a hint, but Python has a powerful discrete math function :)
tempR = X[:] for j in range(1, n+1): minrank = min(tempR) for k in range(n): if X[k] == minrank and k< n+1: X[k] = j tempR[k] = n+1 return X4. Name your function, normalsequence(X). . Here is what I believe a more elegant solution.
def normalsequence(X): #ensure that the ranks are in sequence! for i, x in enumerate(sorted(list(set(R[:])))): for j, y in enumerate(R): if y == x: R[j] = i+1Far more sophisticated (one-liners) for a similar problem is stack overflow: efficient method to calculate the rank vector of a Python list. This solution is also part of the Python code for transforming raw scores to ranks in my other post Python, statistics: Ranking a single array of raw scores
Subscribe to:
Posts (Atom)