//: Playground - noun: a place where people can play
import UIKit
// 课堂练习一:
func mean(_ numbers: Double...) -> Double {
var sum: Double = 0
// 循环累加
for number in numbers {
sum += number
}
return sum / Double(numbers.count)
}
// 验证:
var meanValue = mean(10.5, 11.345, 2223.21)
// 课堂练习二:
func convertBinary(num: inout Int) -> String {
var result = ""
repeat {
result = String(num%2) + result
num /= 2
} while num != 0
return result
}
// 验证
var value = 6
var binaryValue = convertBinary(num: &value)
value // 110
// 课堂练习三:
// 循环修改学生成绩函数
func changeScores(scores: inout [Int], by changeScore: (Int) -> Int) {
for (index, score) in scores.enumerated() {
scores[index] = changeScore(score)
}
}
// 改分方式一:
func changeScoreOne(score: Int) -> Int {
return Int(sqrt(Double(score)) * 10)
}
// 改分方式二:
func changeScoreTwo(score: Int) -> Int {
return Int(Double(score) / 150.0 * 100.0)
}
// 改分方式三:
func changeScoreThree(score: Int) -> Int {
return score + 5
}
// 验证:
var scoreOne = [36, 41, 32, 75, 88, 92]
changeScores(scores: &scoreOne, by: changeScoreOne)
scoreOne
var scoreTwo = [73, 62, 98, 100, 132, 145]
changeScores(scores: &scoreTwo, by: changeScoreTwo)
scoreTwo
var scoreThree = [36, 41, 32, 75, 88, 92]
changeScores(scores: &scoreThree, by: changeScoreThree)
scoreThree