Comparing arrays with -nan - arrays

I have two Double arrays with the equal amount of elements. Both of them contain numbers and some NaN values after trigonometric calculations.
I need to compare every element of the first array to every element of the second, find the greater number, and put it into a new third array. At the end, the third array should contain the same amount of elements as first or second array.
If two Nan are compared, I need to display a specific error message "terrible error" at that exact index. So I guess the third array should be String to be able to display both numbers and error messages. If a Double number is compared to NaN, the Double number should always be chosen as the greater.
How do I do all of that?
Here is my code:
import Foundation
var initValue = Double()
var finalValue = Double()
var stepValue = Double()
while true {
print("Enter the starting number of the range")
if let number = readLine(), Double(number) != nil {
initValue = Double(number)!
break
} else {
print("Enter the correct number!")
}
}
while true {
print("Enter the end value of the range")
if let number = readLine(), Double(number) != nil, Double(number)! > initValue {
finalValue = Double(number)!
break
} else {
print("Enter the correct number, which is greater than starting number of the range!")
}
}
while true {
print("Enter delta")
if let number = readLine(), Double(number) != nil {
stepValue = Double(number)!
break
} else {
print("Enter the correct number!")
}
}
var trueArray = [Double]()
for number in stride(from: initValue, through: finalValue, by: stepValue) {
trueArray.append(number)
}
func calcLn () -> [Double] {
let calculatedArray = trueArray.map { log(1-46/sin($0)) }
return calculatedArray
}
func calcTan () -> [String] {
let calculatedArray = trueArray.map { (tan($0)/46) }
return calculatedArray
}

You can use zip to iterate over your 2 arrays concurrently, and then map to transform the pairs of elements into outputs, as you would like them. It's useful to switching on (ln.isNaN, tan.isNaN), and use that to express the various cases and what their results should be.
Here's a rough start:
import Darwin
func promptForDouble(
initialMessage: String,
errorMessage: String,
acceptanceCriteria isAcceptable: (Double) -> Bool = { _ in true }
) -> Double {
print(initialMessage)
while true {
if let number = readLine().flatMap(Double.init), isAcceptable(number) {
return number
}
print(errorMessage)
}
}
let initValue = promptForDouble(
initialMessage: "Enter the starting number of the range",
errorMessage: "Enter the correct number!"
)
let finalValue = promptForDouble(
initialMessage: "Enter the end value of the range",
errorMessage: "Enter the correct number, which is greater than starting number of the range!",
acceptanceCriteria: { initValue < $0 }
)
let stepValue = promptForDouble(
initialMessage: "Enter delta",
errorMessage: "Enter the correct number!"
)
// TODO: give these functions better names!
func calcLn(_ input: [Double]) -> [Double] {
return input.map { log(1 - 46/sin($0)) }
}
func calcTan(_ input: [Double]) -> [Double] {
return input.map { tan($0) / 46 }
}
func mergeResults(lns: [Double], tans: [Double]) -> [Double?] {
return zip(lns, tans).map { ln, tan -> Double? in
switch (ln.isNaN, tan.isNaN) {
case ( true, true): return nil // Return nil to express error. Don't introduce Strings yet.
case (false, true): return ln
case ( true, false): return tan
case (false, false): return max(ln, tan)
}
}
}
func printResults(_ a1: [Double], _ a2: [Double], _ a3: [Double?]) {
for (a, (b, c)) in zip(a1, zip(a2, a3)) {
let resultString = c.map(String.init) ?? "terrible error" // Note: Strings are only introduced at the UI/presentation layer
print("ln: \(a),\ttan: \(b),\tresult: \(resultString)")
}
}
// TODO: give these arrays better names!
let inputs = Array(stride(from: initValue, through: finalValue, by: stepValue))
let array1 = calcLn(inputs)
let array2 = calcTan(inputs)
let array3 = mergeResults(lns: array1, tans: array2)
printResults(array1, array2, array3)

Related

How to count how many types in array[Any]

I am new to Swift so im just trying to solve basic problems, but can't figure out this one.
I am trying to count how many times a String, Int, Double or another class appears. And output the data. Right now i am only outputting the type. This is the code so far:
var myArr = [Any]()
myArr.append("hello")
myArr.append("goodbye")
myArr.append(1)
myArr.append(1.0)
myArr.append(Student())
for item in myArr {
switch item {
case let stringy as String :
print(stringy, type(of: stringy))
case let inty as Int :
print(type(of: inty))
case let doubly as Double :
print(type(of: doubly))
case let student as Student :
print(type(of: student))
default:
print("unknown object")
}
}
A simple solution is to group the array to a dictionary by the type description
var myArr = [Any]()
myArr.append("hello")
myArr.append("goodbye")
myArr.append(1)
myArr.append(1.0)
myArr.append(Student())
let typeData = Dictionary(grouping: myArr, by: {String(describing:type(of: $0))})
// -> ["Student": [__lldb_expr_9.Student()], "Double": [1.0], "Int": [1], "String": ["hello", "goodbye"]]
and print the result, the key and the number of items in value
for (key, value) in typeData {
print("\(key): \(value.count)" )
}
For your question, you can make a value storeCount to store value each team it match in each switch case
But the thing is that is not the good way to do that. You can split the array into subarray which each array match a type then you can easily to deal with value later.
Using filter to filter from beginning array each value which match with condition ( here is type given). If right type than get value.
let stringArr = myArr.filter {
$0 as? String != nil
}
let intArr = myArr.filter {
$0 as? Int != nil
}
let doubleArr = myArr.filter {
$0 as? Double != nil
}
let studentArr = myArr.filter {
$0 as? Student != nil
}
Usage
print("value string: ", stringArr, "; count: ", stringArr.count) // value string: ["hello", "goodbye"] ; count: 2
print("value int: ", intArr, "; count: ", intArr.count) // value int: [1] ; count: 1
print("value double: ", doubleArr, "; count: ", doubleArr.count) // value double: [1.0] ; count: 1
print("value student: ", studentArr, "; count: ", studentArr.count) // value student: [SelectText.Student] ; count: 1

Finding indices of max value in swift array

I have 2 arrays. One for players and one for scores. e.g.
var players = ["Bill", "Bob", "Sam", "Dave"]
var scores = [10,15,12,15]
I can find the index of the (first) max score (and the winner's name) by using:
let highScore = scores.max()
let winningPlayerIndex = scores.index(of: highScore!)
let winningPlayer = players[winningPlayerIndex!]
This works fine if there is only one player with the highest score but how would I return multiple indices (i.e. 1 and 3 in this example) for all values that are equal to the max value? I need the indices to then map back to the players array to pull out the names of all the players with the highest score. Or is there a better way to do all of this?
The accepted answer doesn't generalize to comparing computed values on the elements. The simplest and most efficient way to get the min/max value and index is to enumerate the list and work with the tuples (offset, element) instead:
struct Player {
let name: String
let stats: [Double]
}
let found = players.enumerated().max(by: { (a, b) in
battingAvg(a.element.stats) < battingAvg(b.element.stats)
})
print(found.element.name, found.offset) // "Joe", 42
In general you shouldn't rely on comparing floating point values by equality and even where you can, if the computation is expensive you don't want to repeat it to find the item in the list.
What you need is to use custom class or structure and make array of it then find max score and after that filter your array with max score.
struct Player {
let name: String
let score: Int
}
Now create array of this Player structure
var players = [Player(name: "Bill", score: 10), Player(name: "Bob", score: 15), Player(name: "Sam", score: 12), Player(name: "Dave", score: 15)]
let maxScore = players.max(by: { $0.0.score < $0.1.score })?.score ?? 0
To get the array of player with max core use filter on array like this.
let allPlayerWithMaxScore = players.filter { $0.score == maxScore }
To get the array of index for player having high score use filter on array like this.
let indexForPlayerWithMaxScore = players.indices.filter { players[$0].score == maxScore }
print(indexForPlayerWithMaxScore) //[1, 3]
To answer just the question in the title -- find the index of the max value in a (single) array:
extension Array where Element: Comparable {
var indexOfMax: Index? {
guard var maxValue = self.first else { return nil }
var maxIndex = 0
for (index, value) in self.enumerated() {
if value > maxValue {
maxValue = value
maxIndex = index
}
}
return maxIndex
}
}
The extension returns nil if the array is empty. Else, it starts by assuming the first value is the max, iterates over all values, updates the index and value to any larger values found, and finally returns the result.
If you have 2 arrays and need to find max score from first one in order to pull the name from second one, then I would recommend you to convert both arrays into one using zip high order func and retrieve the max value from there.
So having your data it will look like this:
let players = ["Bill", "Bob", "Sam", "Dave"]
let scores = [10,15,12,15]
let data = zip(players, scores)
// max score
let maxResult = data.max(by: ({ $0.1 < $1.1 }))?.1 ?? 0
// outputs 15
// leaders
let leaders = data.filter { $0.1 >= maxResult }.map { "\($0.0) - \($0.1)" }
// outputs ["Bob - 15", "Dave - 15"]
You can zip the collection indices with its elements and get the minimum value using collection min method and pass a predicate to compare the elements. Get the result and extract the index of the tuple:
let numbers = [2, 4, 4, 2, 3, 1]
let minIndex = zip(numbers.indices, numbers).min(by: { $0.1 < $1.1 })?.0 // 5
let maxIndex = zip(numbers.indices, numbers).max(by: { $0.1 < $1.1 })?.0 // 1
As an extension where the elements are comparable:
extension Collection where Element: Comparable {
func firstIndexOfMaxElement() -> Index? {
zip(indices, self).max(by: { $0.1 < $1.1 })?.0
}
func firstIndexOfMinElement() -> Index? {
zip(indices, self).min(by: { $0.1 < $1.1 })?.0
}
}
Usage:
numbers.firstIndexOfMinElement() // 5
If you need to find the maximum or minimum properties:
extension Collection {
func firstIndexOfMaxElement<T: Comparable>(_ predicate: (Element) -> T) -> Index? {
zip(indices, self).max(by: { predicate($0.1) < predicate($1.1) })?.0
}
func firstIndexOfMinElement<T: Comparable>(_ predicate: (Element) -> T) -> Index? {
zip(indices, self).min(by: { predicate($0.1) < predicate($1.1) })?.0
}
}
Usage:
struct Product {
let price: Int
}
let products: [Product] = [.init(price: 2),
.init(price: 4),
.init(price: 4),
.init(price: 2),
.init(price: 3),
.init(price: 1),]
let minPrice = products.firstIndexOfMinElement(\.price) // 5
To return the maximum and minimum elements and their indices:
extension Collection where Element: Comparable {
func maxElementAndIndices() -> (indices: [Index], element: Element)? {
guard let maxValue = self.max() else { return nil }
return (indices.filter { self[$0] == maxValue }, maxValue)
}
func minElementAndIndices() -> (indices: [Index], element: Element)? {
guard let minValue = self.min() else { return nil }
return (indices.filter { self[$0] == minValue }, minValue)
}
}
And the corresponding methods to custom structures/classes:
extension Collection {
func maxElementsAndIndices<T: Comparable>(_ predicate: (Element) -> T) -> [(index: Index, element: Element)] {
guard let maxValue = self.max(by:{ predicate($0) < predicate($1)}) else { return [] }
return zip(indices, self).filter { predicate(self[$0.0]) == predicate(maxValue) }
}
func minElementsAndIndices<T: Comparable>(_ predicate: (Element) -> T) -> [(index: Index, element: Element)] {
guard let minValue = self.min(by:{ predicate($0) < predicate($1)}) else { return [] }
return zip(indices, self).filter { predicate(self[$0.0]) == predicate(minValue) }
}
}
Usage:
let maxNumbers = numbers.maxElementAndIndices() // ([1, 2], element 4)
let minNumbers = numbers.minElementAndIndices() // ([5], element 1)
let maxPriceIndices = products.maxElementsAndIndices(\.price) // [(index: 1, element: Product(price: 4)), (index: 2, element: Product(price: 4))]
let minPriceIndices = products.minElementsAndIndices(\.price) // [(index: 5, element: __lldb_expr_22.Product(price: 1))]
There are a couple of ways to solve your problem, you can solve this by saving the indices of scores.max() and iterate through the players list, and also using then zip function:
var max_score = scores.max()
var players_and_score = zip(players, scores)
for player in players_and_score{
if player.1 == max_score{
print(player.0)
}
}

How to compare Equatable in Swift

I have multiple sets of two arrays like that. I am getting them from a 3rd party.
var array1 : [Any?]
var array2 : [Any?]
I know about types of objects in these array (in compile time). As example that first element will be a String and second is an Int.
I currently compare each set of arrays like that (please notice that arrays aren't homogeneous).
array1[0] as? String == array2[0] as? String
array1[1] as? Int == array2[1] as? Int
...
The biggest problem that each set have different types in it. As result, I have let say 10 sets of arrays with 5 elements in each. I had to do 10*5 explicit conversion to specific type and comparation.
I want to be able to write a common method which can compare two arrays like that (without a need to specify all the types)
compareFunc(array1, array2)
I started to go down the road. However, I can't figure out what should I cast the objects too. I tried Equatable. However, swift doesn't like direct usage of it. So, something like that doesn't work
func compare(array1: [Any?], array2: [Any?]) {
for index in 0..<array1.count {
if (array1[index] as? Equatable != array2[index] as? Equatable) {
// Do something
}
}
}
Each object in this array will be Equatable. However, I am not sure how to use it in this case.
Construct a simple element-by-element comparison function based on (attempted) type conversion to known element types
Since you're aiming to compare arrays of (optional) Any elements, you could just construct a function that performs element-by-element comparison by using a switch block to attempt to downcast the elements of the array to different known types in your "3rd party arrays". Note that you needn't specify which element position that corresponds to a specific type (as this might differ between different set of arrays), just the exhaustive set of the different types that any given element might be of.
An example of such a function follows below:
func compareAnyArrays(arr1: [Any?], _ arr2: [Any?]) -> Bool {
/* element-by-element downcasting (to known types) followed by comparison */
return arr1.count == arr2.count && !zip(arr1, arr2).contains {
/* note that a 'true' below indicates the arrays differ (i.e., 'false' w.r.t. array equality) */
if let v1 = $1 {
/* type check for known types */
switch $0 {
case .None: return true
case let v0 as String:
if let v1 = v1 as? String { return !(v0 == v1) }; return true
case let v0 as Int:
if let v1 = v1 as? Int { return !(v0 == v1) }; return true
/* ...
expand with the known possible types of your array elements
... */
case _ : return true
/* */
}
}
else if let _ = $0 { return true }
return false
}
}
or, alternative, making the switch block a little less bloated by making use of (a slightly modified of) the compare(...) helper function from #Roman Sausarnes:s answer
func compareAnyArrays(arr1: [Any?], _ arr2: [Any?]) -> Bool {
/* modified helper function from #Roman Sausarnes:s answer */
func compare<T: Equatable>(obj1: T, _ obj2: Any) -> Bool {
return obj1 == obj2 as? T
}
/* element-by-element downcasting (to known types) followed by comparison */
return arr1.count == arr2.count && !zip(arr1, arr2).contains {
/* note also that a 'true' below indicates the arrays differ
(=> false w.r.t. equality) */
if let v1 = $1 {
/* type check for known types */
switch $0 {
case .None: return true
case let v0 as String: return !compare(v0, v1)
case let v0 as Int: return !compare(v0, v1)
/* ...
expand with the known possible types of your array elements
... */
case _ : return true
/* */
}
}
else if let _ = $0 { return true }
return false
}
}
Example usage:
/* Example usage #1 */
let ex1_arr1 : [Any?] = ["foo", nil, 3, "bar"]
let ex1_arr2 : [Any?] = ["foo", nil, 3, "bar"]
compareAnyArrays(ex1_arr1, ex1_arr2) // true
/* Example usage #2 */
let ex2_arr1 : [Any?] = ["foo", nil, 2, "bar"]
let ex2_arr2 : [Any?] = ["foo", 3, 2, "bar"]
compareAnyArrays(ex2_arr1, ex2_arr2) // false
This is a marginal solution, but it should reduce some of the duplicative code that you are trying to avoid:
func compareAnyArray(a1: [Any?], _ a2: [Any?]) -> Bool {
// A helper function for casting and comparing.
func compare<T: Equatable>(obj1: Any, _ obj2: Any, t: T.Type) -> Bool {
return obj1 as? T == obj2 as? T
}
guard a1.count == a2.count else { return false }
return a1.indices.reduce(true) {
guard let _a1 = a1[$1], let _a2 = a2[$1] else { return $0 && a1[$1] == nil && a2[$1] == nil }
switch $1 {
// Add a case statement for each index in the array:
case 0:
return $0 && compare(_a1, _a2, t: Int.self)
case 1:
return $0 && compare(_a1, _a2, t: String.self)
default:
return false
}
}
}
That isn't the most beautiful solution, but it will reduce the amount of code you have to write, and it should work for any two [Any?], as long as you know that the type at index 0 is an Int, and the type at index 1 is a String, etc...
A compact and Swift 5 version of great solution of Aaron Rasmussen:
func compare(a1: [Any?], a2: [Any?]) -> Bool {
guard a1.count == a2.count else { return false }
func compare<T: Equatable>(obj1: Any, _ obj2: Any, t: T.Type) -> Bool {
return obj1 as? T == obj2 as? T
}
return a1.indices.reduce(true) {
guard let _a1 = a1[$1], let _a2 = a2[$1] else { return $0 && a1[$1] == nil && a2[$1] == nil }
switch $1 {
case 0: return $0 && compare(obj1: _a1, _a2, t: Int.self)
case 1: return $0 && compare(obj1: _a1, _a2, t: String.self)
case 2: return $0 && compare(obj1: _a1, _a2, t: <#TypeOfObjectAtIndex2#>.self)
default: return false
}
}
}

Swift: How to sort an Array<String> [duplicate]

This question already has an answer here:
Swift: sorting of array is not done correctly
(1 answer)
Closed 7 years ago.
I have an Array containing values like 7-4.json, 87-1.json and 102-4.json and want to sort it (ascending). I used the following code:
var fileNames = ["7-4.json", "87-1.json", "102-4.json"]
fileNames = fileNames.sort{ $0 < $1 }
print(fileNames)
which results in:
["102-4.json", "7-4.json", "87-1.json"]
So it didn't worked as I aspected. How can I sort it like 7-4, 87-1, 102-4?
Here you go:
var fileNames = ["7-4.json", "87-1.json", "102-4.json"]
func sortWithCustomFormat(first: String, second: String) -> Bool{
func extract(value: String) -> (Int, Int){
return (Int(value.componentsSeparatedByString("-").first!)!, Int(value.componentsSeparatedByString("-").last!.componentsSeparatedByString(".").first!)!)
}
let firstNumber = extract(first)
let secondNumber = extract(second)
if firstNumber.0 != secondNumber.0 { return firstNumber.0 < secondNumber.0 }
return firstNumber.1 < secondNumber.1
}
fileNames.sort(sortWithCustomFormat)
The function sortWithCustomFormat has a function extract that takes the inputted string and extracts the first and second numbers from it. Then, you compare the first numbers. If they are equal, then you compare the second numbers.
var fileNames = ["87-1.json", "7-4.json", "87-3.json", "102-4.json"]
fileNames = fileNames.sort({ (s1, s2) -> Bool in
let f1 = s1.stringByReplacingOccurrencesOfString(".json", withString: "")
let f2 = s2.stringByReplacingOccurrencesOfString(".json", withString: "")
let arr1 = f1.componentsSeparatedByString("-")
let arr2 = f2.componentsSeparatedByString("-")
var int1 = Int(arr1[0])
var int2 = Int(arr2[0])
if int1 < int2 {
return true
}
else if int1 > int2 {
return false
}
else {
int1 = Int(arr1[1])
int2 = Int(arr2[1])
if int1 < int2 {
return true
}
else if int1 > int2 {
return false
}
else {
return true
}
}
});
print(fileNames)
Try this...
var fileNames = ["87-1.json", "7-4.json", "102-4.json"]
// Modded OP's order to actually test sort
var sorted = fileNames.sort{ $0 < $1 }
print(sorted) // ["102-4.json", "7-4.json", "87-1.json"]
// Not sorted as OP "required", as they are string sorted, not number sorted
// Very simplistic solution
sorted = fileNames.sort { ($0 as NSString).integerValue < ($1 as NSString).integerValue}
print(sorted) // As OP requires, but...
// It won't sort on his count field - add a failing case...
fileNames = ["7-4.json", "87-1.json", "102-4.json", "102-1.json"]
sorted = fileNames.sort { ($0 as NSString).integerValue < ($1 as NSString).integerValue}
print(sorted) // ["7-4.json", "87-1.json", "102-4.json", "102-1.son"]
// WRONG!
// Define a simple function that parses his strings into tuples.
// This assumes that the Strings are valid, and fails safe if not.
// If you want more validation, add it yourself!
func myParse(s: String) -> (Int, Int) {
let c = s.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-."))
switch c.count {
case 0:
print("Careful Will Robinson!")
return (0, 0)
case 1:
print("Careful Will Robinson!")
return ((c[0] as NSString).integerValue, 0)
default:
return ((c[0] as NSString).integerValue, (c[1] as NSString).integerValue)
}
}
let test = fileNames.map { myParse($0) }
print("\(test)") // Test execution of function
sorted = fileNames.sort { (s1: String, s2: String) -> Bool in
let t1 = myParse(s1)
let t2 = myParse(s2)
if t1.0 == t2.0 {
return t1.1 < t2.1
} else {
return t1.0 < t2.0
}
}
print(sorted) // As required ["7-4.json", "87-1.json", "102-1.json", "102-4.json"]

Optional Int to Arrays in Swift - Average Function

How can I write an average function in swift in where input in an array of optional Ints? Here's what I wrote so far:
func ave(array: [Int?] -> Double?) {
var mysum = 0
for num in array {
mysum += num
}
return Double(mysum)/Double(array.count)
}
I read a lot of Optional ints but I don't know how to implement that when the input in an array of optional ints... Any help?
Here it is in Swift 2, since it is only a few days away now:
func ave(array: [Int?]) -> Double? {
guard array.filter({ $0 == nil }).isEmpty else {
print("One of the Ints was nil")
return nil
}
return Double(array.reduce(0, { $0 + $1! })) / Double(array.count)
}
The opening guard statement checks array for any nil members, and returns nil after printing a message if it finds any.
If no nil is found, we use a simple reduce statement to calculate the sum of the array members, then divide by the count.
Here are some examples of the results:
ave([1,2,3]) // 2
ave([1,2,nil]) // nil (and the message will print)
ave([]) // Double.NaN (because you're dividing by zero)
If you want it in Swift 1.2, the implementation is not all that different:
func ave(array: [Int?]) -> Double? {
if array.filter({ $0 == nil }).isEmpty {
return Double(array.reduce(0, combine: { $0 + $1! })) / Double(array.count)
} else {
print("One of the Ints was nil")
return nil
}
}
You just need to make an if let check in your for loop.
func ave(array: [Int?])-> Double {
var arraySize = array.count
var mysum = 0
for num in array {
if let num = num{
mysum += num
}else{
arraySize--
println("Is nil")
}
}
return Double(mysum)/Double(arraySize)
}
As you maybe see, I've added a variable called arraySize because you need to check what's the size of your real array. Without the nils. Otherwise your final calculation doesn't work as wanted. Also I've changed your func-header a little bit. There was a mistake in your code before:
func ave(array: [Int?] -> Double?) {
^^^^^^^^^^
The return-value has to be outside of the ():
func ave(array: [Int?])-> Double {
In Swift you can do average func in two lines (you can do this in one line, of course, but then you get duplicate code - array.filter { $0 != nil }):
func average(array: [Int?]) -> Double {
let arrayWhithoutNils = array.filter { $0 != nil }
return arrayWhithoutNils.count > 0 ? (arrayWhithoutNils.map { Double($0!) }.reduce(0, combine: +) / Double(arrayWhithoutNils.count)) : 0
}
print(average([1, 2, 3])) // 2
print(average([nil, 4, 5, nil])) // 4.5
When you have an array with optional elements, it usually helps to use flatmap to first give you an array with no optionals...
func ave ( nums:[Int?] ) -> Double?
{
var answer : Double? = .None
let realInts = nums.flatMap { $0 }
if ( realInts.count > 0 ) {
var accum : Int = 0
realInts.map { accum += $0 }
answer = Double(accum) / Double(realInts.count)
}
return answer
}
This should do the job
func average(numbers: [Int?]) -> Double {
let sum = numbers.reduce(0) { $0 + ($1 ?? 0) }
let numValidElms = numbers.filter { $0 != nil }.count
let delta = numbers.count - numValidElms
if delta > 0 {
println("Found \(delta) nil values")
}
if numValidElms > 0 {
return Double(sum) / Double(numValidElms)
} else {
return 0
}
}
Examples
average([nil]) // 0
average([1,2,3]) // 2
average([1, nil, 2]) // 1.5
Hope this helps.
One more option:
func ave(array: [Int?])-> Double {
var mysum = 0
var c = 0
for num in array {
if let n = num {
mysum += n
c++
}
return Double(mysum)/Double(c)
}

Resources