0

I have some array of Tuples with definition like this:

[(description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)] and sorted by decrease of .criterion.

I need to add .group member to each Touple in this array based on matching values of .criterion.

Value of .group is 1...n increasing by 1. If several Tuples have the same value of .criterion, then they will have the same value of .group.

If Tuple have unique .criterion, then it only one will have unique .group value.

I'm trying to do this in code below:

func appendingGroup(_ input: [(description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)]) -> [(description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)] {
var output: [(description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)] = []
var index = 1
while index < input.count - 1 {
    if input[index].criterion != input[index + 1].criterion && input[index].criterion != input[index - 1].criterion {
        print(index)
        output[index].group = index
    }
    index += 1
}
return output}

This is based on @Nicolai Henriksen question Swift: loop over array elements and access previous and next elements

But I have [] in my output.

What I'm doing wrong?

George
  • 25
  • 8

2 Answers2

2

The reason why you get empty output is because you didn't modify it.

Try change

var output: [(description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)] = []

to

var output = input

Full:

typealias Type = (description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)

func appendingGroup(_ input: [Type]) -> [Type] {
    guard input.count > 2 else { return input } // without this check app will crash for arrays that are less than 2
    var output = input
    var index = 1

    while index < input.count - 1 {
        if input[index].criterion != input[index + 1].criterion && input[index].criterion != input[index - 1].criterion {
            output[index].group = index
        }

        index += 1
    }

    return output
}
Bohdan Savych
  • 3,310
  • 4
  • 28
  • 47
  • Looks like it's works...but in my `input` I have initial value `.group = 0` and in output all `.group` is **0**. – George Jun 10 '19 at 14:36
  • 1
    @George Have you tried to debug? I guess that for your array if conditions (`if input[index].criterion != input[index + 1].criterion && input[index].criterion != input[index - 1].criterion`) is not satisfy. – Bohdan Savych Jun 10 '19 at 14:39
  • Not yet. Thanks for your help! – George Jun 10 '19 at 14:43
  • @George If my answer helped you then consider to accept it :) Thanks – Bohdan Savych Jun 10 '19 at 15:57
  • Almost done... But still don't know how to be with the last element of array! It'sout of the scope! – George Jun 11 '19 at 09:17
  • `var index = 0 var group = 1 while index < input.count - 1 { if input[index].criterion == input[index + 1].criterion { output[index].group = group } else { group += 1 output[index].group = group } index += 1 }` – George Jun 11 '19 at 09:18
0

Finally working solution based on @Bohdan Savych comment:

    typealias Type = (description: [String], criterion: Int, relative: Double, average: Int, type: String, group: Int)

    func appendingGroup(_ input: [Type]) -> [Type] {
          guard input.count > 2 else { return input } // without this check app will crash for arrays that are less than 2
          var output = input
          var index = 0
          var group = 1
          while index < input.count - 1 {
                if input[index].criterion == input[index + 1].criterion {
                      output[index].group = group
                } else {
                      output[index].group = group
                      group += 1
                } 
                index += 1
          }
          if input[input.count - 1].criterion == input[input.count - 2].criterion {
                output[input.count - 1].group = output[input.count - 2].group
          } else {
                output[input.count - 1].group = (output[input.count - 2].group) + 1
          }
          return output
    }        
George
  • 25
  • 8