我想使用几个函数(例如minimum,maximum ...)并使用函数determineStatistics返回所有结果,以将exam-block变为true。
我尝试添加一个init并返回统计类。但我认为这不是解决问题的正确方法吗?
class statistic{
var Minimum:Int?;
var Maximum:Int?;
var Average:Double?;
var Median:Double?;
init (Minimum:Int?, Maximum:Int?, Average:Double?, Median:Double?){
self.Minimum=Minimum;
self.Maximum=Maximum;
self.Average=Average;
self.Median=Median;
}
}
func Summ(input input:[Double]) -> Double? {
var memory:Double=0;
if input.count == 0{
return nil
}
for number in input{
memory=memory+number
}
return memory;
}
func Average(input input:[Double]) -> Double? {
var sum=Summ(input: input)
var countedInput:Double = Double(input.count)
if countedInput == 0.0{
return nil
}
return sum!/countedInput;
}
func Minimum(input input:[Int]) -> Int? {
var memory:Int?=0;
if input.count == 0{
return nil
}
for i in input{
for j in input{
if i<j{
memory=i;
}
}
}
return memory;
}
func Maximum(input input:[Int]) -> Int? {
var memory:Int=0;
if input.count == 0{
return nil
}
for i in input{
for j in input{
if i>j{
memory=i;
}
}
}
return memory;
}
func sort(input input:[Int]) -> [Int] {
var data = input
for i in 0..<data.count-1{
for j in 0..<data.count-1{
if (data[j] > data[j+1]){
var temp = data[j+1]
data[j+1] = data[j]
data[j+1] = temp
}
}
}
return data;
}
func Median(input input:[Int]) -> Int? {
if input.count == 0{
return nil
}
var median = sort(input: input)
var medi = input.count
var me = medi/2
return median[me]
}
//The function determineStatistics() should output a statistic-object with all calculable datas
func determineStatistics(_ input:[Int]) -> statistic {
var result = statistic(Minimum: Minimum(input: input),Maximum: Maximum(input: input),Average: Average(input: input),Median: Median(input: input));
return result;
}
//here I get the error: Cannot convert value of type [Int] to expected argument type [Double]
//exam block - sould turn true
let a4adata = determineStatistics([1,2,3,4,5,6,7,8,9,10]);
let a4a = a4adata.Minimum == 1 && a4adata.Maximum == 10 && a4adata.Average == 5.5 && a4adata.Median == 6;
let a4bdata = determineStatistics([]);
let a4b = a4bdata.Minimum == nil && a4bdata.Maximum == nil && a4bdata.Average == nil && a4bdata.Median == nil;
let a4 = a4a && a4b;
我不知道从哪里开始修复问题并将考试块变为真。
答案 0 :(得分:1)
class Statistic {
var minimum: Int?
var maximum: Int?
var average: Double?
var median: Double?
}
停止检查数组计数
if input.count == 0 {
return nil
}
并使用优化
if input.isEmpty {
return nil
}
甚至更加快捷
guard !input.isEmpty else { return nil }
并且您可能将在Statistic类中声明为Double的中值与声明为Int?
的中值函数的输出混合试试这个。已经在这里和那里做了一些事情,它可以做得更好,所以尽量清理一下。还有改进的余地。
struct Statistic {
var minimum: Int?
var maximum: Int?
var average: Double?
var median: Int?
}
func summFunc(input input: [Double]) -> Double? {
guard !input.isEmpty else { return nil }
return input.reduce(0, +)
}
func averageFunc(input input: [Double]) -> Double? {
guard !input.isEmpty else { return nil }
var sum = summFunc(input: input)
return sum! / Double(input.count)
}
func minimumFunc(input input: [Int]) -> Int? {
guard !input.isEmpty else { return nil }
return input.minElement()
}
func maximumFunc(input input: [Int]) -> Int? {
guard !input.isEmpty else { return nil }
return input.maxElement()
}
func sortFunc(input input: [Int]) -> [Int] {
return input.sorted { $0 < $1 }
}
func medianFunc(input input: [Int]) -> Int? {
guard !input.isEmpty else { return nil }
var median = sortFunc(input: input)
let medi = input.count
let me = medi/2
return median[me]
}
func determineStatistics(_ input:[Int]) -> Statistic {
return Statistic(minimum: minimumFunc(input: input),
maximum: maximumFunc(input: input),
average: averageFunc(input: input.map { Double($0) } ),
median: medianFunc(input: input))
}
let a4adata = determineStatistics([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
let a4a = a4adata.minimum == 1 && a4adata.maximum == 10 && a4adata.average == 5.5 && a4adata.median == 6
let a4bdata = determineStatistics([])
let a4b = a4bdata.minimum == nil && a4bdata.maximum == nil && a4bdata.average == nil && a4bdata.median == nil
let a4 = a4a && a4b
我使用struct而不是类,所以我不必编写初始化器。可能更多的var可以更改为let。