1

Im trying to find the smallest number that is also false in my dictionary. I'm also using the new Swift Language. How can I make this work?

var firstRow = [1: false, 2: false, 3: false, 4: false, 5: false, 6: false]




  for (number, bool) in firstRow {

            if bool == false {

              //  NSLog("\(number)")


                for i = number; i > 0; i-- {
                    if number <= smallest {

                        smallest = number

                        NSLog("\(smallest)")
                    }

                }
               // NSLog("\(bool, number)")
            }
      }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
SpringN
  • 904
  • 1
  • 11
  • 21

4 Answers4

2

Here you go:

var smallest = 10000000000
for (number, bool) in firstRow {

        if bool == false && number < smallest{
             smallest = number

        }
}
println("\(smallest)")
ASKASK
  • 657
  • 8
  • 16
0

If you're doing this strictly in Swift, I'd go with ASKASK while adding the the change:

let firstRow = [1: true, 2: false, 3: true]

var smallest = Int.max

for (number, bool) in firstRow {
    if bool == false && number < smallest {
        smallest = number
    }
}

There's no guarantee about the order in which your key-value pairs will be enumerated in your Dictionary, so given this example the fact that your keys are Integers that are displayed in ascending order does not help us, you will still need to check every pair in the Dictionary.

You might benefit from using an Array of Tuples here, especially if you know beforehand that the associated integers will be added to your collection in increasing order - or even if you don't, then you could at least sort the array by the first value in each tuple and thereby break out of the for loop when appropriate; for example:

let firstRow = [(1, true), (2, false), (3: true)]
var smallest: Int!  // given that it might be possible _none_ of them are 'false'
for (number, bool) in firstRow {
    if bool == false {
        smallest = number
        break
    }
}
fqdn
  • 2,823
  • 1
  • 13
  • 16
  • You're suggesting *hand-sorting* the table? Why not just do the whole thing by hand and just say smallest = 2 ? – Jim Balter Jun 08 '14 at 04:14
  • not at all... I was rather suggesting that if we know that members are going to be pushed to the collection using strictly increasing integers, that we take advantage of one of the ordered data structures so that we can break from the search confident that we have found the smallest number; in any case, looks like there have been several options presented here, I'm sure one of them must have ended up serving the need! – fqdn Jun 11 '14 at 19:34
  • "I was rather suggesting that if we know that members are going to be pushed to the collection using strictly increasing integers" -- uh, also known as "sorted". Like I said, hand sorting ... that's the only way you would "know beforehand" that the data is sorted without having to sort it. Since the OP didn't say that's guaranteed, I would omit the break. And hopefully Apple will add functional facilities to their collection library (if they haven't already) so you can just say smallest = firstRow.min({$0.1 == false}) as in other modern languages. – Jim Balter Jun 12 '14 at 00:17
0

There seems to be a built-in filter() method on arrays in Swift, so I'd go with something like:

let falseNumbers: Int[] = firstRow.keys.filter { firstRow[$0] == false }
let smallestFalseNumber: Int = sort(falseNumbers)[0]

The above assumes there is at least one false value in firstRow dictionary. To ensure that:

if find(firstRow.values, false) {
    // The above code
}
Community
  • 1
  • 1
Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
0

You can use the built-in min(by: ) method to fetch the minimum value using key or value in dictionary.

let minmum = firstrow.min { a, b in
    return a.value < b.value
 }
 // use minimum object to print key and value 
 print(minimum.key)
 print(minimum.value)
Umair Ali
  • 758
  • 8
  • 17