[Tutor] How to run same lines of code in different order at runtime

Alan Gauld alan.gauld at btinternet.com
Fri Jul 10 02:29:27 CEST 2015


On 09/07/15 18:40, George wrote:

> and i am not looking for route solving problems but rather a peculiar
> problem of running different lines of code arbitarily.

I'm not sure what you mean by that.
What is arbitrary? What determines which lines are run?

> either start from any direction.  So i have to devise a way by which i
> can run all the posibilities to find the best route. so i have to run
> 4*3*3*3 runs to find out the shortest route.  My code is as below, any
> way i can achieve it in python without typing all the posibilites.

The normal way to deal with this kind of thing is to a) create
a function that does the work and b) make it data driven so
you can code the choices as data.

But i don;t pretend to know what your algorithm would look like
so I'll somply make some suggestions to tidy the code below in
the hope that they might fire some ideas.

> def GetShortestRoute(source:list,destination:City,recursion=0):
>      print("recursion",recursion)

recursion may not be the best solution for this since Pythons
recursion limit is not huge (1000 last time I looked) and you
could easily hit that with this type of problem.

>      #print(source)
>      assert type(source)==list,"Source must be a list of list"
>      assert len(source)>0,"Source must contain atleast 1 item"
>      assert type(source[0])==list,"Sub items of source must be a list"

Just an observation but you seem very concerned with typechecking. Thats 
not usually necessary in Python since it will check for incompatible 
types as you go and raise exceptions.

>      #found something
>      foundlist=[]
>      for miniroute in source:
>          if destination in miniroute:
>              print("found",destination," in ",miniroute,":", destination
> in miniroute)
>              foundlist.append(miniroute)
>          else:
>              print("Not found",destination," in ",miniroute,":",
> destination in miniroute)
>
>      shortestRoute=[None,9999999.9]
>      if len(foundlist)>0:

This is more usually written as

if foundlist:

>          for miniroute in foundlist:
>              distance=calculateRouteDistantce(miniroute)
>              if distance<shortestRoute[1]:
>                  shortestRoute[1]=distance
>                  shortestRoute[0]=miniroute
>          return shortestRoute

>      duplicatesource=source[:]
>      for route in source:
>          lastCity=route[len(route)-1]

It would be easierv to access the last element directly rather than looping:

lastCity = route[-1]

>      #the part i do not want to recode everytime to find the shortest route
>          if lastCity.NE!=None and
> isCityInRouteList(lastCity.NE,source)==False:
>              tmproute=route[:]
>              tmproute.append(lastCity.NE)
>              if tmproute not in duplicatesource:
>                  duplicatesource.append(tmproute)

You repeat the if clause each time so put it in a function.
I'm not sure what you would call it but it will look like:

def add_route(direction,source):
     node = getattr(lastCity,direction)
     if node and not isCityInRouteList(node,source):
        tmproute=route[:]
        tmproute.append(lastCity.NE)
        if tmproute not in duplicatesource:
           duplicatesource.append(tmproute)

And you can apply it by iterating over a list of 'directions'.

for dir in ['NE','NW','SE','SW']:
     add_route(dir,source)

>      return GetShortestRoute(duplicatesource,destination,recursion+1)

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list