Reputation: 21
I want to use several functions (like minimum, maximum...) and return all results with the function determineStatistics to turn the exam-block to true.
I tried to add an init and return the statistic class. But I don't think that´s the right way to solve the problem?
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;
I don´t know where to start fixing the problem and turn the exam-block to true.
Upvotes: 0
Views: 60
Reputation: 1592
man please if you have to do it in Swift, declare class with uppercase and variables with lowercase and functions start with lowercase.
class Statistic {
var minimum: Int?
var maximum: Int?
var average: Double?
var median: Double?
}
stop checking array count
if input.count == 0 {
return nil
}
and use optimised
if input.isEmpty {
return nil
}
or even more swiftier
guard !input.isEmpty else { return nil }
and you are probably mixing median declared as Double in Statistic class with output of median function declared as Int?
try this. have done some bits here and there and it could be done better so try to clean it up a bit more. there is room for improvement.
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
and I have used struct instead of class so I don't have to write the initialiser. probably lot more var's can be changed to let.
Upvotes: 1