donnist
(Don)
February 5, 2020, 6:53pm
1
So I have a array of 6 numbers and I need to append an asterisk to set of three number that's generated in the loop. But only the second set numbers. It loops to 25 then starts back at 1.
For example: [1, 2, 3, 4, 5, 6] [4*, 5*, 6*, 7, 8, 9] [7*, 8*, 9*, 10, 11, 12] [10*, 11*, 12*, 13, 14, 15] [...]
Here is my code for generating the loop but I don't know how to append the asterisk.
let original = [1, 2, 3, 4, 5, 6]
var currentArray = original
print("currentArray =", currentArray)
var stop = false
repeat {
currentArray = currentArray.map() { ($0 + 2) % 25 + 1}
print("currentArray =", currentArray)
stop = currentArray[0] == 25
} while !stop
Lantua
February 5, 2020, 6:59pm
2
The formatted op's code
let original = [1, 2, 3, 4, 5, 6]
var currentArray = original
print("currentArray =", currentArray)
var stop = false
repeat {
currentArray = currentArray.map() { ($0 + 2) % 25 + 1 }
print("currentArray =", currentArray)
stop = currentArray[0] == 25
} while !stop
You can not append the asterisk, because currentArray
is an array of Int
, and 4*
is not a valid Int
.
If all you care is the printed output, you can expand the printing logic, or create a new array of String
, and append asterisk manually.
donnist
(Don)
February 5, 2020, 7:01pm
3
Yes, I need it appended to the output. Just what's printed out.
Lantua
February 5, 2020, 9:00pm
4
The easiest way is probably to have custom logic for printing.
Printing array of String
will give you ["1*", "2*", "3*", "4", "5", "6"]
. You probably don't want the double quote.
You could try to come up with the logic yourself first since there are many ways to do it, and might be the point of the exercise.
In any case, this is one way you can do:
var current = [1, 2, 3, 4, 5, 6]
while current[0] != 3 {
print("currentArray = [\(current.first!)*", terminator: "")
var starCount = 2
for value in current.dropFirst() {
if starCount > 0 {
// There's some stars left. Print it out.
print(", \(value)*", terminator: "")
starCount -= 1
} else {
print(", \(value)", terminator: "")
}
}
print("]")
current = current.map() { ($0 + 2) % 25 + 1 }
}
donnist
(Don)
February 12, 2020, 4:56pm
5
Awesome!!!
Thats it!
Thank you MUCH!!!