Algebraic Chess Notation

We use algebraic chess notation to represent chess positions without posting a full chessboard. This allows players to converse about chess positions clearly without a board in front of us.
Imagine the chess board as a 2D plot. Below is the table I made to explain this notation in my paper How Stockfish Works: An Evaluation of the Databases Behind the Top Open-Source Chess Engine. I will post more about this paper when I’ve finished editing it.

Symbol Meaning
a-h file from white’s left to right
1-8 rank from white to black
R, N, B/S, Q, K Rook, Knight, Bishop, Queen, King
x capture; the piece that was at this location is removed
+ a note that the king is threatened
# or ++ checkmate; a note that this is the reason for the end of the game
= promoted to; a pawn arriving at the opposite side of the board is promoted to another piece, often a queen.
0-0 castle on the kings side; move to positions (B – Kg8 Rf8 ; W – Kf1 Rg1) (if neither has moved before this point in the game)
0-0-0 castle on the queens side; (B – Kd8 Rc8 ; W – Kc1 Rd1) (if neither has moved before this point in the game)
e.p. en passant capture (non-SAN), a note that a pawn was taken by another pawn passing it. When a pawns first move is a two space move (from 7 to 5 for black or 2 to 4 for white) it can be captured by moving behind it to the 6th rank (white taking black) or 3rd rank (black taking white).
?, ??, !, !! editorial comments, weak, very weak, strong, very strong
Coordinates in Algebraic Chess Notation
I encourage you to explore additional background information provided by the online book Building Skills in Python.

Perl is a Beautiful Thing (Replace All Instances of a String in a Directory)

Perl’s string handling is a beautiful thing. I’ll talk about grep in a later post.

Let’s say you have a directory on your computer which is filled with over 1000 text/source-code documents. You want to replace all instances of a variable name, “find_var” with “replace_var”, in all files within this directory. There are a couple ways you can do this.

0. You could open each document and Ctrl+F to find each instance of the “find_var” (within the document) and manually replace it with “replace_var” Gross, several hours of tedious work.

1. You could open each document and Ctrl+H to replace all instances of “find_var” (within the document) with “replace_var” Better, but still gross & hours of work.

2. Use Perl, which replaces all instances of “find_var” (within the entire directory -subdirectories included) with “replace var” in one line. Pythonic! Or, I suppose, Perltastic/Perly…. seconds of work.

In this example, assuming the desired directory’s path is “Dropbox/desireddirectory”, you open the terminal and have two options.

Case1: If you wish to simply replace all instances of “find_var” with “replace_var”, use the following command:

perl -e “s/find_var/replace_var/g;” -pi $(find ~/Dropbox/desireddirectory -type f)

Case2: If you wish to keep a copy of the original files, use the following command:

perl -e “s/find_var/replace_var/g;” -pi.save $(find ~/Dropbox/desireddirectory -type f)

This will save back-ups of the originals of each file, attaching “.save” to their titles. Thus, doc.txt will have “replace_var”, and doc.txt.save will be a back-up of of the original “find_var”.

I Write Poetry in Python

I enjoy songwriting. Here are the lyrics to a silly, fun song I wrote a couple of months ago.

I Write Poetry in Python by Catherine Ray

I have no life and I can prove it mathematically.
I judge your human worth using a spectrum of rationality
I know vector calculus, but I can’t remember how to do long division.
When I compile, my waiting ritual resembles superstition.
I know how to integrate a chicken and take the derivative of water,
I had to pick just one, Asimov ‘s my favorite author.
And I… I write poetry in Python.

You put the period outside the quotes! You’re not quoting the end of the sentence
What the heck do english majors know, anyway. Correction fail, I demand repentance!
I’ve gutted and rebuilt my laptop 5 times since I last changed the oil in my crappy car.
Yet, I’ll bet you I can’t name a single television star.
‘Cause I, I write poetry in Python.

I’ve got to bring a jacket with me, in the middle of summer,
’cause there’s a wind-chill factor in the lab, my priority is comfort
The three most dangerous things in the world are a programmer with a soldering iron,
a hardware engineer with a software patch,
and a user with an idea.
So, I, I just write poetry in Python.

Find the Fractional Form of a Repeating Decimal

I think math shortcuts and tricks are groovy. Here’s an arithmetic trick to find the fractional form of a repeating decimal.

Generally speaking, if the repeating decimal has a as the repetend, then the fraction that is represented by that repeating decimal is just a/Z where Z is a number with the same number of digits as a, but all the digits are 9’s.

For example, the let’s find the fractional form of 0.567567…

a = 567 => 0.567567… = 567/999 (= 21/37 after reduction)

Doctor Who Poems

I was writing a letter to my grandma. In the letter, I included a poem. My Uncle asked if I would send him more poems.

I asked him for a topic and promised to compose a poem for him. My Uncle requested Dr. Who and the following two “poems” resulted:

Alone by Catherine Ray

Alone in the night
A fiery man
burns,
through time
through space,
in step
with the scrape of an emergency brake.
The sound of adventure.
The sound of destruction.
The sound of the universe.
The sound of him.

TARDIS by Catherine Ray

Out from
the churning blue-black
its corners protrude.
From the box in the sky,
light falls forward.
A looking glass
into
the unaging intellect
whistling inside.

CAMEL paper

I used Braille as a test language, but this is a framework to automate the decoding of any partially understood (ancient) language by creating probabilistic dictionaries.

I began working on CAMEL (Contextual Machine Learning Through the Analysis and Chunking of Partially Translated Grade 2 Braille) when I saw this atrocity: the Braille below translates to “STAIRWELL”
stairwellmis
Most sighted people don’t know Braille (to my dismay- the grammar is beautiful), this gave me the idea to write an optical Braille reader app for the sighted: the user holds their Android camera up to a sign and it automatically translates the Braille into English.

As I sat down to code this, I realized that I’d have to hard code a dictionary of Grade 2 Braille (a grammatically complex language). I have a deep disgust for hard coding \(\Rightarrow\) CAMEL is the program I wrote to automate the creation of a Grade 2 Braille dictionary. All code used in this project is on github.

This program is based on contextual machine learning, so I named my project CAMEL (ContextuAl MachinE Learning). The title of my paper is a mouthful, because I’m unsure of how to shorten it while maintaining clarity: CAMEL

Pythonic Method Calling

I have an aversion to hard-coding. Hard coding is when you write out a long, elaborate code that could also be written with a dynamic loop. This usually limits the ability to easily adjust your own code in case you want to change something later (or re-use it). “Hard coding” refers to “rigidly” writing out things instead of keeping them dynamic.

This practice is frowned upon by most of the programmers I’ve met (especially the Python programmers). In Python, code is ‘pythonic’ if it is well-written and concise. This link is to an excellent article on ‘What is Pythonic?’, I highly recommend reading this for a more in-depth explanation of the term: http://blog.startifact.com/posts/older/what-is-pythonic.html

Today, while I was revising my Prime Number Algorithm program (an earlier post), I figured out how to pass functions as strings. The eval function allows me to run python code within itself. This definition of eval is vague and unhelpful.

Let me show you to make a calling method, ‘execute,’ pythonic in order to explain the purpose of the eval function.

Let’s assume that we want a program that executes methods p1(n), p2(n), …, p5(n) given some string n. I will refer to these methods collectively as ‘p’-methods.

We could hard-code this as such:

class Test():
    def execute(self, n):
        self.p1(n)
        self.p2(n)
        self.p3(n)
        self.p4(n)
        self.p5(n)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.execute(‘ World!’)

The above program will run all 5 ‘p’-methods and print the appropriate strings. However, what if there were 1000 different ‘p’-methods we needed to run? Must we type out each method in order to call these ‘p’-methods within the execute method?

At first, I created a forloop to iteratively call execute, providing a new method each time. If you run the code below, execute will treat the ‘method’ parameter as a String type (instead of a function) and throws an error.

(NON-FUNCTIONAL)

class Test():
    def execute(self, method_name):
        self.method_name(‘ World!’)   

    def send_method(self):
        for version in xrange(1,6):
            version = str(version) #run each ‘p’-method version = 1, 2, 3, 4
            method = ‘self.’+’p’+version #note that type(method == String
            self.execute(method)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.send_method()

We are so very close to having a pythonic solution. In order to fix this error, we must make the String into a method call. With one extra line, we evaluate the String input (‘method_name’) as a function and run all ‘p’-methods! The final version of the execute method is:

class Test():
    def execute(self, method_name):
        method = eval(method_name)
        methodrun = method(‘ World!’)   

    def send_method(self):
        for version in xrange(1,5):
            version = str(version) #test each version of the algorithm, version = 1, 2, 3, 4
            method = ‘self.’+’p’+version
            self.execute(method)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.send_method()

I hope this gives you an idea of: why hard-coding is bad, what ‘pythonic’ means, how ‘eval’ comes in handy and what I do in my free time.

Listing Programming Languages on Your Resume

I love programming and know a few programming languages. Every programmer I’ve met has a favorite language, however, most of us can code in more than one language. As I was revising the ‘Language’ portion of my resume, I became confused.

Should you mention all the languages you can code in?

What if they expect you to be practiced in (insert language you haven’t used in the last 3 months) and you fail the interview?

Should you be safe and only list the languages you are fluent in?

How do you simply represent skill level in languages?

I decided to divide my knowledge of languages into “proficient” and “familiar.” Here is a guide on how to sort your languages into these two categories.

If you are “proficient” at a programming language, you can sit down and code (without consulting the internet or a book) and produce a functional program. You can write efficient, concise code in this language. If given source code, you can optimize it.

If you are “familiar” at a programming language, you’ve coded at least 5 functional programs in this. You can code a simple program with no bugs. If given internet access, you can produce a complex program. If given source code in this language, you can understand what it does and check for errors. You will likely think “I know how to do this in (insert proficient language here)! How do I do this in (insert familiar language here)?” over 3 times while coding a simple program.

For example, my ‘Language’ section currently looks like this:

(I added my knowledge of Braille and Morse Code because it amuses me to present all types of languages in my language section.)