[GAP Forum] Sublists of list

Tim Kohl tkohl at math.bu.edu
Thu Oct 2 17:11:57 BST 2014




Hello,

If I understand correctly, you want all sublists of sizes 2..(Size of list - 1) with 'wrapping' 
to include cases like [11,1] and [6,11,1] etc.

Here is some code which seems to work.

Note that your 'result' is missing a few cases which I think you meant to include.

gap> a:=[[1,2,4,6,11],[12,23,19,17,15]];
[ [ 1, 2, 4, 6, 11 ], [ 12, 23, 19, 17, 15 ] ]
gap> xa:=[];;xa[1]:=ShallowCopy(a[1]);;xa[2]:=ShallowCopy(a[2]);;xa[1][Size(a[1])+1]:=a[1][1];;xa[2][Size(a[2])+1]:=a[2][1];;

gap> na1:=Size(a[1]);;for slsize in [2..na1-1] do I:=[1..slsize]; for j in [0..na1-slsize+1] do Print(List(I,k->xa[1][k+j]),"\n");od;od;
[ 1, 2 ]
[ 2, 4 ]
[ 4, 6 ]
[ 6, 11 ]
[ 11, 1 ]
[ 1, 2, 4 ]
[ 2, 4, 6 ]
[ 4, 6, 11 ]
[ 6, 11, 1 ]
[ 1, 2, 4, 6 ]
[ 2, 4, 6, 11 ]
[ 4, 6, 11, 1 ]

gap> na2:=Size(a[2]);;for slsize in [2..na2-1] do I:=[1..slsize]; for j in [0..na2-slsize+1] do Print(List(I,k->xa[2][k+j]),"\n");od;od;
[ 12, 23 ]
[ 23, 19 ]
[ 19, 17 ]
[ 17, 15 ]
[ 15, 12 ]
[ 12, 23, 19 ]
[ 23, 19, 17 ]
[ 19, 17, 15 ]
[ 17, 15, 12 ]
[ 12, 23, 19, 17 ]
[ 23, 19, 17, 15 ]
[ 19, 17, 15, 12 ]

Hope this helps.

	-Tim Kohl
         Boston University



On Thu, 2 Oct 2014, Siddiqua Mazhar (PGR) wrote:

> Dear Sir/Madam,
> 
> My question is, if I have a list like
> 
> a:=[[1,2,4,6,11],[12,23,19,17,15]]
> 
> then how can I find the sublists of this list in GAP that are in the same order of the elements as it has been placed in the list. For instance I want to find the result in this form
> 
> result:= [1,2],[2,4],[4,6],[6,11],[11,1],[1,2,4],[2,4,6],[4,6,11],[6,11,1],[11,1,2],[1,2,4,6],[2,4,6,11],[12,23],[23,19],[19,17],[17,15],[15,12],[12,23,19],[23,19,17],[19,17,15],[17,15,12],[15,12,23][12,23,19,17],[23,19,17,15];
> 
> 
> Kind regards
> Siddiqua
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum
> 

-- 



More information about the Forum mailing list