Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ankit92ios/410b3333e29d90c2c47fb1de9eef52fb to your computer and use it in GitHub Desktop.
Save ankit92ios/410b3333e29d90c2c47fb1de9eef52fb to your computer and use it in GitHub Desktop.
Day 0: Mean, Median, and Mode
/*
10 Days of Statistics
Objective
In this challenge, we practice calculating the mean, median, and mode. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an array, , of integers, calculate and print the respective mean, median, and mode on separate lines. If your array contains more than one modal value, choose the numerically smallest one.
Note: Other than the modal value (which will always be an integer), your answers should be in decimal form, rounded to a scale of decimal place (i.e., , format).
Input Format
The first line contains an integer, , denoting the number of elements in the array.
The second line contains space-separated integers describing the array's elements.
Constraints
, where is the element of the array.
Output Format
Print lines of output in the following order:
Print the mean on a new line, to a scale of decimal place (i.e., , ).
Print the median on a new line, to a scale of decimal place (i.e., , ).
Print the mode on a new line; if more than one such value exists, print the numerically smallest one.
Sample Input
10
64630 11735 14216 99233 14470 4978 73429 38120 51135 67060
Sample Output
43900.6
44627.5
4978
Explanation
Mean:
We sum all elements in the array, divide the sum by , and print our result on a new line.
Median:
To calculate the median, we need the elements of the array to be sorted in either non-increasing or non-decreasing order. The sorted array . We then average the two middle elements:
and print our result on a new line.
Mode:
We can find the number of occurrences of all the elements in the array:
4978 : 1
11735 : 1
14216 : 1
14470 : 1
38120 : 1
51135 : 1
64630 : 1
67060 : 1
73429 : 1
99233 : 1
Every number occurs once, making the maximum number of occurrences for any number in . Because we have multiple values to choose from, we want to select the smallest one, , and print it on a new line.
*/
import Foundation
func mean(numbers: [Int]) -> Double {
var sum = 0.0
for number in numbers {
sum += Double(number)
}
let mean = sum / Double(numbers.count)
return mean
}
func median(numbers: [Int]) -> Double {
let sortedNumbers = numbers.sorted(by: { num1, num2 in
return num1 < num2 })
let midIndex = numbers.count / 2
var median = Double(sortedNumbers[midIndex])
if(numbers.count % 2 == 0){
median = Double(sortedNumbers[midIndex] + sortedNumbers[midIndex - 1]) / Double(2)
}
return median
}
func mode(numbers: [Int]) -> Int {
let sortedNumbers = numbers.sorted(by: { num1, num2 in
return num1 < num2 })
var count = 0
var current = sortedNumbers[0]
var mode = 0
var highOccurance = 0
for var i in 0..<sortedNumbers.count{
if(sortedNumbers[i] == current){
count = count + 1
}else{
count = 1
current = sortedNumbers[i]
}
if(count > highOccurance){
highOccurance = count
mode = sortedNumbers[i]
}
}
// var highestPair: (key: Int, value: Int) = (0, 0)
// for (key, value) in occurrences {
// highestPair = (value > highestPair.value) ? (key, value) : highestPair
// }
// print(highestPair)
// print(sortedNumbers)
// print(occurrences)
return mode
}
//let number = Int(readLine(strippingNewline: true)!)!
//let arrValue = readLine(strippingNewline: true)!.characters
// .split {$0 == " "}
// .map (Int.init)
//print(mean(numbers: arrValue))
//print(median(numbers: arrValue))
//print(mode(numbers: arrValue))
//let arrValue = [1,1,1,2,2,2,3,3]
//print(mode(numbers: arrValue))
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let arTemp = readLine() else { fatalError("Bad input") }
let ar: [Int] = arTemp.split(separator: " ").map {
if let arItem = Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
return arItem
} else { fatalError("Bad input") }
}
guard ar.count == n else { fatalError("Bad input") }
let result = mean(numbers: ar)
let result2 = median(numbers: ar)
let result3 = mode(numbers: ar)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
fileHandle.write(String(result2).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
fileHandle.write(String(result3).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment