Full story

Posted on 06-06-2008 under Python

Kata Three was a thought exercise, so I’m moving on to Kata Four. This is a 3 part kata with first being reading temperature data from a file and outputting the day the smallest temperature spread. I didn’t use RegEx to parse the input lines (I know it’s really something I should learn), so I just checked the character offsets. The day was from character 0 - 3, the max temperature was from 6 - 7, and the min was from 12 - 13. Here’s what I came up with:

def get_smallest_spread(self):
day = 0
spread = 100

lines = self.file.readlines()
for line in lines:
if len(line) >= 14:
try:
max = int(line[6:8])
min = int(line[12:14])
if max - min < spread:
day = int(line[0:4])
spread = max - min
except:
continue

return day

The code is pretty straight forward. I just read the lines, extract the appropriate character and turn them into ints and compare them, and then keep track of the smallest spread.

The second part of the kata is to read a data file with soccer (they call it football, but I’m in the U.S.) team data and output the team with the smallest goals for and goals against differential.

def get_smallest_differential(self):
team = ”
spread = 100

lines = self.file.readlines()
for line in lines:
if len(line) >= 52:
try:
gf = int(line[43:45])
ga = int(line[50:52])
if abs(gf - ga) < spread:
team = line[7:21]
spread = abs(gf - ga)
except:
continue

return team.strip()

Not much to comment on, this was pretty much the same code except the offsets were different.

The final part of the kata is to refactor the code to get as much common code a possible. This was pretty easy since the only differences were the offsets to find the values to compare and the value to return. Here’s what I ended up with:

def get_smallest_spread2(self):
return int(self.generic_spread(6, 8, 12, 14, 0, 4))

def get_smallest_differential2(self):
return self.generic_spread(43, 45, 50, 52, 7, 21).strip()

def generic_spread(self, r1_start, r1_end, r2_start, r2_end, d_start, d_end):
value = ”
spread = 100

lines = self.file.readlines()
for line in lines:
if len(line) >= self.max_index(r1_end, r2_end, d_end):
try:
r1 = int(line[r1_start:r1_end])
r2 = int(line[r2_start:r2_end])
if abs(r1 - r2) < spread:
value = line[d_start:d_end]
spread = abs(r1 - r2)
except:
continue

return value

def max_index(self, v1, v2, v3):
if (v1 >= v2):
if (v1 > v3):
return v1
else:
return v3
return v1
elif (v2 >= v3):
return v2

return v3

Not exactly elegant, but definitely multipurpose. The max_index method doesn’t feel like the most elegant way to handle line length checking, but it’s not a hack so I was pragmatic and did what worked. Other than that I’m just doing the same string parsing and comparisons, just with the index being passed in.

That’s my take on kata four. I feel like working on the katas has done what I wanted them to do, which was to get me comfortable with the Python syntax and the flow when doing Python development. Going forward I’m going to be developing the Tetris knock-off and a web-based address book.

No comment yet

Don't be shy, express yourself ! ;)

Post your comments

Fill up the fields below. Email is required but won't be revealed to anyone. Some (simple) html is allowed. If you want to cite another comment use the "Quote" link located beside each author. Finally be nice or at least keep it polite. Thanks for posting.