Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a list of tupples - Maze exercise

Status
Not open for further replies.

prologuser8

Programmer
May 6, 2016
1
0
0
PT
hello, I'm having problems to sort a list consisting of tuples, imagine a maze with lines x columns, I get an initial position in the form of tuple: (top row, starting column) and also get a final position (end line, final column) and still get a list composed of elements that are tuples of the form: (direction, row, column), the direction can be, up down, the list is composed of the possible movements. and my predicate must return a list with the same elements that receives but ordered according to two conditions:
1 order by distance to the end position
2 in case of the distance to the end position equal to 2 or more elements of the list I get, I have to order those who have the same distance according to the largest distance to the starting position, that is, the farther from the end position has elements first.
Note: the maximum list I get with possible movements may have 4 elements

Here is what I tried so far:

Code:
distance ((L1, C1), (L2, C2), Dist): - Dist is abs (L1-L2) + abs (C1-C2)

ordena_poss (Poss, Poss_ord, Inicial_position, Final_position): -
length (Poss, Lenght_of_Poss)
((Length_of_Poss = 0, size = 1) -> Poss_ord = Poss, Poss Poss_ord =)

% If lenght of Poss is 0 or 1 Poss_ordn = Poss (comment)

Poss - is the list you get with possible moves
eg [(c, 4, 4), (d, 5, 5)]

Inicial_position - is the place of departure of the maze. eg, (1, 6)
Final_position - is the place of arrival of the maze. eg (5, 6))

In the end result should be a Poss_ord list containing the same elements of Poss but ordered in the distance.

In this example when I call:
Code:
? - Ordena_poss ([(c, 4, 4), (d, 5, 5)], Poss_ord (1, 6), (5, 6)).

should give the result:
Code:
Poss_ord = [(d, 5, 5), (c, 4, 4)]

The code has now already works for when I get an empty or with only one element list.

If anyone has any idea how to solve this problem, I thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top