Swapping elements based on index given from another list

VHdk

Hello my task is to swap elements of list, with the index that needs to be swapped in another list, so fx:

If I have:

[3,1,2] as the list

And:

[[1,2],[2,3]] as the index that needs to be swapped

Then it should go like:

[1,2] = 3 and 1 getting swapped
[2,3] = 3 and 2 getting swapped

So i would end up with Output = [1,2,3]

The predicate is specified to be like this:

swap(C,Input,Output)

Where C is the list of which elemenst that should be swapped.

Input is the list that should be swapped.

Output is the swapped list.

I would like some advice on how to swapping those elements based on this, I have already looked at this: swap two elements from list with specified indices

Hope that someone can help me with this.

Edit:

So far I have tried something like this:

swap( Input,[I|J], Input ) :-
    I = J.
swap( Input, [I|J], Output ) :-
    swap( Input, [I|J], Output, _, _ ).
swap( Input, [I|J], Output ) :-
    swap( Input, J, I, Output, _, _ ).

swap( [E2|Ls], I,  0,  [E1|Ls], E1, E2 ):-!.
swap( [E1|Es], 0, J, [E2|Rs], E1, E2 ) :-
    N2 is J - 1,
    swap( Es, -1, N2, Rs, E1, E2 ),!.
swap( [E|Es], [I|J], [E|Rs], E1, E2 ) :-
    N1 is I - 1,
    N2 is J - 1,
    swap( Es, N1, N2, Rs, E1, E2 ). 

But I am only able to use "one" list as the indexes of what that has to be swapped, like [1,2], what I am looking for is being able to use multiple like [[1,2],[2,3]] and so on.

user27815

Using list_i_j_swapped/4

list_i_j_swapped(As,I,J,Cs) :-
 same_length(As,Cs),
 append(BeforeI,[AtI|PastI],As),
 append(BeforeI,[AtJ|PastI],Bs),
 append(BeforeJ,[AtJ|PastJ],Bs),
 append(BeforeJ,[AtI|PastJ],Cs),
 length(BeforeI,I),
 length(BeforeJ,J).

swap(List,[],List).
swap(List1,Swaps,ListSwapped):-
  Swaps =[[Index1,Index2]|T],
  list_i_j_swapped(List1,Index1,Index2,List2),
  swap(List2,T,ListSwapped).

Q:

?- swap([3,1,2],[[0,1],[1,2]],X).
X = [1, 2, 3] ;
false.

The positions are zero indexed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract vectors from a list based on given elements

Filter Elements from a list based on another list

Split a list based on index numbers given by another list

Append list every Nth index from another list and repeat based on the number of elements

Delete elements from python array with given index of elements as list

Am I swapping elements from a list correctly?

Finding the index of elements in an array/list based on another list or array

get some list element index based on another list elements

Extract from a list elements with indexes given in another list

Removing elements from a list by index from another list of integers

Filter elements from list based on True/False from another list

Retrieve lists from a list of lists based on the given first elements

Arranging a list based on the distance of its elements from a given value

Repeat elements in one list based on elements from another

Remove duplicates from a list and remove elements at same index in another list

Swapping linked list elements

swapping elements in list

Swapping elements in a list in Python

swapping elements of a given symbolic matrices

Python: Deleting a list element based on elements from another list

How to remove elements from a list with lambda based on another list

Remove certain elements in one list based on condition from another list

Summing up elements from one list based on indices in another list

Sum of specific elements of a list based on indexes from another list in python

Replacing elements of a list with those from another list based on name

Removing elements from a list based on indices in another list in Python

Is there another way to print multiple list elements where the list index increases by a given amount with each print?

Add a range of elements to a list at a given index, replacing the its contents from that index in the list and resizing it (if need it)

Replacing certain elements of an array based on a given index