1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
import UIKit
func minMax(arr array: [Int], _ x: Int, t: Double) -> (min: Int, max: Int)? { if array.isEmpty { return nil } var currentMin = array[0] var currentMax = array[0] for value in array[1..<array.count]{ if value < currentMin { currentMin = value } else if value > currentMax { currentMax = value } } return (currentMin, currentMax) }
minMax(arr: [1, -1, 2, 23, 934, -234], 3, t: 2.0) minMax(arr: [], 2, t: 1.0)
func someFunciton(x: Int = 11) { print(x) }
someFunciton() someFunciton(22)
func arithmeticMean(numbers: Double...) -> Double { var total: Double = 0 for number in numbers { total += number } return total }
arithmeticMean(1, 2, 3, 32.232432, 32432.32423) arithmeticMean(3) arithmeticMean(1, 3)
func alignRight(var string: String, var totalLength: Int, pad: Character) -> String { totalLength++ let amountToPad = totalLength - string.characters.count if amountToPad < 1 { return string } let padString = String(pad) for _ in 1...amountToPad { string = padString + string } return string }
alignRight("fdsf", totalLength: 8, pad: "*")
func swapTwoInts(inout a: Int, inout _ b: Int) { let tmporaryA = a a = b b = tmporaryA }
var a = 3 var b = 5 swapTwoInts(&a, &b) a - b
func addTwoInts(a: Int, _ b: Int) -> Int { return a + b }
func multiplyTwoInts(a: Int, _ b: Int) -> Int { return a * b }
func printHelloWorld() { print("Hello world!") }
var mathFunction: (Int, Int) -> Int = addTwoInts print("Result: \(mathFunction(2, 4))")
mathFunction = multiplyTwoInts mathFunction(2, 4)
func printMathResult(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) { print("Result: \(mathFunction(a, b))") } printMathResult(addTwoInts, 3, 7)
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward }
var currentValue = 3 let moveNearerToZero = chooseStepFunction(currentValue > 0)
print("Counting to zero:") while currentValue != 0 { print("\(currentValue)... ") currentValue = moveNearerToZero(currentValue) } print("zero!")
func chooseStepFunction1(backwards: Bool) -> (Int) -> Int { func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } return backwards ? stepBackward : stepForward }
var currentValue1 = -4 let moveNearerToZero1 = chooseStepFunction1(currentValue > 0)
print("Counting to zero:") while currentValue1 != 0 { print("\(currentValue1)... ") currentValue1 = moveNearerToZero1(currentValue1) } print("zero!")
|