Swift Coding Test Course, Calculating Average

In this course, we will solve an algorithm problem to calculate the average using the Swift programming language. This problem, frequently seen in coding tests for recruitment, is a good foundation for data processing. Additionally, it will be an opportunity to learn the basic syntax of Swift and how to manipulate arrays.

Problem Description

An array is given as follows. Write a function to calculate the average of all elements in this array. The elements of the array are integers.

Input

  • An integer array numbers is provided. (1 ≤ numbers.count ≤ 100, 0 ≤ numbers[i] ≤ 1000)

Output

  • Output the average value of the given array rounded to the nearest integer at the first decimal place.

Example

Input: [60, 82, 75, 90, 45]
Output: 70
Input: [100, 90, 80]
Output: 90

Solution Process

Step 1: Understanding the Problem

To understand the problem, one must acknowledge that to find the average, all elements of the given array should be summed and then divided by the number of elements. Additionally, it should be remembered that the average needs to be returned as an integer rounded to the nearest whole number.

Step 2: Input Handling

Swift provides various methods to handle arrays easily, so the input array can be used directly. For example, the reduce function can be used to calculate the sum of the array.

Step 3: Algorithm Implementation

Now, let’s implement the algorithm that we will actually use.

func average(numbers: [Int]) -> Int {
    // 1. Compute the total sum of the elements in the array.
    let total = numbers.reduce(0, +)
    
    // 2. Determine the number of elements in the array.
    let count = numbers.count
    
    // 3. Calculate the average and round it.
    let average = Double(total) / Double(count)
    return Int(round(average))
}

Step 4: Writing Test Cases

Based on the function we’ve written, let’s create a few test cases.

let test1 = average(numbers: [60, 82, 75, 90, 45]) // 70
let test2 = average(numbers: [100, 90, 80]) // 90
let test3 = average(numbers: [0, 0, 0, 0]) // 0
let test4 = average(numbers: [1, 2, 3, 4, 5, 6]) // 4
let test5 = average(numbers: [1, 2, 3]) // 2

Step 5: Checking Results and Output

Let’s output the results through testing. We will use the print function for this purpose.

print(test1) // 70
print(test2) // 90
print(test3) // 0
print(test4) // 4
print(test5) // 2

Complete Code

Now, let’s introduce the final code that integrates all the steps.

func average(numbers: [Int]) -> Int {
    let total = numbers.reduce(0, +)
    let count = numbers.count
    let average = Double(total) / Double(count)
    return Int(round(average))
}

// Test Cases
let test1 = average(numbers: [60, 82, 75, 90, 45]) // 70
let test2 = average(numbers: [100, 90, 80]) // 90
let test3 = average(numbers: [0, 0, 0, 0]) // 0
let test4 = average(numbers: [1, 2, 3, 4, 5, 6]) // 4
let test5 = average(numbers: [1, 2, 3]) // 2

print(test1)
print(test2)
print(test3)
print(test4)
print(test5)

Conclusion

Through this course, we learned how to solve the algorithm problem of calculating the average using Swift. The series of processes involved in summing array elements, calculating the average, and outputting the rounded result forms a foundation for data processing. The ability to solve such problems is very important for recruitment coding tests, so it is advisable to strengthen your skills through practice.

References