Filtered Usage in GAP

257 Views Asked by At

if I have a list of elements

FV:= [[ 1, 2, 4 ], [ 2, 4 ], [ 3, 1, 2, 4 ], [ 5, 3, 1, 2, 4 ], [ 15, 16, 18 ], 
     [ 16, 18 ], [ 17, 15, 16, 18 ],[ 19, 17, 15, 16, 18 ], [ 29, 30, 32 ], 
     [ 30, 32 ], [ 31, 29, 30, 32 ], [ 33, 31, 29, 30, 32 ], [ 43, 44, 46 ],
     [ 44, 46 ], [ 45, 43, 44, 46 ], [ 47, 45, 43, 44, 46 ], [ 57, 58, 60 ],
     [ 58, 60 ], [ 59, 57, 58, 60 ],[ 61, 59, 57, 58, 60 ], [ 74, 75, 76, 77 ], 
     [ 75, 76, 77 ], [ 76, 77 ], [ 81, 82, 83, 84 ], [ 82, 83, 84 ],[ 83, 84 ], 
     [ 88, 89, 90, 91 ], [ 89, 90, 91 ], [ 90, 91 ], [ 95, 96, 97, 98 ],
     [ 96, 97, 98 ], [ 97, 98 ],[ 102, 103, 104, 105 ], [ 103, 104, 105 ], 
     [ 104, 105 ]]

and I want to select only those elements that are of length $2$ in this list, I am trying to use Filtered function like this

gap> Filtered(FV,n->Length(FV[n]=2));

but it doesn't work. How should I use this function operation to find the right answer as I want.

2

There are 2 best solutions below

1
On BEST ANSWER

However, the posted code can solve the problem swiftly; it might be done by another basic way as follows:

gap> e:=Elements(FV);;
     for i in [1..Size(FV)] do if Length(e[i])=2 then Print(e[i], "\n"); fi; od;

[ 2, 4 ]
[ 16, 18 ]
[ 30, 32 ]
[ 44, 46 ]
[ 58, 60 ]
[ 76, 77 ]
[ 83, 84 ]
[ 90, 91 ]
[ 97, 98 ]
[ 104, 105 ]
1
On

This command can work for it.

gap> Filtered(FV, u -> Length(u)=2);