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.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Saturday, March 6th, 2010 Algorithms No Comments

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 ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Wednesday, January 6th, 2010 Programming No Comments

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 ;-)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , , ,

Saturday, December 12th, 2009 Cool Tricks No Comments

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 ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Monday, September 28th, 2009 Easter Eggs 1 Comment

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 ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, September 26th, 2009 Programming No Comments

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 ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, September 5th, 2009 Ebooks No Comments

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

Download the source code here: Brainfuck Generator Generator 2.41 KB

Enjoy ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Thursday, June 18th, 2009 Tools No Comments

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.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , ,

Sunday, June 14th, 2009 Programming No Comments

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.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, March 7th, 2009 Firefox 2 Comments

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.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Saturday, March 7th, 2009 Collaboration 1 Comment
my email
Already a member?
Login
Login using Facebook:
Last visitors
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and am currently studying for a BSc. in electrical engineering at The Electrical Engineering school , Technion , Israel.
I work at the verification dept. in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
March 2010
S M T W T F S
« Jan    
 123456
78910111213
14151617181920
21222324252627
28293031  
Designed for Linux. Vista Incapable
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.