Today is a good day to code

Teaching my 7 Year Old Daughter to Code (Crypto)

Posted: August 24th, 2011 | Author: | Filed under: Lifestyle, Parenting, Programming, Ruby, Teaching Coding, Uncategorized | Tags: , , , , | 1 Comment »

When I started teaching programming to my children, I thought starting with JavaScript was a good idea. I still think that JavaScript is one of the most important languages to learn early in a programming career.  It just doesn’t seem to be the right choice for teaching someone to program when they are 7.

The reason is likely not what one would naturally think, the code isn’t too opaque, and the syntax wasn’t much of a problem for her.  It was just so much work to get output.  With my son, we worked on a really simple Python program working on his hand-me-down OLPC, my daughter was upgraded to an Acer Aspire 1 for her birthday.

With the Python, I felt like we made more progress due to the availability of a REPL.  We were able to make changes to the core code that was solving the problem and see results on the command line quickly.  With the JavaScript, we had to create an HTML page, load it into it, create some sort of markup output, etc… It just wasn’t as clean an approach to programming.

I have been asked by many about using “kid friendly” programming languages.  I think the people working on those are doing good work, however there is nothing “kid unfriendly” about the languages that I use for programming as an adult.  I think that in general when educating our kids, we need to stop coddling them so much.  Creating an approximation of an already dumbed down environment to write software to drive machines will not help them.  Most of the kids that I have seen are already beyond logo and they don’t even know it.  What they seem to want to do are real world things, and there is no reason they can’t.

What I settled on was to use Ruby for the tasks.  It is a language that has a great REPL, and is easier to read.  It also has the benefit of having a sane shell input mechanism as well as not requiring a ton of objects to get started.

We discussed what she wanted to do, there were several things, all of them were deeply social, but the one we settled on, I thought was the easiest to implement.  I thought that encrypting messages to her friend where only she and her friend had the crypt key would be enlightening.

She agreed, so we started into coding it up.  First we ended up working through a few encryption techniques on paper, taking short messages, converting them into their character codes and then shifting them by adding the char codes of each letter of the crypt key to each letter of the message.

def encode msg,key
	coded_msg = ""
	msg.each_char do |letter|
		coded_msg = coded_msg + add_cipher(key,letter)
	end
	return coded_msg
end

def add_cipher cipher,letter
	code = ""
	cipher.each_char do |cl|
		code = code + pad(cl[0] + letter[0])
	end
	return code
end

def pad num

   length=num.to_s.length

   if length > 3 then
      num.to_s
   else
      padded_id='0' * (3-length)  + num.to_s
   end
   return padded_id
end

At first, she put the crypt key into the program, but we discussed that it would be a bad idea since anyone with the source code could then crack the message.  She then asked me how her friend would decode the message.  I told her that the only way was for her to create a “pre-shared key,” something that she told her friend verbally that they would both have to remember, only then could that key be used to decrypt the messages.

What we did was to create a multi step command line program to accept the key and then the message.  We haven’t gotten around to the demux yet, but here is the mux:

print "Enter the crypto key, or die!: "
key = $stdin.gets.chomp

print "Tell me what to do ( 1 for encode, 2 for decode ): "
op = $stdin.gets.chomp

if(op.to_i == 1)
	print "Enter message to encode: "
	message = $stdin.gets.chomp
	coded_message = encode(message, key)
	print "Here is your message: \n"
	print coded_message
else

end

The nice thing about all of this is that the code is approachable, and the execution path makes sense… this happens, then this happens, etc… she can easily understand the flow of this program. We had significant problems with the flow of a client web application.

One of the first things that my daughter noticed was that whenever you make the crypt key longer, the message gets a bit longer, and that the encrypted message was many times longer than the original.  So it is working, not only is she understanding programming, but basic cryptography as well.  The only thing I am concerned about now is what happens when she is encrypting her posts on the social media site du jour at 16 with quantum encryption techniques.  How will I ever crack her codes?


Teaching my 7 Year Old Daughter to Code (Mazes)

Posted: February 5th, 2011 | Author: | Filed under: Lifestyle, Parenting, Teaching Coding | No Comments »

Today we advanced in our lessons.  I started teaching my daughter how to code about a month ago.  I started when I was about 7, so I thought it was about high time I got her going.  I bought her an Acer Netbook a while back.  Windows 7 Starter is a bit weird, but it does what we need it to.

I started teaching her JavaScript / CSS / HTML because I wanted for her to be able to see her work immediately, and  UI work is usually best for that.  We went through some basic typing, data structures, and some general information like number systems, hexadecimal, etc… She stayed with me.  We did that for a few lessons.

I typically keep her for about 1 hour per session.  Sometimes she wants to go for longer, sometimes she loses interest after about 20 minutes.  I always let her go when her attention starts to waver.

Today we started on our real project.  The first few things we built were simple, a list of her favorite things, some colored blocks, drawing some lines with the canvas element, but the end goal is to make a maze game for her brother, who loves mazes and is 3.

At first her suggestion was to make mazes manually, but after we thought about it a bit more, she realized that she would need to build many, many mazes to keep her brother busy.  So we started to think through how we could make the computer create the mazes.

My Daughter's Maze Generation Algorithm

My Daughter's Maze Generation Algorithm

Some of the problems we thought through were firstly how the machine should visualize the maze.  We thought through 3D constructs, which were crazy complicated and I suggested we do those later, and eventually we settled on a battleship style construct.

The next thing we started was to define the rules of movement through the maze for the computer while it is creating the path from the beginning to the end.  She decided that the computer should choose an entry point and an end point, and after some discussion we decided that they should be on the outside of the grid.

Then we thought about whether or not we wanted the entry and exit to possibly be the same.  My wife suggested we play simon says for a bit since we were talking about instructions, which was very helpful.  Then we decided that the computer, and her little brother, couldn’t move diagonally, but we needed a way to write a rule that the computer would understand.  The number and letter thing she came up with toward the end of the hour.

Teaching her how to hack is going surprisingly well.  I do not think that I have an overly gifted daughter, but I think that teaching coding by writing code is the best way.  For defining a set of rules though, a piece of paper is always a good first step.

The ultimate difficulty is that she wants to put the game on the iPad, and eventually in the AppStore.  We’ll see how far we get with that.  I suspect that most of the iOS coding will be done by me ;-).  I am hoping that this helps other software engineer parents who want to figure out how to teach their kids the art.  I’ll try to chronicle my setbacks, as well as my successes in subsequent posts.