learning python through video lecturesOne of the upcoming projects I am doing (I will reveal it in one of the next blog posts.) is going to be written entirely in Python. I have a good understanding of Python but, same as I had with JavaScript, I have little experience doing projects from the ground up in it.

Update: the project was redditriver.com, read designing redditriver.com (includes full source code).

Before diving into the project I decided to take a look at a few Python video lectures to learn language idioms and features which I might have not heard of.

Finding Python video lectures was pretty easy as I run a free video lecture blog.

First Python Lecture: Python for Programmers

Interesting moments in the lecture:

  • [07:15] There are several Python implementations - CPython, PyPy, IronPython and Jython.
  • Python has similarities with [12:04] Java, [15:30] C++ and [19:05] C programming languages.
  • [15:37] Python is multi-paradigm language supporting object oriented, procedural, generic and functional programming paradigms.
  • [19:49] Python follows C standard's rationale: 1. trust the programmer; 2. don't prevent the programmer from doing what needs to be done; 3. keep the language small and simple; 4. provide only one way to do an operation.
  • [13:02] Python code is normally implicitly compiled to bytecode.
  • [13:25] Everything inherits from object.
  • [14:56] Garbage collection in classic Python happens as soon as possible.
  • [24:50] Python has strong but dynamic typing.
  • [28:42] Names don't have types, objects do.
  • [36:25] Why are there two ways to raise a number to a power (with double star ** operator and pow())? - Because pow() is a three argument function pow(x, y, z) which does x^y mod z.
  • [36:52] Python supports plain and Unicode strings.
  • [38:40] Python provides several built-in container types: tuple's, list's, set's, frozenset's and dict's.
  • [41:55] c[i:j:k] does slicing with step k.
  • [42:45] c[i:j] always has first bound included and last bound excluded.
  • [44:11] Comparisons can be "chained", for example 3 < x < 9.
  • [45:05] False values in Python are 0, "", None, empty containers and False.
  • [49:07] 'for' is implemented in terms of iterators.
  • [52:18] Function parameters may end with *name to take a tuple of arbitrary arguments, or may end with **name to take a dict of arbitrary arguments.
  • [55:39] Generators.
  • [01:00:20] Closures.
  • [01:02:00] Classes.
  • [01:05:30] Subclassing.
  • [01:07:00] Properties.
  • [01:14:35] Importing modules.
  • [01:16:20] Every Python source file is a module, and you can just import it.
  • [01:17:20] Packages.

Okay, this talk was a very basic talk and it really was an introduction for someone who never worked in Python. I could not find many interesting points to point out from the lecture, so the last 8 points are just titles of topics covered in the lecture.

Second Python Lecture: Advanced Python or Understanding Python

Interesting moments in the lecture:

  • [03:18] Python is designed by implementation.
  • [04:20] Everything is runtime (even compiletime is runtime).
  • [04:42] A namespace is a dict.
  • [05:33] A function is created by having its code compiled to code object, then wrapped as a function object.
  • [10:00] Everything is an object and a reference, except variables.
  • [11:00] Python has 3-scopes rule - names are either local, global or builtin.
  • [11:12] Global names mean they exist in a module, not everywhere!
  • [14:02] 'import mod' statement is just a syntactic sugar for mod = __import__("mod").
  • [14:15] sys.modules contains a list of cached modules.
  • [14:30] You may set the value of a module name in sys.modules dict to None, to make it unimportable.
  • [15:20] Mutable objects are not hashable, most immutable objects are hashable.
  • [18:05] Assignments, type checks, identity comparison, 'and or not', method calls are not object hooks.
  • [22:15] Any Python object has two special attributes __dict__ which holds per object data and __class__ which refers to the class.
  • [27:18] Iterators are not rewindable, reversible or copyable.
  • [29:04] Functions with yield return generators.
  • [39:20] "New" style classes unified C types and Python classes.
  • [47:00] __slots__ prevent arbitrary attribute assignments.
  • [48:10] __new__ gets called when the object gets created (__init__ gets called when the object has already been constructed).
  • [01:01:40] Inheritance is resolved using a C3 Method Resolution Order algorithm.
  • [01:04:57] Unicode in Python.
  • [01:06:45] UTF8 is not Unicode, it's a Unicode encoding!
  • [01:11:50] codecs module automatically converts between encodings.
  • [01:13:00] Recommended reading - Functional Programming HOWTO and Python source code ;)

This lecture gets pretty complicated towards the end as the lecturer goes deep into subjects which require adequate experience with Python.

Third Python Lecture: Python: Design and Implementation

Interesting moments in the lecture:

  • [01:27] Python started in late 1989, around December 1989.
  • [01:57] Python's named after Monty Python's Flying Circus.
  • [06:20] Python was first released to USENET and then a public group comp.lang.python was started.
  • [08:06] Guido van Rossum, the author of Python, moved to US in 1995.
  • [09:58] Python will never become a commercial project thanks to Python Software Foundation, founded in 2001.
  • [11:23] Python origins go back to ideas from ABC programming language (indentation for statement grouping, simple control structures, small number of data types).
  • [13:01] Being on ABC's implementation team, Guido learned a lot about language design and implementation.
  • [16:37] One of the main goals of Python was to make programmer's productivity more important than program's performance.
  • [17:10] Original positioning of Python was in the middle between C and sh.
  • [21:13] Other languages, such as, Modula-3, Icon and Algol 68 also had an impact on Python's implementation details.
  • [24:32] If a feature can be implemented as a clear extension module, it is always preferable to changing the language itself.
  • [25:23] The reason Python uses dictionaries for namespaces is that it required minimal changes to the stuff the language already had.
  • [28:11] Language features are accepted only if they will be used by a wide variety of users. A recent example of a new language feature is the 'with' statement.
  • [31:13] Question from the audience - "Can't the 'with' statement be implemented via closures?"
  • [34:25] Readable code is the most important thing.
  • [37:57] To add a new language feature, PEP, Python Enhancement Proposal has to be written.
  • [40:47] Python's goal was to be cross-platform (hardware & OS) right from the beginning.
  • [47:09] Python's lexer has a stack to parse indentation.
  • [49:20] Two passes are run over abstract syntax tree, one to generate symbol table and the other to produce bytecode.
  • [50:20] Bytecode opcodes are very high level, close to conceptual primitive operations in language, rather close to what hardware could do.
  • [01:02:54] Jython generates pure Java bytecode.
  • [01:03:01] Jython's strings are always Unicode.
  • [01:06:45] IronPython is as fast or even faster than CPython.

Question and answer session:

  • [01:08:57] Have there been attempts to compile Python to machine code (for example, x86)?
  • [01:13:46] Why not use simple tail recursion?
  • [01:16:09] How does the garbage collection work?

This video lecture gives an insight on history and development ideas of Python language. I believe it is important to know the history and details of the language design decisions to be really competent in it.

There are a few more lectures I have found:

There is also some great reading material available:

Have fun learning Python!

PS. Do you know any other video lectures on Python that I haven't mentioned here? Feel free to post them in the comments! Thanks! :)

bash readline emacs editing mode default keyboard shortcut cheat sheetLet me teach you how to work efficiently with command line history in bash.

This tutorial comes with a downloadable cheat sheet that summarizes (and expands on) topics covered in this guide.

Download PDF cheat sheet: bash history cheat sheet (.pdf) (downloaded: 186181 times)
Download ASCII cheat sheet: bash history cheat sheet (.txt) (downloaded: 17275 times)
Download TEX cheat sheet: bash history cheat sheet (.tex) (downloaded: 6906 times)

In case you are a first time reader, this is the 3rd part of the article series on working efficiently in bourne again shell. Previously I have written on how to work efficiently in vi and emacs command editing modes by using predefined keyboard shortcuts (both articles come with cheat sheets of predefined shortcuts).

First, lets review some basic keyboard shortcuts for navigating around previously typed commands.

As you remember, bash offers two modes for command editing - emacs mode and vi mode. In each of these editing modes the shortcuts for retrieving history are different.

Suppose you had executed the following commands:

$ echo foo bar baz
$ iptables -L -n -v -t nat
$ ... lots and lots more commands
$ echo foo foo foo
$ perl -wle 'print q/hello world/'
$ awk -F: '{print$1}' /etc/passwd
$

and you wanted to execute the last command (awk -F ...).

You could certainly hit the up arrow and live happily along, but do you really want to move your hand that far away?

If you are in emacs mode just try CTRL-p which fetches the previous command from history list (CTRL-n for the next command).

In vi mode try CTRL-[ (or ESC) (to switch to command mode) and 'h' ('j' for the next command).

There is another, equally quick, way to do that by using bash's history expansion mechanism - event designators. Typing '!!' will execute the previous command (more about event designators later).

Now, suppose that you wanted to execute 'iptables -L -n -v -t nat' command again without retyping it.

A naive user would, again, just keep hitting up-arrow key until he/she finds the command. But that's not the way hackers work. Hackers love to work quickly and efficiently. Forget about arrow keys and page-up, page-down, home and end keys. They are completely useless and, as I said, they are too far off from the main part of the keyboard anyway.

In emacs mode try CTRL-r and type a few first letters of 'iptables', like 'ipt'. That will display the last iptables command you executed. In case you had more than one iptables commands executed in between, hitting CTRL-r again will display older entries. In case you miss the right command and move too deep into history list, you can reverse the search direction by hitting CTRL-s (don't forget that by default CTRL-s stops the output to the terminal and you'll get an effect of "frozen" terminal (hit CTRL-q to "unfreeze"), see stty command to change this behavior).

In vi mode the same CTRL-r and CTRL-s still work but there is another way more specific to vi mode.
Switch to command mode by hitting CTRL-[ or ESC and hit '/', then type a first few characters of 'iptables' command, like 'ipt' and hit return. Bash will display the most recent match found in history. To navigate around use 'n' or just plain '/' to repeat the search in the same direction, and 'N' or '?' to repeat the search in opposite direction!

With event designators you may execute only the most recently executed command matching (or starting with) 'string'.

Try '!iptables' history expansion command which refers to the most recent command starting with 'iptables'.

Another way is to use bash's built in 'history' command then grep for a string of interest and finally use an event designator in form '!N', where N is an integer which refers to N-th command in command history list.

For example,

$ history | grep 'ipt'
  2    iptables -L -n -v -t nat
$ !2     # will execute the iptables command

I remembered another way to execute N-th command in history list in vi editing mode. Type 'N' (command number) and then 'G', in this example '2G'

Listing and Erasing Command History

Bash provides a built-in command 'history' for viewing and erasing command history.

Suppose that we are still working with the same example:

$ echo foo bar baz
$ iptables -L -n -v -t nat
$ ... lots and lots more commands
$ echo foo foo foo
$ perl -wle 'print q/hello world/'
$ awk -F: '{print$1}' /etc/passwd
$

Typing 'history' will display all the commands in bash history alongside with line numbers:

  1    echo foo bar baz
  2    iptables -L -n -v -t nat
  ...  lots and lots more commands
  568  echo foo foo foo
  569  perl -wle 'print q/hello world/'
  570  awk -F: '{print$1}' /etc/passwd

Typing 'history N', where N is an integer, will display the last N commands in the history.
For example, 'history 3' will display:

  568  echo foo foo foo
  569  perl -wle 'print q/hello world/'
  570  awk -F: '{print$1}' /etc/passwd

history -c will clear the history list and history -d N will delete a history entry N.

By default, the history list is kept in user's home directory in a file '.bash_history'.

History Expansion

History expansion is done via so-called event designators and word designators. Event designators can be used to recall previously executed commands (events) and word designators can be used to extract command line arguments from the events. Optionally, various modifiers can be applied to the extracted arguments.

Event designators are special commands that begin with a '!' (there is also one that begins with a '^'), they may follow a word designator and one or more modifiers. Event designators, word designators and modifiers are separated by a colon ':'.

Event Designators

Lets look at a couple of examples to see how the event designators work.

Event designator '!!' can be used to refer to the previous command, for example,

$ echo foo bar baz
foo bar baz
$ !!
foo bar baz

Here the '!!' executed the previous 'echo foo bar baz' command.

Event designator '!N' can be used to refer to the N-th command.
Suppose you listed the history and got the following output:

  1    echo foo foo foo
  2    iptables -L -n -v -t nat
  ...  lots and lots more commands
  568  echo bar bar bar
  569  perl -wle 'print q/hello world/'
  570  awk -F: '{print$1}' /etc/passwd

Then the event designator '!569' will execute 'perl ...' command, and '!1' will execute 'echo foo foo foo' command!

Event designator '!-N' refers to current command line minus N. For example,

$ echo foo bar baz
foo bar baz
$ echo a b c d e
a b c d e
$ !-2
foo bar baz

Here the event designator '!-2' executed a one before the previous command, or current command line minus 2.

Event designator '!string' refers to the most recent command starting with 'string'. For example,

$ awk --help
$ perl --help

Then the event designator '!p' or '!perl' or '!per' will execute the 'perl --help' command. Similarly, '!a' will execute the awk command.

An event designator '!?string?' refers to a command line containing (not necessarily starting with) 'string'.

Perhaps the most interesting event designator is the one in form '^string1^string2^' which takes the last command, replaces string1 with string2 and executes it. For example,

$ ehco foo bar baz
bash: ehco: command not found
$ ^ehco^echo^
foo bar baz

Here the '^ehco^echo^' designator replaced the incorrectly typed 'ehco' command with the correct 'echo' command and executed it.

Word Designators and Modifiers

Word designators follow event designators separated by a colon. They are used to refer to some or all of the parameters on the command referenced by event designator.

For example,

$ echo a b c d e
a b c d e
$ echo !!:2
b

This is the simplest form of a word designator. ':2' refers to the 2nd argument of the command (3rd word). In general ':N' refers to Nth argument of the command ((N+1)-th word).

Word designators also accept ranges, for example,

$ echo a b c d e
a b c d e
$ echo !!:3-4
c d

There are various shortcuts, such as, ':$' to refer to the last argument, ':^' to refer to the first argument, ':*' to refer to all the arguments (synonym to ':1-$'), and others. See the cheat sheet for a complete list.

Modifiers can be used to modify the behavior of a word designators. For example:

$ tar -xvzf software-1.0.tgz
software-1.0/file
...
$ cd !!:$:r
software-1.0$

Here the 'r' modifier was applied to a word designator which picked the last argument from the previous command line. The 'r' modifier removed the trailing suffix '.tgz'.

The 'h' modifier removes the trailing pathname component, leaving the head:

$ echo /usr/local/apache
/usr/local/apache
$ echo !!:$:h
/usr/local

The 'e' modifier removes all but the trailing suffix:

$ ls -la /usr/src/software-4.2.messy-Extension
...
$ echo /usr/src/*!!:$:e
/usr/src/*.messy-Extension    # ls could have been used instead of echo

Another interesting modifier is the substitute ':s/old/new/' modifier which substitutes new for old. It can be used in conjunction with 'g' modifier to do global substitution. For example,

$ ls /urs/local/software-4.2 /urs/local/software-4.3
/usr/bin/ls: /urs/local/software-4.2: No such file or directory
/usr/bin/ls: /urs/local/software-4.3: No such file or directory
$ !!:gs/urs/usr/
...

This example replaces all occurances of 'urs' to 'usr' and makes the command correct.

There are a few other modifiers, such as 'p' modifier which prints the resulting command after history expansion but does not execute it. See the cheat sheet for all of the modifiers.

Modifying History Behavior

Bash allows you to modify which commands get stored in the history list, the file where they get stored, the number of commands that get stored, and a few other options.

These options are controlled by setting HISTFILE, HISTFILESIZE, HISTIGNORE and HISTSIZE environment variables.

HISTFILE, as the name suggests, controls where the history file gets saved.
For example,

$ export HISTFILE=/home/pkrumins/todays_history

will save the commands to a file /home/pkrumins/todays_history

Set it to /dev/null or unset it to avoid getting your history list saved.

HISTFILESIZE controls how many history commands to keep in HISTFILE.
For example,

$ export HISTFILESIZE=1000

will keep the last 1000 history commands.

HISTSIZE controls how many history commands to keep in the history list of current session.
For example,

$ export HISTSIZE=42

will keep 42 last commands in the history of current session.

If this number is less than HISTFILESIZE, only that many commands will get written to HISTFILE.

HISTIGNORE controls the items which get ignored and do not get saved. This variable takes a list of colon separated patterns. Pattern '&' (ampersand) is special in a sense that it matches the previous history command.

There is a trick to make history ignore the commands which begin with a space. The pattern for that is "[ ]*"

For example,

$ export HISTIGNORE="&:[ ]*:exit"

will make bash ignore duplicate commands, commands that begin with a space, and the 'exit' command.

There are several other options of interest controlled by the built-in 'shopt' command.

The options may be set by specifying '-s' parameter to the 'shopt' command, and may be unset by specifying '-u' parameter.

Option 'histappend' controls how the history list gets written to HISTFILE, setting the option will append history list of current session to HISTFILE, unsetting it (default) will make HISTFILE get overwritten each time.

For example, to set this option, type:

$ shopt -s histappend

And to unset it, type:

$ shopt -u histappend

Option 'histreedit' allows users to re-edit a failed history substitution.

For example, suppose you had typed:

$ echo foo bar baz

and wanted to substitute 'baz' for 'test' with the ^baz^test^ event designator , but you made a mistake and typed ^boo^test^. This would lead to a substitution failure because the previous command does not contain string 'boo'.

If you had this option turned on, bash would put the erroneous ^baz^test^ event designator back on the command line as if you had typed it again.

Finally, option 'histverify' allows users to verify a substituted history expansion.

Based on the previous example, suppose you wanted to execute that 'echo' command again by using the '!!' event designator. If you had this option on, bash would not execute the 'echo' command immediately but would first put it on command line so that you could see if it had made the correct substitution.

Tuning the Command Prompt

Here is how my command prompt looks:

Wed Jan 30@07:07:03
pkrumins@catonmat:1002:2:~$

The first line displays the date and time the command prompt was displayed so I could keep track of commands back in time.
The second line displays username, hostname, global history number and current command number.

The global history number allows me to quickly use event designators.

My PS1, primary prompt display variable looks like this:

PS1='\d@\t\n\u@\h:\!:\#:\w$ '

Bash History Cheat Sheet

Here is a summary cheat sheet for working effectively with bash history.

This cheat sheet includes:

  • History editing keyboard shortcuts (emacs and vi mode),
  • History expansion summary - event designators, word designators and modifiers,
  • Shell variables and `shopt' options to modify history behavior,
  • Examples

Download Bash History Summary Sheet

PDF format (.pdf):
Download link: bash history cheat sheet (.pdf)
Downloaded: 186181 times

ASCII .txt format:
Download link: bash history cheat sheet (.txt)
Downloaded: 17275 times

LaTeX format (.tex):
Download link: bash history cheat sheet (.tex)
Downloaded: 6906 times

This cheat sheet is released under GNU Free Document License.

Are there any tips you want to add?

bash readline vi editing mode default keyboard shortcut cheat sheet Bash provides two modes for command line editing - emacs and vi. Emacs editing mode is the default and I already wrote an article and created a cheat sheet for this mode.

This time I am going to introduce you to bash's vi editing mode and give out a detailed cheat sheet with the default keyboard mappings for this mode.

The difference between the two modes is what command each key combination (or key) gets bound to. You may inspect your current keyboard mappings with bash's built in bind command:

$ bind -P

abort can be found on "\C-g", "\C-x\C-g", "\M-\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
...

To get into the vi editing mode type

 
$ set -o vi

in your bash shell (to switch back to emacs editing mode, type set -o emacs).

If you are used to a vi text editor you will feel yourself at home.

The editing happens in two modes - command mode and insert mode. In insert mode everything you type gets output to the terminal, but in the command mode the keys are used for various commands.

Here are a few examples with screenshots to illustrate the vi editing mode.

Let '[i]' be the position of cursor in insert mode in all the examples and '[c]' be the position of cursor in command mode.

Examples:

Once you have changed the readline editing mode to vi (by typing set -o vi), you will be working in insert mode.

The example will be performed on this command:

$ echo arg1 arg2 arg3 arg4[i]

Example 1:

Suppose you have typed a command with a few arguments and want to insert another argument before an argument which is three words backward.

$ echo arg1 (want to insert arg5 here) arg2 arg3 arg4[i]

Hit 'ESC' to switch to command mode and press '3' followed by 'B':

$ echo arg1 [c]arg2 arg3 arg4

Alternatively you could have hit 'B' three times: 'BBB'.

Now, enter insert mode by hitting 'i' and type 'arg5 '

$ echo arg1 arg5 [i]arg2 arg3 arg4

Example 2:

Suppose you wanted to change arg2 to arg5:

$ echo arg1 [c]arg2 arg3 arg4

To do this, you can type 'cw' which means 'change word' and just type out 'arg5':

$ echo arg1 arg5[c] arg3 arg4

Or even quicker, you can type 'f2r5', where 'f2' moves the cursor right to next occurrence of character '2' and 'r5' replaces the character under the cursor with character '5'.

Example 3:

Suppose you typed a longer command and you noticed that you had made several mistakes, and wanted to do the correction in the vi editor itself. You can type 'v' to edit the command in the editor and not on the command line!

Example 4:

Suppose you typed a long command and remembered that you had to execute another one before it. No need to erase the current command! You can switch to command mode by hitting ESC and then type '#' which will send the current command as a comment in the command history. After you type the command you had forgotten, you may go two commands back in history by typing 'kk' (or '2k'), erase the '#' character which was appended as a comment and execute the command, this makes the whole command look like 'ESC 2k0x ENTER'.

These are really basic examples, and it doesn't get much more complex than this. You should check out the cheat sheet for other tips and examples, and try them out!

To create the cheat sheet, I downloaded bash-2.05b source code and scanned through lib/readline/vi_keymap.c source code file and lib/readline/vi_mode.c to find all the default key bindings.

It turned out that the commands documented in vi_keymap.c were all documented in man 3 readline and I didn't find anything new.

After that I checked bashline.c source file function initialize_readline to find how the default keyboard shortcuts were changed. I found that 'CTRL-e' (which switched from vi mode to emacs) got undefined, 'v' got defined which opens the existing command in the editor, and '@' which replaces a macro key (char) with the corresponding string.

The cheat sheet includes:

  • Commands for entering input mode,
  • Basic movement commands,
  • Character finding commands,
  • Character finding commands,
  • Deletion commands,
  • Undo, redo and copy/paste commands,
  • Commands for history manipulation,
  • Completion commands,
  • A few misc. commands, and
  • Tips and examples

Download Vi Editing Mode Cheat Sheet

PDF format (.pdf):
Download link: bash vi editing mode cheat sheet (.pdf)
Downloaded: 111358 times

ASCII .txt format:
Download link: bash vi editing mode cheat sheet (.txt)
Downloaded: 21546 times

LaTeX format (.tex):
Download link: bash vi editing mode cheat sheet (latex .tex)
Downloaded: 5827 times

This cheat sheet is released under GNU Free Document License.

Do you want to have a broader discussion on this topic?
Discuss it on catonmat forums!

charles darvin - natural selectionI have started working on my bachelor's thesis in physics. It's about using genetic algorithms for finding optimal solutions to physics problems. One of the problems I will solving is simulating equilibrium configurations of two dimensional systems of particles which can attract and repel (dipole systems). This problem is NP hard, which means there is no effective algorithm to find the exact solution in an adequate time. Several other algorithms can be used to approximate the solution and find near-equilibrium configurations, one of them being genetic algorithm technique.

The easiest situation is when the particles are bounded in a circle which border they can't trespass. The goal is to find how these particles will position themselves inside this circle.

This case has already been tackled using the simulated annealing method and the near-optimum solutions for hundreds of particles have been found. This method uses different principles than genetic algorithm method which I will describe shortly. The main idea of simulated annealing method is that the system is given an initial temperature which basically controls how much the particles will fluctuate inside the system. The greater the temperature, the bigger the fluctuations. The temperature gradually gets decreased and the fluctuations get smaller and smaller. Calculating the energy of the system and requiring it to be minimal at each step the temperature gets decreased eventually leads to a solution.

Here is how the solutions for 10, 11, 12, 13, 14 and 15 particles looks like, found using simulated annealing algorithms:

dipole system

Notice how interestingly and non-intuitively the particles position themselves in a case when another particle gets added to a system of 12 particles. Instead of positioning the new particle somewhere in the middle as in transition from 11 particles to 12, the system decides to put it on the border.

In my thesis I will be using genetic algorithms to arrive at the solution for this problem.

Here is a general (not related to my topic of thesis) description of what genetic algorithms are.

Genetic Algorithms 101

Genetic algorithms mimic the evolution by natural selection. The basic idea of genetic algorithms is very simple. Genetic algorithms feature populations of individuals which evolve with the use of the principles of selection, variation and inheritance.

One of the ways to implement this idea in computer programs is to represent individuals as strings of binary digits. Each bit in the string represents one gene. Each individual is assigned a numerical evaluation of its merit by a fitness function. The fitness function determines how each gene of an individual will be interpreted and, thus, what specific problem the population will evolve to solve.

Once all individuals in the population have been evaluated, their fitness values are used for selection. Individuals with low fitness get eliminated and the strongest get selected. Inheritance is implemented by making multiple copies of high-fitness individuals. The high-fitness individuals get mutated and they crossover to produce a new population of individuals. Mutation is implemented as flipping individual bits in the binary string representation of an individual and crossover happens as an exchange of binary substrings of two individuals to obtain a new offspring.

By transforming the previous set of individuals to a new one, the algorithm generates a new set of individuals that have better fitness than the previous set of individuals. When the transformations are applied over and over again, the individuals in the population tend to represent improved solutions to whatever problem was posed in the fitness function.

Here is an illustration how a genetic algorithm might work:

operation of the genetic algorithm programming

I look forward to finding what other interesting projects that I can make using the very useful information I have learnt.

PS. I am writing blog posts less often now because I am really, really busy with studies. Sorry about that.

I was doing a WordPress installation the other day when I noticed how insecure the default generated password was.

On line 38 in wp-admin/includes/upgrade.php (wordpress version 2.3.1) I found that a 6 character password is generated this way:

$random_password = substr(md5(uniqid(microtime())), 0, 6);

The md5 function returns a 32 character hexadecimal number and substr chops off first six characters. Doing elementary combinatorics we can find that the number of possible passwords is 166 (16 to the power 6) or 16,777,216, or roughly just 16.7 million passwords!

I am more than sure that most people doing WP installations never change the default password. If you're on a good connection and can do just 100 password checks per second, then you can crack a WordPress installation in worst case time of 16,777,216/100 seconds, which is 46.6 hours! Most likely you'd crack the password in half of that time, so you can crack any WordPress installation that has a default password in about 24 hours!