Programming
Finding The Median Of Two Sorted Arrays
This is one of the beautiful cases where solving the general case first, and then applying it to a particular case is simpler and smoother than solving the particular case at once.
The problem:
Given two sorted arrays, “a” and “b”, with sizes “sa” and “sb” respectively, find the median of the union of these arrays.
Complexity requirements: O(log(sa + sb)) in the worst case, both time and space.
There are a few solutions for this problem, but non of them is as intuitive as the solution of a more general problem: The Select Problem:
Given two sorted arrays, “a” and “b”, with sizes “sa” and “sb” respectively, find the k-th smallest element of the union of these arrays. Under the same complexity requirements as above.
Array Indexing in C
I found this interesting thing about array indexing in C somewhere in the web:
Suppose that “a” is an array. Then, a[5] and 5[a] are quivallent. Both are interpreted as *(5 + a) or *(a + 5) which is the same
Also: “Hello World”[3] and 3["Hello World"] are the same. Try and C
Infinite zip File
A nice idea: How to build an infinitely recursive zip file. Details and resulting file here:
http://www.steike.com/code/useless/zip-file-quine/
Have fun
Wisdom by Python (Easter Egg)
An easter egg by python displays a list of quotations, useful in programming and in many cases in life as well.
In your shell, type this:
1 | python -c 'import this' |
You’ll get this:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
via commandlinefu
Have fun
Tools to Improve the Quality of Your Code
Just found a nice link at Wikipedia with a collection of tools to help analyzing the quality of your code.
One interesting one is PMD (for Java), which is opensource. It statically analyses your code and alerts for potential problems.
For C, there is splint (opensource as well), which statically analyses your code for potential coding mistakes and security vulnerabilities.
The full list at Wikipedia is here.
Another great tool, which is not directly related to the above, is Valgrind. While the above statically analyze your code (i.e., they find potential problems by “just looking at the code”), Valgrind is a tool for dynamic analysis, which means, it analyses the code by monitoring the way it runs. It can report possible memory leaks, possible deadlooks, and many other information. (The full tool suite description can be found here). The output of Valgrind is not very easy to read, but with a little practice one can feel more comfortable with it.
There is also a list of tools for dynamic analysis at Wikipedia. It’s located here and may be worths a glance.
Enjoy coding
Free Programming Books
A great collection of freely available programming books in various languages (Bash, C, C++, Java, Perl, Python and more) is available in this link via stackoverflow.com
Enjoy
Brainfuck String Generator Generator
Here is a small C program, which takes a string as its input, and generates a (not so trivial) brainfuck code which generates this string.
For a tool to convert brainfuck code into C, see this post
To run the program, first compile it with gcc:
gcc -o bf_generator bf_generator.c
No, run it:
./bf_generator “a string to convert into brainfuck code”
You can also pass a text file as its input through a pipe:
cat testfile.txt | ./bf_generator
A Bash Script to Convert brainfuck Code Into C
Brainfuck is a minimalist Turing Complete language. You can read about it here.
The following bash script takes a file with brainfuck code (legal characters are >< ,.+-[] any other characters are ignored), and generates its C equivalent code, which can then be compiled with a C compiler and executed.
The output of this script lacks indentation. If you insist on having the C code indented you can achieve this by passing the output through a pipe to “indent”, “astyle” or similar programs.
The script should be called in the following way:
./bf.sh code.bf > code.c
Where code.bf is the file containing the brainfuck code.
Firefox Add-On to Easily Insert Tabs in Textareas
The default behaviour of hitting a tab in a web page, is moving between HTML objects. If you’re inside a textarea, when you hit tab, the focus will move to another button/textarea/form/etc…
This can be annoying if you’re trying to write code in a textarea (in codepad for example).
Somebody had a solution for this already: Tabinta (Tab In TextArea), a firefox add-on which allows you to insert tab in a text area with no effort.
Use Codepad to Save, Share and Compile Code Snippets
In a previous post, I wrote about Bonetree’s Codebox, a very nice web interface to share syntax highlighted snippets.
Codepad is a similar tool, with more features: You can compile your code and save it permanently. It also has a plugin for vim, which allows you to save/run your code in codepad right from vim.
Codepad supports a few languages, like C, C++, PHP, Python and others. There are many languages it does not support though.
About Me
Tags
My Twitter
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Science
- Security
- Shell Scripts
- Short Posts
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Archives
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)


Me @ Social Media