1

I very new to Swift and programming in general, so I can't figure out where is the problem. Tried to find the answer in the existing topics - couldn't. Thanks in advance.

func smartAssigning(names: [String], statuses: [Bool], projects: [Int], tasks: [Int]) -> String {
    //beginig
    var collect: [Int] = [], nameLast = 0
    var candidateNumber: [Int] = []
    for x in 0...statuses.count {

        //Defines who is working
        if statuses[x] == false {
            collect.append(x)
        } else {}
    }

    // Checks for min tasks assigned among those not on vacation
    let rt: Int = tasks.min()!
    let rp: Int = projects.min()!
    for i in collect {
        if tasks[i] == rt {
            candidateNumber.append(i)
        } else {}
    }
    // if there is only 1 with min tasks - returns his number in array
    if candidateNumber.count == 1 {
        nameLast = candidateNumber[0]
    } else {
        // checks for min projects
        for e in candidateNumber {
            if projects[e] == rp {
                nameLast = e
            }
        }
    }
    return names[nameLast]
}
smartAssigning(names: ["sd", "dfsd","dfsdf"], statuses: [true, false, false], projects: [2, 1, 1], tasks: [3, 2, 1])

Screen:

Screen

4
  • 1
    The first error is at for x in 0...statuses.count– Did you try to debug the problem? Single-stepping the function would reveal the problem quickly. Commented Dec 20, 2016 at 20:50
  • Are the elements in the names, statuses, projects and tasks arrays all tied together? E.g. the first name is tied to the first status, the first project, and first task? Commented Dec 20, 2016 at 20:50
  • Yes, they Are. But I found my problem. Thanks to you and Martin R. Damn. so easy. Why XCODE doesn't recognize it as an error? Commented Dec 20, 2016 at 20:57
  • @JackMov Because it's a run time error. Commented Dec 20, 2016 at 21:09

1 Answer 1

1

The error is here:

for x in 0...statuses.count { // Error is here

    //Defines who is working
    if statuses[x] == false {
        collect.append(x)
    } else {}
}

If statuses.count is n, then the maximum index is n-1. It should be 0..<statuses.count. You should avoid manually creating such a range, exactly because it can cause this typo. It's much better to just do for x in status.indices.

On a side note, you don't need to have an else clause if it doesn't do anything.

Also, rather than comparing against false (== false), just negate the Bool.

for x in statuses.indices { // Error is here
    //Defines who is working
    if !statuses[x] {
        collect.append(x)
    }
}

This whole code can be written using a single filter(_:) expression:

// TODO: Give me a new meaningful name!
let collect = statues.enumerated() // Get index, status pairs
                     .filter{ index, status in !status } // filter all false statuses
                     .map{ index, status in index } // Take just the indices
Sign up to request clarification or add additional context in comments.

5 Comments

Thx. The first error is found? Looks like it works in XCODE, but doesn't want to in CODECHALLENGE window. Says: file.swift on line 15:24: error: value of type '[Int]' has no member 'min' if tasks[i] == tasks.min() { ^~~~~ ~~~
I have no idea what you're saying. o.0'
IDK, open a new question. That seems like it should be okay
I found the reason. They use Swift 2 and it requires array.minElement() instead of array.min()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.