How to count the numbers with even number of digits in an array with Swift

1 Answer

0 votes
/*
    Architecture notes:
    -------------------
    This program demonstrates a modular design:
    - countDigits: counts digits using integer math.
    - hasEvenDigits: checks if a number has an even digit count.
    - countEvenDigitNumbers: processes an array and returns the count.
    - Multiple test cases in main.

    Performance notes:
    ------------------
    - All operations are O(n) for n array elements.
    - Digit counting uses integer division, avoiding string conversions.
    - Negative numbers handled safely.
    - Uses Swift's standard library for clarity and safety.

    Security notes:
    ---------------
    - No unsafe operations.
    - No unchecked indexing.
    - No external dependencies.
*/

// Count the number of digits in an integer.
// Complexity: O(log n) due to repeated division by 10.
// Pitfall: negative numbers must be handled correctly.
func countDigits(_ value: Int) -> Int {
    if value == 0 { return 1 }  // zero has one digit

    var n = abs(value)
    var count = 0

    while n > 0 {
        n /= 10
        count += 1
    }

    return count
}

// Returns true if the number has an even number of digits.
func hasEvenDigits(_ value: Int) -> Bool {
    countDigits(value) % 2 == 0
}

// Count how many numbers in an array have an even number of digits.
// Complexity: O(n)
func countEvenDigitNumbers(_ arr: [Int]) -> Int {
    arr.filter { hasEvenDigits($0) }.count
}

// Utility: print an array in a readable format.
func printArray(_ arr: [Int]) {
    print("[ ", terminator: "")
    arr.forEach { print("\($0) ", terminator: "") }
    print("]")
}

// ------------------------------------------------------------
// Main: multiple test cases, including edge cases.
// ------------------------------------------------------------

print("=== Count Numbers with Even Number of Digits (Swift) ===\n")

// Test cases
let a1 = [12, 345, 2, 6, 7896]
let a2 = [0, -22, 1000, -7]
let a3 = [1, 3, 5]          // No even-digit numbers
let a4 = [10, 99, 1001]     // All even-digit numbers
let a5: [Int] = []          // Edge case: empty array
let a6 = [-100000, 500000]  // Large numbers

// Test 1
print("Test 1: ", terminator: "")
printArray(a1)
print(" -> Count = \(countEvenDigitNumbers(a1))")

// Test 2
print("Test 2: ", terminator: "")
printArray(a2)
print(" -> Count = \(countEvenDigitNumbers(a2))")

// Test 3
print("Test 3: ", terminator: "")
printArray(a3)
print(" -> Count = \(countEvenDigitNumbers(a3))")

// Test 4
print("Test 4: ", terminator: "")
printArray(a4)
print(" -> Count = \(countEvenDigitNumbers(a4))")

// Test 5: empty array
print("Test 5: ", terminator: "")
printArray(a5)
print(" -> Count = \(countEvenDigitNumbers(a5))")

// Test 6: large numbers
print("Test 6: ", terminator: "")
printArray(a6)
print(" -> Count = \(countEvenDigitNumbers(a6))")


/*
run:

=== Count Numbers with Even Number of Digits (Swift) ===

Test 1: [ 12 345 2 6 7896 ]
 -> Count = 2
Test 2: [ 0 -22 1000 -7 ]
 -> Count = 2
Test 3: [ 1 3 5 ]
 -> Count = 0
Test 4: [ 10 99 1001 ]
 -> Count = 3
Test 5: [ ]
 -> Count = 0
Test 6: [ -100000 500000 ]
 -> Count = 2

*/

 



answered Sep 21 by avibootz

Related questions

...