Search code examples
pythonpython-3.xbuilt-in-types

Range() function not working


I am trying to write the program that will be given a 2d list with coordinates of points on a grid (ex.[[4, 7], [5, 6], [5, 2]]), and will return all points passed during "walking". We can go diagonal.

def pole(lista):
    passed = []   #List with all points passed
    before = lista[0]  #list with coordinates before
    for i in range(1,len(lista)):
        first = [list(range(before[0],lista[i][0]))]   # Lists that should have all points
        second = [list(range(before[1],lista[i][1]))]  # passed from point to point

        if(len(first) == 1):                  #If we do not go diagonal, one list will only have one number here,
            first = [before[0]*len(second)]   # but we need the same number of itmes,
        if(len(second) == 1):                 # so we do not get IndexOutOFRange error in next for   
            second = [before[1]*len(first)]
        #print(first,second)
        for j in range(len(first)):
            passed.append([first[j], second[j]])
        before = lista[i]
    return passed

We are using the example list as an input. [[4, 7], [5, 6], [5, 2]]

The problem is that the output is wrong and I do not know why:

[[4, 7], [5, 6]]

And the output should be:

[[4,7], [5,6], [5,5], [5,4], [5,3], [5,2]]

I think it is a problem of range function


Solution

  • I think easiest way (presuming you've imported scipy) for going backwards is to say-> for i in scipy.linspace(10,8,3)