The reshuffle did not pass

I wrote some code to implement the algorithm I found on string permutations. I have an arraylist of words (up to 200) and I need to permute a list at 5 levels. Basically group strings in 5 and rearrange them. That I take the first 5 words, generate permutations, and ignore the rest of the arraylist? Any ideas appreciated.

Private Function permute(ByVal chunks As ArrayList, ByVal k As Long) As ArrayList
        ReDim ItemUsed(k)
        pno = 0
        Permutate(k, 1)

        Return chunks
    End Function


    Private Shared Sub Permutate(ByVal K As Long, ByVal pLevel As Long)

        Dim i As Long, Perm As String
        Perm = pString ' Save the current Perm
        ' for each value currently available


        For i = 1 To K

            If Not ItemUsed(i) Then
                If pLevel = 1 Then
                    pString = chunks.Item(i)
                    'pString = inChars(i)
                Else
                    pString = pString & chunks.Item(i)
                    'pString += inChars(i)
                End If
                If pLevel = K Then 'got next Perm
                    pno = pno + 1
                    SyncLock outfile
                        outfile.WriteLine(pno & " = " & pString & vbCrLf)
                    End SyncLock
                    outfile.Flush()
                    Exit Sub
                End If
                ' Mark this item unavailable
                ItemUsed(i) = True
                ' gen all Perms at next level
                Permutate(K, pLevel + 1)
                ' Mark this item free again
                ItemUsed(i) = False
                ' Restore the current Perm 
                pString = Perm
            End If
        Next

      

K above = = 5 for the number of words in one permutation, but when I change the for loop to the size of the arraylist, I get an out of bounds index error

+2


source to share


3 answers


An out-of-error index error usually occurs when starting a loop from 1 to length. The for loop is as follows.




    For i  = 0 to array.length - 1

      

0


source


You will receive this error.

When you do

For i = 1 K K

The last value i will be the size of your array.

chunks.Item (i)



It crashes when I will be equal to the size of the array as the index starts at 0.

I suggest you change your for loop to

When i = 0 To K - 1

Or would you change the way you access the values ​​in your arrays in

chunks.Item (I-1)

0


source


C ++ rearrangement

#include <stdio.h>

void print(const int *v, const int size)
{
  if (v != 0) 
  {
    for (int i = 0; i < size; i++) 
    {
      printf("%4d", v[i] );
    }
    printf("\n");
  }
} // print


void permute(int *v, const int start, const int n)
{  
  if (start == n-1) {
    print(v, n);
  }
  else {
    for (int i = start; i < n; i++) {
      int tmp = v[i];

      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}


main()
{
  int v[] = {1, 2, 3, 4};
  permute(v, 0, sizeof(v)/sizeof(int));
}

      

0


source







All Articles