当前位置: 首页 > 面试题库 >

使我的函数计算数组Swift的平均值

柳韬
2023-03-14
问题内容

我希望函数计算Double型数组的平均值。该数组称为“投票”。现在,我有10个号码。

当我致电average function以获取阵列投票的平均值时,它不起作用。

这是我的代码:

var votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

func average(nums: Double...) -> Double {
    var total = 0.0
    for vote in votes {
        total += vote
    }
    let votesTotal = Double(votes.count)
    var average = total/votesTotal
    return average
}

average[votes]

我如何在此处称平均值以获得平均值?


问题答案:

您应该使用reduce()方法对数组求和,如下所示:

Xcode Xcode 10.2+•Swift 5或更高版本

extension Sequence where Element: AdditiveArithmetic {
    /// Returns the total sum of all elements in the sequence
    func sum() -> Element { reduce(.zero, +) }
}
extension Collection where Element: BinaryInteger {
    /// Returns the average of all elements in the array
    func average() -> Element { isEmpty ? .zero : sum() / Element(count) }
    /// Returns the average of all elements in the array as Floating Point type
    func average<T: FloatingPoint>() -> T { isEmpty ? .zero : T(sum()) / T(count) }
}
extension Collection where Element: BinaryFloatingPoint {
    /// Returns the average of all elements in the array
    func average() -> Element { isEmpty ? .zero : Element(sum()) / Element(count) }
}
let votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let votesTotal = votes.sum()                       // 55
let votesAverage = votes.average()                 // 5
let votesDoubleAverage: Double = votes.average()   // 5.5

如果您需要使用Decimal类型的总和,那么它已经被AdditiveArithmetic协议扩展方法所涵盖,因此您只需要实现平均值:

extension Collection where Element == Decimal {
    func average() -> Decimal { isEmpty ? .zero : sum() / Decimal(count) }
}

如果您需要对自定义结构的某个属性求和,我们可以扩展Sequence并创建一个以KeyPath作为参数来计算其总和的方法:

extension Sequence  {
    func sum<T: AdditiveArithmetic>(_ keyPath: KeyPath<Element, T>) -> T {
        reduce(.zero) { $0 + $1[keyPath: keyPath] }
    }
}

用法:

let users: [User] = [
    .init(name: "Steve", age: 45),
    .init(name: "Tim", age: 50)]

let ageSum = users.sum(\.age) // 95

并扩展集合以计算其平均值:

extension Collection {
    func average<I: BinaryInteger>(_ keyPath: KeyPath<Element, I>) -> I {
        sum(keyPath) / I(count)
    }
    func average<I: BinaryInteger, F: BinaryFloatingPoint>(_ keyPath: KeyPath<Element, I>) -> F {
        F(sum(keyPath)) / F(count)
    }
    func average<F: BinaryFloatingPoint>(_ keyPath: KeyPath<Element, F>) -> F {
        sum(keyPath) / F(count)
    }
    func average(_ keyPath: KeyPath<Element, Decimal>) -> Decimal {
        sum(keyPath) / Decimal(count)
    }
}

用法:

let ageAvg = users.average(\.age)                 // 47
let ageAvgDouble: Double = users.average(\.age)   // 47.5


 类似资料:
  • 问题内容: 我正在尝试使用下面的代码来计算用户输入的一组值的平均值,并将其显示在中,但它无法正常工作。假设用户输入7、4和5,该程序在应显示5.3时显示平均值。 代码有什么问题? 问题答案: 当您拥有增强的for循环时,为什么还要对索引使用笨拙的for循环?

  • 在此输入图像描述 你好,我刚刚了解了Javascript函数,想知道在JS中使用数组和函数找出平均值的方法。我已经链接了我的代码截图,你能帮我吗?

  • 问题内容: Y1961 Y1962 Y1963 Y1964 Y1965 Region 0 82.567307 83.104757 83.183700 83.030338 82.831958 US 1 2.699372 2.610110 2.587919 2.696451 2.846247 US 2 14.131355 13.690028 13.599516 13.649176 13.649046

  • 问题内容: 我希望我能弄清楚。我需要生成一个平均值为AVG_AMT(整数)的表,并且没有小数。它可以舍入或截断。这张桌子真的没关系。 这是我试图写的: 有什么建议? 问题答案:

  • 我试图使用下面的代码来计算用户输入的一组值的平均值,并将其显示在中,但它不能正常工作。例如,用户输入7、4和5,程序显示1作为平均值,而它应该显示5.3

  • 我一直在尝试编写一些代码来使用MapReduce查找数字的平均值。 我尝试使用全局计数器来实现我的目标,但是我无法在映射器的< code>map方法中设置计数器值,也无法在缩减器的< code>reduce方法中检索计数器值。 我是否必须在< code>map中使用全局计数器(例如,通过使用所提供的< code>Reporter的< code>incrCounter(key,amount))?或者