This is my test post because I’m testing some things out.


And hello again.

I’ll start this off with an obvious truth. The location of if/elif statements is very important. Well of course they’re important, right?

Backstory: I was working on a problem from the MIT python course on a ‘rock, paper, scissors’ game. I could have gone the rout of simply naming each winning condition (i.e., if player1=rock and player2=paper or player1=paper and player2=rock or player1=scissors and player2=paper, etc. etc.), but I wanted to go with a formula instead to use a ‘>’ or ‘>’ to determine the winner.

So I set initial values for each player (value1 and value2), and set rock’s value at 3, scissors at 2 and paper at one. This way I could just say if value1>value2, print ‘player1 wins.’ I’d just need an else statement for if paper and rock were chosen (because paper beats rock, rock doesn’t beat paper, or rather, 1 is greater than 3 in that instance), and an else statement for if they tie.

Simple enough right? I hammered out my program and tested it. But it kept giving me the wrong winner for paper versus rock. I swapped my > and < signs around and messed with it for a while trying to figure out what was wrong.

As it turns out, the elif statement i had for rock and paper should have been at the TOP of the if-elif series. I had been calculating whether rock was greater than paper all along, which is always true, so my program never got to the elif player1 is rock and player2 is paper exception.

This was almost counterintuitive for me because when I program, I want to get the parts of the program written that do the most work (i.e, 3 is greater than 2 and 2 is greater than 1, so that would only leave one tiny else statement needed for 3 and 1). But in programming, this is backwards. You need those “small” exceptions up front, or your program will not work as you hope it will.

Yeah, it’s a pretty basic concept, but one I had to learn the hard way. Hopefully it’ll stick this time.

All told, I got a successful program (with comments!!!) out of it, but it took me a while longer than it should have. In short, here’s to learning.


Welcome back folks.

So I’ve been reading just about everything I can today about python. Thanks to another helpful comment, I found an MIT course on ‘a gentle introduction to programming with Python’ (or something similar). I’ve been reading that today and trying a few things out from it, mostly figuring out how the course is set up. I’ve also been trying the one from Stanford.

Another note, bought a new keyboard yesterday. The Logitech k800, a handy little keyboard that’s an improvement over the old Microsoft ARC I had (with no number pad, a disappointment).

I worked on the problem I originally posted about on the blog, the solving of a 4×4 grid when given a number at the end of each column and each row that the numbers in that column or row multiply out to. Thanks to a few tips I was able to get a better grasp on the use of arguments in my functions. Here’s what it was simplified down to: El paste-o. And here’s the code with no formatting:

#!/usr/bin/python
import math
vert = [162, 200, 147, 140]
nums = []
def isprime(vert):
for divby in range(1,10):
if vert%divby == 0:
nums.append(divby)
print nums

for num in vert:
isprime(num)

I had had something similar, but left out the last two lines, which is a big part of getting it to run correctly for each value in vert. Slowly gettin’ there.

Someone else had code that prints out the numbers only once. I think that code prints several lists out, but it’s a vast improvement over what I had before. I’m still trying to figure some algorithm for actually SOLVING the puzzle, though. No dice yet.

I also decided to pick up a book today. I have an e-copy of “How to Think Like a Computer Scientist: Python,” which is what the MIT course uses. I’ve made my way through that about half way. Now I have a much more comprehensive book by the title of “Beginning Python, From Novice To Professional,” by Magnus Lie Hetland.

Lastly, I think I’m going to start working on my windows7 box, as it has much better hardware than my laptop, and can handle having 20+ tabs open in chrome much better than the lappy.

Well, that’s the blog of the day. Hope somebody found something worth reading in there 🙂 Until tomorry.



Well hello again folks.

I’m very excited for my first blog post to have received so much attention! It made its way up to number 8 on the front page of Hacker News, which was, for me, just way awesome. I didn’t expect it to go that far, though i was hoping the folks there would find it at least partly interesting.

How’d it make it that far, you ask? Honestly, I have no clue. But it sho’ made me feel good!

I haven’t worked on coding much today. After receiving some feedback from the tech community, many said to find a class to follow along with so that I’d get the concepts rather than just the specifics of one language. So I turned to the Stanford CS106a online course and have been watching lectures on that today (also bought a new keyboard, the logitech k800). Now I’m debating whether to keep learning on my ubuntu-laptop or my windows-desktop. There are advantages to both. I suppose I could set up an SSH client and try to work that way, but I’m not sure. I may install ubuntu (or some other distro) on my desktop and just dual boot, but that doesn’t seem optimal either. My desktop is way more powerful than my laptop, and I may just end up doing stuff in a virtual machine there.

A few folks showed me a way to get my prime factorization functions to be less wieldy, so I’ll start working on that in just a bit. But first things first, to code on my desktop? or laptop? Oi. I’m thinking desktop because I really like using the mouse there, which means I’ll probably install a dualboot of ubuntu/windows7. I really like my new keyboard too 🙂


Welcome to my first post. 🙂

The backstory: I’m a journalist by trade, but have a lot of interest in computers. So i decided on learning my first programming language, and thought I’d blog about it so you folks can read about my journey. I figure if nothing else, it’ll remind you of some of the difficulties you faced when you were learning your first language, and if you don’t know one, maybe it’ll help you learn some!

I’ve taken up Python, as it seemed a good starting language. It’s really interesting so far and I like it a lot. I had considered Perl and Java, but settled on Python for some reason I don’t remember. Ruby might be my next effort (though that is some ways off, since I still have quite a bit to learn about Py).

So i’ve been actually learning the language for a few months now and am starting to get a grasp on the very basics of it, like string and integer manipulation. I still have a lot to learn, though.

The project I’m working on right now is a code golf challenge a friend came up with. Pic explains more, so look at it. The original problem is: given the empty 4×4 grid, determine which four numbers from 1 to 9 multiply out to the number on the far right, and the far bottom. 1 will only be used once in each column and in each row.

Image

I’ve gotten the program to determine the prime factors for the numbers on the right and on the bottom, but now the trouble is finding some algorithm to determine which number is possible to go where. I’ve been stuck on this part now for a couple days (but admittedly haven’t spend a whole lot of time trying to code after work). Hopefully i’ll get it soon!

This is the pastebin of what i have so far (The formatting there looks nicer .. still learning to navigate wordpress): The Pastey of Bins
Here’s what I have so far:

#!/usr/bin/python
import math

vert = (‘162’, ‘200’, ‘147’, ‘140’)
hor = (140, 150, 441, 72)
vert1 = 162
vert2 = 200
vert3 = 147
vert4 = 140
hor1 = 140
hor2 = 150
hor3 = 441
hor4 = 72

row1poss = []

def isprime():
print “row 1:”,
for divby in range(1,10):
if vert1%divby == 0:
row1poss.append(divby)
isprime()
print row1poss

row2poss = []

def isprime():
print “row 2:”,
for divby in range(1,10):
if vert2%divby == 0:
row2poss.append(divby)
isprime()
print row2poss

row3poss = []
def isprime():
print “row 3:”,
for divby in range(1,10):
if vert3%divby == 0:
row3poss.append(divby)
isprime()
print row3poss

row4poss = []

def isprime():
print “row 4:”,
for divby in range(1,10):
if vert4%divby == 0:
row4poss.append(divby)
isprime()
print row4poss

col1poss = []

def isprime():
print “column 1:”,
for divby in range(1,10):
if hor1%divby == 0:
col1poss.append(divby)
isprime()
print col1poss

col2poss = []

def isprime():
print “column 2:”,
for divby in range(1,10):
if hor2%divby == 0:
col2poss.append(divby)
isprime()
print col2poss

col3poss = []

def isprime():
print “column 3:”,
for divby in range(1,10):
if hor3%divby == 0:
col3poss.append(divby)
isprime()
print col3poss

col4poss = []

def isprime():
print “column 4:”,
for divby in range(1,10):
if hor4%divby == 0:
col4poss.append(divby)
isprime()
print col4poss

a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
p = 0

endresult = [[a, b, c, d, 162], [e, f, g, h, 200], [i, j, k, l, 147], [m, n, o, p, 140], [140, 150, 441, 72]]
print endresult

As you can see, it’s lengthy code, and I’m pretty sure it’s not at all as short as it could be. But I suppose that’s all part of the learning process. Once I get a functional program I can work on shortening it up and cleaning everything a bit. Until then, function is my first priority (no use in it being pretty if it doesn’t work, right?).

One last thing: i hope to blog at least once a day about my efforts. Hopefully you’ll find something interesting here. If not, it just means i’m a failure! 😛