So I have recently started programming in Python and I'm still trying to think in the latter. I have a small project for University where I have 6 problems based on a list of lists. My teacher calls it an array. I dont know if this is the right place to ask for some help, but here I go: I've got the first two alright but then I got stuck.

First of all, I had to calculate the Euclidean distance between two points: Each 'cell' of the list of lists represents a square with 20m on the side, which represents a certain altitude.

Altitude = [ [206,205,204,190,208],
             [196,196,195,200,193] ]

from numpy import sqrt
def distance(altitude, side, p1, p2):
    r1 = p1[0]
    r2 = p2[0]
    c1 = p1[1]
    c2 = p2[1]
    w = side
    a1 = altitudes [p1[0]][p1[1]]
    a2= altitudes [p2[0]][p2[1]]
    d =sqrt((w*(r1 - r2))**2+ (w*(c1-c2))**2 + (a1-a2)**2)
    return d

Then I had to create a function to calculate the distance when it was given a certain path of 'cells':

def traveled_distance (altitude, side, path):
    dist = 0
    for v in range(len(path)-1):
            dist = dist + distance(altitude, side, path[v], path[v+1])
    return dist

This is where I started getting stuck. The next exercise consists in calculating the 'cell' in which the path will stop when travelling north, from a starting point. Note that one can only travel to a cell with higher value in altitude.

I tried a 'for loop' using if and elif, but couldn't write a working code.

def climb_north (altitude, start_point):
    alt_north = 0
    for a in altitude (...)

The 4th part of the work consists in implementing a function called:

def neighbours(altitude, cell):

which returns a list of tuples with the coordinates (row and column) of neighbor cells of the variable --- cell ---which is also a tuple. The neighbour cells are located S, N, W, E, NE, NW, SW and SE.

The 5th problem is about the neighbour cell with the higher altitude value. The function should be called:

def higher_neighbour(altitude, cell):

If the cell has a bigger value than its neighbours it should return its own coordinates; If the cell has more than one neighbour with the same higher altitude, it can return any of those coordinates.

In the last exercise we are told to write the code of a function

def climb_higher (altitude, cell)

where given a certain 'cell' it returns a list of tuples with the row and column of each visited point, so that each cell moves to a neighbouring cell with a higher altitude, until it reaches a peak, where that cell is the highest between its neighbours

0 Answers