locked
for loop position rotation RRS feed

  • Question

  • User-1279294283 posted

    Does anyone know how to create this sequence using a for loop and/or nested for loops? I'm having trouble. Thanks

    1 2 3 4 5 6 7 8 9 10 11
    2 3 4 5 6 7 8 9 10 11 1
    3 4 5 6 7 8 9 10 11 1 2
    4 5 6 7 8 9 10 11 1 2 3
    5 6 7 8 9 10 11 1 2 3 4
    6 7 8 9 10 11 1 2 3 4 5
    7 8 9 10 11 1 2 3 4 5 6
    8 9 10 11 1 2 3 4 5 6 7
    9 10 11 1 2 3 4 5 6 7 8
    10 11 1 2 3 4 5 6 7 8 9
    11 1 2 3 4 5 6 7 8 9 10
    Friday, November 4, 2011 2:25 PM

Answers

  • User-952121411 posted

    Yep here it is:

            Dim MyIntList As New List(Of Integer)
    
            Dim x As Integer = 1
            For j As Integer = 1 To 11
                For i As Integer = x To 11
                    MyIntList.Add(i)
                    If (x > 1) And (i = 11) Then i = 0
                    If (x > 1) And (i = (x - 1)) Then Exit For
                Next
                x += 1
            Next

    An interview or logic question by chance. Wink I didn't recognize a number pattern I am familiar with (which is almost none) so if that is a know pattern they may be a mathematically better way to solve this problem. There is probably other ways to refine the code, but what I provided you will spit out those 121 numbers in the exact order you provided.

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2011 2:47 PM

All replies

  • User-952121411 posted

    Yep here it is:

            Dim MyIntList As New List(Of Integer)
    
            Dim x As Integer = 1
            For j As Integer = 1 To 11
                For i As Integer = x To 11
                    MyIntList.Add(i)
                    If (x > 1) And (i = 11) Then i = 0
                    If (x > 1) And (i = (x - 1)) Then Exit For
                Next
                x += 1
            Next

    An interview or logic question by chance. Wink I didn't recognize a number pattern I am familiar with (which is almost none) so if that is a know pattern they may be a mathematically better way to solve this problem. There is probably other ways to refine the code, but what I provided you will spit out those 121 numbers in the exact order you provided.

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2011 2:47 PM
  • User-1279294283 posted

    Thanks atconway! It's for a employee rotation. each number represents a job postion in the rotation.

    Friday, November 4, 2011 4:23 PM