TokyoToo
TokyoToo

Reputation: 926

unresolved identifier - for in loop logic

This is a sort from bottom to top. While looping the iteration is substituted with the lowest number in the array and this continues all the way to the end.

As you can see I'm refactoring to use stride. Unfortunately var lowest = firstIndex is giving me some troubles.

I should be able to complete this function using stride right? I believe I should be using stride: to instead of stride: through. Thanks to Tim for that tip.

func selOrganize(myList: Array<Int>) -> Array<Int> { 1

    var extract = myList

    for firstIndex in 0..<extract.count {

        var lowest = firstIndex

        for var secondIndex = firstIndex + 1; secondIndex < extract.count; secondIndex++ {

            if extract[lowest] > extract[secondIndex] {
                lowest = secondIndex
    }
        }

        if firstIndex != lowest {
            swap(&extract[firstIndex], &extract[lowest])
        }   
    }
    return extract 
}

Updated syntax

func selOrganize(myList: Array<Int>) -> Array<Int> {

var extract = myList

    // var lowest = firstIndex

    // Do I need 'key'? Should I declare 'lowest' as a variable here?
    // If I do use it here I get a "'lowest' is unmutable because it's a let" error
    for (firstIndex, key) in extract.enumerate() {

        // < > stride uses 'to' and  <= >= stride uses through
        for secondIndex in (firstIndex).stride(to: 0, by: +1) {  

            if extract[lowest] > extract[secondIndex] {
                lowest = secondIndex
            }
        }

    if firstIndex != lowest {
        swap(&extract[firstIndex], &extract[lowest])
    }

}

return extract
}

Upvotes: 0

Views: 295

Answers (2)

vadian
vadian

Reputation: 285069

Stride is not needed, you can do that with standard for i in start..<end syntax

func selOrganize(myList: Array<Int>) -> Array<Int> {

  var extract = myList

  for firstIndex in 0..<extract.count {
  var lowest = firstIndex
    for secondIndex in (firstIndex + 1)..<extract.count {
      if extract[lowest] > extract[secondIndex] {
        lowest = secondIndex
      }
    }

    if firstIndex != lowest {
      swap(&extract[firstIndex], &extract[lowest])
    }
  }
  return extract
}

But actually you can do the same in one line

let sortedList = myList.sort{$0 < $1}

Upvotes: 1

Aaron Rasmussen
Aaron Rasmussen

Reputation: 13316

I got it to work like this:

func selOrganize(myList: Array<Int>) -> Array<Int> {

    var extract = myList

    // Accessing indices is simpler than calling enumerate, and
    // the function only needs access to the indices, not the 
    // values of the enumeration:
    for firstIndex in extract.indices {

        // lowest needs to be defined inside this loop if you 
        // are going to initialize it using firstIndex 
        // because firstIndex isn't defined outside the loop.
        var lowest = firstIndex

        // You need to stride from the firstIndex to the end of the
        // array, so the call to stride should look like this:
        for secondIndex in firstIndex.stride(to: extract.count, by: 1) {  

            if extract[lowest] > extract[secondIndex] {
                lowest = secondIndex
            }
        }

        if firstIndex != lowest {
            swap(&extract[firstIndex], &extract[lowest])
        }
    }

    return extract
}

Upvotes: 1

Related Questions