Decrement index in a loop after Swift C-style loops deprecated - swift

How would you express a decrementing indexed loop in Swift 3.0, where the syntax below is not valid any more?
for var index = 10 ; index > 0; index-=1{
print(index)
}
// 10 9 8 7 6 5 4 3 2 1

Here is an easier (and more Swifty) approach.
for i in (0 ..< 5).reversed() {
print(i) // 4,3,2,1,0
}
let array = ["a", "b", "c", "d", "e"]
for element in array.reversed() {
print(element) // e,d,c,b,a
}
array.reversed().forEach { print($0) } // e,d,c,b,a
print(Array(array.reversed())) // e,d,c,b,a

C-style loops with a fixed increment or decrement can be replaced
by stride():
for index in 10.stride(to: 0, by: -1) {
print(index)
}
// 10 9 8 7 6 5 4 3 2 1
Use stride(to: ...) or stride(through: ...) depending on whether
the last element should be included or not.
This is for Swift 2. The syntax changed (again) for Swift 3, see
this answer.

From swift 3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)
for index in stride(from: 10, to: 0, by: -1) {
print(index)
}
// You can also use stride condition like
// {Some Value}.stride(to: 0, by: -1)
// for index in 10.stride(to: 0, by: -1) { }

You can use stride method:
10.stride(through: 0, by: -1).forEach { print($0) }
or classic while loop.

If you still want to use this C-style loop, here is what you need:
let x = 10
infix operator ..> { associativity left }
func ..>(left: Int, right: Int) -> StrideTo<Int> {
return stride(from: left, to: right, by: -1)
}
for i in x..>0 {
print(i)
}

Related

Calling different extent of randomness of arc4random in Swift?

This might be rather stupid question. I would like to know if different nuances/extent of randomness would be possible using arc4random_uniform in Swift. Here's an example:
let number = arc4random_uniform(10) + 1
print(number)
In this case, a number will be printed randomly from 1 to 10. But is there a way that I can repeat the random result, 2 to 3 times? The result would be something like this:
1, 1, 6, 6, 6, 3, 3, 8, 8, 9, 9, 9 ...
// 1) Randomly selected and 2) repeated 2 to 3 times randomly.
Perhaps I might use two arc4random_uniform functions together, but cannot express them properly. Would be much appreciated if you could give me some suggestions. <3
In order to do this, you will need to generate two values: your random value and a repeatCount. Also, you'll need to remember both of those values so that you can repeat the value. You can do this with a custom class:
class RandomWithRepeats {
var range: ClosedRange<Int>
var repeatRange: ClosedRange<Int>
var repeatCount = 0
var value = 0
init(range: ClosedRange<Int>, repeatRange: ClosedRange<Int>) {
self.range = range
self.repeatRange = repeatRange
}
// generate a random number in a range
// Just use Int.random(in:) with Swift 4.2 and later
func random(in range: ClosedRange<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound + 1))) + range.lowerBound
}
func nextValue() -> Int {
// if repeatCount is 0, its time to generate a new value and
// a new repeatCount
if repeatCount == 0 {
// For Swift 4.2, just use Int.random(in:) instead
value = self.random(in: range)
repeatCount = self.random(in: repeatRange)
}
repeatCount -= 1
return value
}
}
Example:
let rand = RandomWithRepeats(range: 1...10, repeatRange: 2...3)
// generate 20 random repeated numbers
for _ in 1...20
{
print(rand.nextValue(), terminator: " ")
}
6 6 6 8 8 8 10 10 10 2 2 9 9 5 5 8 8 8 5 5
With regards to the nuances of random number generators: have a look at GKRandomSource.
What you're doing here is not really making something less random, or modifying the parameters in the random number generator. You're simply applying an operation (with one random parameter) to a collection of random integers.
extension Collection {
func duplicateItemsRandomly(range: CountableClosedRange<Int>) -> [Element] {
return self.reduce(into: [Element](), { (acc, element) in
let distance = UInt32(range.upperBound - range.lowerBound + 1)
let count = Int(arc4random_uniform(distance) + UInt32(range.lowerBound))
let result = Array.init(repeating: element, count: count)
acc.append(contentsOf: result)
})
}
}
let sequence = [1, 6, 3, 8, 9]
sequence.duplicateItemsRandomly(range: 2...3)
// [1, 1, 6, 6, 6, 3, 3, 3, 8, 8, 8, 9, 9, 9]
P.S: If you're writing this code in Swift 4.2, please use Int.random(in:).
I'd suggest a custom Sequence:
class RepeatingRandomSequence : Sequence {
let rangeLow, rangeSpan : UInt32
let repeatLow, repeatSpan : UInt32
init(range:Range<UInt32>, count:Range<UInt32>) {
rangeLow = range.lowerBound
rangeSpan = range.upperBound - range.lowerBound + 1
repeatLow = count.lowerBound
repeatSpan = count.upperBound - count.lowerBound + 1
}
func makeIterator() -> AnyIterator<UInt32> {
var count : UInt32 = 0
var value : UInt32 = 0
return AnyIterator {
if(count <= 0) {
count = arc4random_uniform(self.repeatSpan) + self.repeatLow
value = arc4random_uniform(self.rangeSpan) + self.rangeLow
}
defer { count = count - 1 }
return value
}
}
}
let sequence = RepeatingRandomSequence(range: 0..<10, count: 2..<3)
let randoms = sequence.makeIterator()
Note that the iterator, randoms now generates an endless sequence of random numbers using randoms.next() Since the sequence is endless, many things aren't particularly useful, like sort, map, etc. You could however use it like:
for value in random {
print(value)
if(value == 9) { // or any other termination condition
break
}
}
Or more conventionally, as:
(0..<10).forEach { _ in
print(String(describing: random.next()))
}

Swift range operator for i = mymax; i >= 0 i-- [duplicate]

This question already has answers here:
#warning: C-style for statement is deprecated and will be removed in a future version of Swift [duplicate]
(4 answers)
Decrement index in a loop after Swift C-style loops deprecated
(5 answers)
Closed 6 years ago.
I facing problem in executing for loop in Swift 3. I can use loop for range operator ... and ..< but in may case, I want something like ..> but its not available.
How do I execute following loop in Swift 3?
var myMax = 20
for var i = myMax ; i >= 0 ; i -= 1 {
...
}
It's easy to reverse the loop. User reversed function.
Swift 3
let myMax = 20
for i in (1..<myMax).reversed() {
print(i)
}
You can also use stride as #ZaidPathan said :
This question have all answers with all versions : How to iterate for loop in reverse order in swift?
for i in (1..<20).reversed() {
print(i)
}
Hope it helps.Read More
Swift 3, You can use stride
var myMax = 20
for var i = myMax ; i > 0 ; i -= 1 {
}
//Equivalent
let myMax = 20
for value in stride(from: 20, to: 0, by: -1){
print(value)
}
var myMax = 20
for var i = myMax ; i >= 0 ; i -= 1 {
}
//Equivalent
for value in stride(from: 20, through: 0, by: -1){
print(value)
}

Swift 3.0 multiple conditions in c-style for loop

I understand how to write a for loop using swift syntax (using .enumerate() and .revers()), however how would I re-write (in swift) the following javascript version of for loop, considering I have multiple conditions to adhere to:
for(var j = 10; j >= 0 && array[j] > value; j--) {
array[j + 1] = array[j];
}
What about this?
for j in (0...10).reversed() {
guard array[j] > value else { break }
array[j + 1] = array[j]
}
I'm not sure that produces exactly the same result, but this is one of the approaches in Swift 3
for j in stride(from:10, through:0, by: -1) {
if array[j] <= value { break }
array[j + 1] = array[j]
}
For the sake of completion (I would personally prefer to use a for loop with a check for an early break, as others have already suggested) – in Swift 3.1 you could use Sequence's prefix(while:) method in order to get the suffix of the array's indices where the elements meet a given predicate.
var array = [2, 3, 6, 19, 20, 45, 100, 125, 7, 9, 21, 22]
let value = 6
for i in array.indices // the array's indices.
.dropLast() // exclude the last index as we'll be accessing index + 1 in the loop.
.reversed() // reversed so we can get the suffix that meets the predicate.
.prefix(while: {array[$0] > value}) // the subsequence from the start of the
{ // collection where elements meet the predicate.
array[i + 1] = array[i]
}
print(array) // [2, 3, 6, 19, 19, 20, 45, 100, 125, 7, 9, 21]
This is assuming that you're looking to begin iterating at the penultimate index of the array. If you want to start at a particular index, you can say:
for i in (0...10).reversed().prefix(while: {array[$0] > value}) {
array[i + 1] = array[i]
}
This will start at the index 10 and iterate down to 0, giving you the same behaviour to the code in your question.
It's worth noting that both of the above variants will first iterate through the reversed indices (until the predicate isn't met), and then through the array's elements. Although, in Swift 3.1, there is a version of prefix(while:) which operates on a lazy sequence – which would allow for just a single iteration through the elements until the predicate isn't met.
Until Swift 3.1, you can use the below extension to get prefix(while:) on a Collection:
extension Collection {
func prefix(while predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence {
var index = startIndex
while try index < endIndex && predicate(self[index]) {
formIndex(after: &index)
}
return self[startIndex..<index]
}
}

does Swift array.count get evaluated each time in a loop

I suppose this is a question that must get asked for every language, but when you write for example:
while i < array.count {
...
}
does array.count get evaluated each time the loop runs? Is it better to store it in a let constant before running the loop like this?
let length = array.count
while i < length {
...
}
Array gets the count property because it conforms to Collection. The documentation for count in Collection states
Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the length of the collection.
Source
Since Array also conforms to RandomAccessCollection, it is a constant time operation to get the count of the array. There shouldn't be any major performance difference between getting it once at the start vs every loop iteration.
while loops (and do while loops) have their predicates evaluated on each iteration.
for loops evaluate the sequences once.
Here's is a demonstration:
var array: [Int]
print("Test Case 1 - while i < array.count")
array = [1, 2, 3, 4, 5, 6]
var i = 0
while i < array.count {
print(array[i])
if i < 3 { array.append(123) }
i += 1
}
print("\r\nTest Case 2 - for i in array.indices")
array = [1, 2, 3, 4, 5, 6]
for i in array.indices {
print(array[i])
if i < 3 { array.append(123) }
}
print("\r\nTest Case 3 - for i in 0 ..< array.count")
array = [1, 2, 3, 4, 5, 6]
for i in 0 ..< array.count {
print(array[i])
if i < 3 { array.append(123) }
}
Test Case 1 - while i < array.count
1
2
3
4
5
6
123
123
123
Test Case 2 - for i in array.indices
1
2
3
4
5
6
Test Case 3 - for i in 0 ..< array.count
1
2
3
4
5
6
Yes it's evaluated on each iteration.
Assigning to a constant will be slightly more performant. However with all of the optimisations in a modern compiler I wouldn't bother. Unless the loop count is going to be humongous.
Careful with for loops like the following. Since elems.count is part of a range that gets constructed once at the top of the loop, it is evaluated exactly once. The following code will die when i = 4:
var elems = [1, 2, 3, 4, 5, 6]
for i in 0 ..< elems.count {
if i % 2 == 0 { // remove even elements
elems.remove(at: i)
}
}
The array.count in your while does indeed get evaluated each time the condition is evaluated.
Yes, it gets called every time
Let's run a simple test, first of all we need the following Array Extension
extension Array {
var myCustomCount: Int {
print("myCustomCount")
return self.count
}
}
And then we can try this code
let nums = [1, 2, 3, 4, 5]
var i = 0
while i < nums.myCustomCount {
i += 1
}
The output is
myCustomCount
myCustomCount
myCustomCount
myCustomCount
myCustomCount
myCustomCount

Simple Swift Fibonacci program crashing (Project Euler 2)

I am trying to solve the second problem on Project Euler. The problem is as follows:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
I think I've written a solution, but when I try to run my code it crashes my Swift playground and gives me this error message:
Playground execution aborted: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
var prev = 0
var next = 1
var num = 0
var sum = 0
for var i = 1; i < 400; i++ {
num = prev + next
if next % 2 == 0 {
sum += next
}
prev = next
next = num
}
print(sum)
The weird thing is, if I set the counter on my loop to less than 93, it works fine. Explicitly setting the variable names to Double does not help. Anyone know what's going on here?
There is nothing weird about this at all. Do you know how large the 400 fibonacci number is?
176023680645013966468226945392411250770384383304492191886725992896575345044216019675
Swift Int64 or UInt64 simply cannot handle that large of a number. The later can go up to 18446744073709551615 at max - not even close.
If you change your variables to be doubles it works but will be inaccurate:
var prev : Double = 0
var next : Double = 1
var num : Double = 0
var sum : Double = 0
will yield
2.84812298108489e+83
which is kind of close to the actual value of
1.76e+83
Luckily you do not need to get values that big. I would recommend not writing a for loop but a while loop that calculates the next fibonacci number until the break condition is met whose values do not exceed four million.
The Fibonacci numbers become very large quickly. To compute large Fibonacci numbers, you need to implement some kind of BigNum. Here is a version the makes a BigNum that is implemented internally as an array of digits. For example, 12345 is implemented internally as [1, 2, 3, 4, 5]. This makes it easy to represent arbitrarily large numbers.
Addition is implemented by making the two arrays the same size, then map is used to add the elements, finally the carryAll function restores the array to single digits.
For example 12345 + 67:
[1, 2, 3, 4, 5] + [6, 7] // numbers represented as arrays
[1, 2, 3, 4, 5] + [0, 0, 0, 6, 7] // pad the shorter array with 0's
[1, 2, 3, 10, 12] // add the arrays element-wise
[1, 2, 4, 1, 2] // perform carry operation
Here is the implementation of BigNum. It is also CustomStringConvertible which makes it possible to print the result as a String.
struct BigNum: CustomStringConvertible {
var arr = [Int]()
// Return BigNum value as a String so it can be printed
var description: String { return arr.map(String.init).joined() }
init(_ arr: [Int]) {
self.arr = carryAll(arr)
}
// Allow BigNum to be initialized with an `Int`
init(_ i: Int = 0) {
self.init([i])
}
// Perform the carry operation to restore the array to single
// digits
func carryAll(_ arr: [Int]) -> [Int] {
var result = [Int]()
var carry = 0
for val in arr.reversed() {
let total = val + carry
let digit = total % 10
carry = total / 10
result.append(digit)
}
while carry > 0 {
let digit = carry % 10
carry = carry / 10
result.append(digit)
}
return result.reversed()
}
// Enable two BigNums to be added with +
static func +(_ lhs: BigNum, _ rhs: BigNum) -> BigNum {
var arr1 = lhs.arr
var arr2 = rhs.arr
let diff = arr1.count - arr2.count
// Pad the arrays to the same length
if diff < 0 {
arr1 = Array(repeating: 0, count: -diff) + arr1
} else if diff > 0 {
arr2 = Array(repeating: 0, count: diff) + arr2
}
return BigNum(zip(arr1, arr2).map { $0 + $1 })
}
}
// This function is based upon this question:
// https://stackoverflow.com/q/52975875/1630618
func fibonacci(to n: Int) {
guard n >= 2 else { return }
var array = [BigNum(0), BigNum(1)]
for i in 2...n {
array.append(BigNum())
array[i] = array[i - 1] + array[i - 2]
print(array[i])
}
}
fibonacci(to: 400)
Output:
1
2
3
5
8
...
67235063181538321178464953103361505925388677826679492786974790147181418684399715449
108788617463475645289761992289049744844995705477812699099751202749393926359816304226
176023680645013966468226945392411250770384383304492191886725992896575345044216019675