Category Archives: python

developer python technical thoughts

Do you remember the Mackerel?

You might remember my recent post on writing a short python program to solve the Yakult problems on the tube
I said then that I didn't think it was the only or best way to solve the problem. I've spent a few minutes tonight improving it and have also made it so it takes a command line argument so it can be run from the shell

e.g
>> python yakult-problem.py mackerel

st. Johns wood

Not going to go into details her but if you are interested it is up on github

developer python technical thoughts

Solving the 'Yakult Problem' in Python

They say one of the best ways to learn something is to teach it,  I think they do, sure I have heard that somewhere, never sure who they are and  even if they don't I'm saying it now.  As I'm doing a proper course in learning Python I decided to write this short tutorial on one aspect of the python Language.

If you live in London you may have seen Posters advertising the Yakult Pro-bio-whatever drink stuff.  Not the faintest idea if its any good but I do like the posters with a short teaser on it.  Something similar to:

Yakult riddle

 

 

o.k Lets break the problem down.

1. load the file in

2. Go over (or Iterate over as we say in computer speak) all the station names

3.  Iterate over all the letters in the word that I am testing the  station names against.

4. Iterate over all the letters in the station name and compare it against the word being test

5 if  there is a match of letters , stop and move onto the next station, no point in carrying on

6 If no letter matches are found, store that station name for retrieval later.

Loading the File

It took a little while to find a list of all that station names in an easy to use format, but manages it after a bit of searching a messing. The stations are stored in a file called station_list.txt

#Create  a list to store the station names in 

station_list = []   

#open the file called stations_list.txt and store it in the object called stations_file

stations_file = open('./station_list.txt', 'r')  

#Read the stations in from a file
     for line in stations_file:

#Store all the stations in a list after converting the characters to lower case

station_list.append(line.lower())

stations_file.close()

I don't want to focus on the file reading as it is not core to this exerscise.  Converting the characters to lower case was something that was picked up in testing when I realised the the Upper case initial letters of the station names were not triggering matches against lower case letters in the chosen string.

Ok rather than going line by line it will make more sense if I add in the 3 iterations of step 2,3 and 4 then flesh those out with what happens in each one.

 

for station in station_list:

    for my_char in my_string:

        for station_char in station:

 

 

As described above the program iterates over the station_list.  'my_string' is then name of the variable that holds the string that is passed to the function when it is called, that is then iterated over and finally every character in the current station name is iterated over.

So the first thing to do is compare the current station character with the current character in my_string and if they match then stop the current loop

if station_char == my_char:

    break

 

Thats stops any more tests on that station name happening for that one particular letter in my_string, but carrying on testing my_string at all is wasteful and we haven't done anything to store the station name if none of the characters match.

 

 if my_char_count == len(my_string) -1 and not found_letter:

    result_stations_list.append(station)

 

You will see that these lines test a variable called  my_char_count against the length of  my_string and variable called found_letter .  This is finding out if the current character is the last one in the word and if no matches have been found.

If  that test is passed the station name is added to the list of  results with

     result_stations_list.append(station)

Finally the my_char_count is incremented and when all the iterations are compete the result_stations_list is returned from the function

 

    my_char_count +=1

 return result_stations_list

Because the station list are separated by a newline the list shows that as '\n' these can be removed if you like, but i'll leave that as an exercise for you to try.

 

That shows the basic workings of the program,  the full thing is on github have a look and see what you think.  I'm not saying this is the best way to solve this problem, am sure its not the only way.   But its the way I wanted to solve it using the Python language constructs we have been learning in class and hopefully it nails the workings of the language into my head.

For another take on the problem a solution in Java can be found on this blog

 

Oh and if you are interested 'St Johns Wood'