Array Noob ... In Need of an [Int] Array Expert

I have one issue that I can utilize in either of two way...


**Problem**

So... Im trying to get and array (set) of 6 integers to cycle (loop) itself from numbers 1 - 25, but when it reaches 25 start back at 1 and continue the loop. (pretty easy)


**Restraints/Condition**

The conditions are that I have to reuse the last three number of the first array and have those three start the next set of six integers.



Solution #1


the last three incremented by +1 then start the next 6:


[1, 2, 3, 4, 5, 6, ] [4, 5, 6, 7, 8, 9,] [7, 8, 9, 10, 11, 12,] [10, 11, 12, 13, 14, 15,] Etc...



Solution #2


Just have the block of arrays repeat itself to so that it appears to be incrementing:


[1, 2, 3, 4, 5, 6, ] [4, 5, 6, 7, 8, 9,] [7, 8, 9, 10, 11, 12,] [10, 11, 12, 13, 14, 15,] [13, 14, 15, 16, 17, 18,] [16, 17, 18, 19, 20, 21,]


[19, 20, 21, 22, 23, 24,] [22, 23, 24, 25, 1, 2,] .... until it repeats itself back to: [1, 2, 3, 4, 5, 6, ]



I dont even know how or where to start programmatically. Thanks for your help.

Accepted Reply

You don't tell what you want to do with arrays, so that's just code to show how to loop.


let original = [1,  2,  3,  4,  5,  6]
var currentArray = original
print("currentArray =", currentArray)   // Should do something more useful
var stop = false
repeat {
    currentArray = currentArray.map() { ($0 + 2) % 25 + 1}  // when $0 + 3 >= 26, we return to 1
    print("currentArray =", currentArray)   // Should do something more useful
    stop = currentArray[0] == 25    // We stop when the array starts with 25, just to have a stop condition
} while !stop


Gives:

currentArray = [1, 2, 3, 4, 5, 6]

currentArray = [4, 5, 6, 7, 8, 9]

currentArray = [7, 8, 9, 10, 11, 12]

currentArray = [10, 11, 12, 13, 14, 15]

currentArray = [13, 14, 15, 16, 17, 18]

currentArray = [16, 17, 18, 19, 20, 21]

currentArray = [19, 20, 21, 22, 23, 24]

currentArray = [22, 23, 24, 25, 1, 2]

currentArray = [25, 1, 2, 3, 4, 5]


If you want to stop after having cycled, just change the stop condition:


let original = [1,  2,  3,  4,  5,  6]
var currentArray = original
print("original =", original)   // Should do something more useful
var stop = false
repeat {
    print("currentArray =", currentArray)   // Should do something more useful
    currentArray = currentArray.map() { ($0 + 2) % 25 + 1}  // when $0 + 3 >= 26, we return to 1
    stop = currentArray == original    // We stop when the array starts with 1, which means we have looped back to original
} while !stop


yields:

original = [1, 2, 3, 4, 5, 6]

currentArray = [1, 2, 3, 4, 5, 6]

currentArray = [4, 5, 6, 7, 8, 9]

currentArray = [7, 8, 9, 10, 11, 12]

currentArray = [10, 11, 12, 13, 14, 15]

currentArray = [13, 14, 15, 16, 17, 18]

currentArray = [16, 17, 18, 19, 20, 21]

currentArray = [19, 20, 21, 22, 23, 24]

currentArray = [22, 23, 24, 25, 1, 2]

currentArray = [25, 1, 2, 3, 4, 5]

currentArray = [3, 4, 5, 6, 7, 8]

currentArray = [6, 7, 8, 9, 10, 11]

currentArray = [9, 10, 11, 12, 13, 14]

currentArray = [12, 13, 14, 15, 16, 17]

currentArray = [15, 16, 17, 18, 19, 20]

currentArray = [18, 19, 20, 21, 22, 23]

currentArray = [21, 22, 23, 24, 25, 1]

currentArray = [24, 25, 1, 2, 3, 4]

currentArray = [2, 3, 4, 5, 6, 7]

currentArray = [5, 6, 7, 8, 9, 10]

currentArray = [8, 9, 10, 11, 12, 13]

currentArray = [11, 12, 13, 14, 15, 16]

currentArray = [14, 15, 16, 17, 18, 19]

currentArray = [17, 18, 19, 20, 21, 22]

currentArray = [20, 21, 22, 23, 24, 25]

currentArray = [23, 24, 25, 1, 2, 3]

Replies

You don't tell what you want to do with arrays, so that's just code to show how to loop.


let original = [1,  2,  3,  4,  5,  6]
var currentArray = original
print("currentArray =", currentArray)   // Should do something more useful
var stop = false
repeat {
    currentArray = currentArray.map() { ($0 + 2) % 25 + 1}  // when $0 + 3 >= 26, we return to 1
    print("currentArray =", currentArray)   // Should do something more useful
    stop = currentArray[0] == 25    // We stop when the array starts with 25, just to have a stop condition
} while !stop


Gives:

currentArray = [1, 2, 3, 4, 5, 6]

currentArray = [4, 5, 6, 7, 8, 9]

currentArray = [7, 8, 9, 10, 11, 12]

currentArray = [10, 11, 12, 13, 14, 15]

currentArray = [13, 14, 15, 16, 17, 18]

currentArray = [16, 17, 18, 19, 20, 21]

currentArray = [19, 20, 21, 22, 23, 24]

currentArray = [22, 23, 24, 25, 1, 2]

currentArray = [25, 1, 2, 3, 4, 5]


If you want to stop after having cycled, just change the stop condition:


let original = [1,  2,  3,  4,  5,  6]
var currentArray = original
print("original =", original)   // Should do something more useful
var stop = false
repeat {
    print("currentArray =", currentArray)   // Should do something more useful
    currentArray = currentArray.map() { ($0 + 2) % 25 + 1}  // when $0 + 3 >= 26, we return to 1
    stop = currentArray == original    // We stop when the array starts with 1, which means we have looped back to original
} while !stop


yields:

original = [1, 2, 3, 4, 5, 6]

currentArray = [1, 2, 3, 4, 5, 6]

currentArray = [4, 5, 6, 7, 8, 9]

currentArray = [7, 8, 9, 10, 11, 12]

currentArray = [10, 11, 12, 13, 14, 15]

currentArray = [13, 14, 15, 16, 17, 18]

currentArray = [16, 17, 18, 19, 20, 21]

currentArray = [19, 20, 21, 22, 23, 24]

currentArray = [22, 23, 24, 25, 1, 2]

currentArray = [25, 1, 2, 3, 4, 5]

currentArray = [3, 4, 5, 6, 7, 8]

currentArray = [6, 7, 8, 9, 10, 11]

currentArray = [9, 10, 11, 12, 13, 14]

currentArray = [12, 13, 14, 15, 16, 17]

currentArray = [15, 16, 17, 18, 19, 20]

currentArray = [18, 19, 20, 21, 22, 23]

currentArray = [21, 22, 23, 24, 25, 1]

currentArray = [24, 25, 1, 2, 3, 4]

currentArray = [2, 3, 4, 5, 6, 7]

currentArray = [5, 6, 7, 8, 9, 10]

currentArray = [8, 9, 10, 11, 12, 13]

currentArray = [11, 12, 13, 14, 15, 16]

currentArray = [14, 15, 16, 17, 18, 19]

currentArray = [17, 18, 19, 20, 21, 22]

currentArray = [20, 21, 22, 23, 24, 25]

currentArray = [23, 24, 25, 1, 2, 3]

Claude31




WOW! I would have never have figured that one out. Its just a loop I need to run in the background for a playlist.


thanks a million!





Great, wish good continuation.

Don't forget to close the thread by marking the correct answer.