-
-
Save allurco/fff8bfba170d2fa5aa74f4055e166db4 to your computer and use it in GitHub Desktop.
Max and Min sum of 4 values in array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let string = "1 2 3 4 5" | |
let numbers = string.split(separator: " ").map{Int($0)!} | |
var asc = numbers.sorted() | |
var desc = Array(asc.reversed()) | |
print (asc) | |
asc.removeFirst() | |
desc.removeFirst() | |
print (asc) | |
var min = 0 | |
var max = 0 | |
for i in 0..<asc.count { | |
max = max + asc[i]; | |
} | |
for i in 0..<desc.count { | |
min = min + desc[i]; | |
} | |
print("\(min) \(max)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment