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

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.
    - No unnecessary allocations; uses standard library collections.

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

object EvenDigitCounter {

  // 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.
  def countDigits(value: Int): Int = {
    if (value == 0) return 1 // zero has one digit

    var n = math.abs(value)
    var count = 0

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

    count
  }

  // Returns true if the number has an even number of digits.
  def hasEvenDigits(value: Int): Boolean =
    countDigits(value) % 2 == 0

  // Count how many numbers in an array have an even number of digits.
  // Complexity: O(n)
  def countEvenDigitNumbers(arr: Array[Int]): Int =
    arr.count(hasEvenDigits)

  // Utility: print an array in a readable format.
  def printArray(arr: Array[Int]): Unit = {
    print("[ ")
    arr.foreach(v => print(s"$v "))
    print("]")
  }

  def main(args: Array[String]): Unit = {
    println("=== Count Numbers with Even Number of Digits (Scala) ===")
    println()

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

    // Test 1
    print("Test 1: ")
    printArray(a1)
    println(s" -> Count = ${countEvenDigitNumbers(a1)}")

    // Test 2
    print("Test 2: ")
    printArray(a2)
    println(s" -> Count = ${countEvenDigitNumbers(a2)}")

    // Test 3
    print("Test 3: ")
    printArray(a3)
    println(s" -> Count = ${countEvenDigitNumbers(a3)}")

    // Test 4
    print("Test 4: ")
    printArray(a4)
    println(s" -> Count = ${countEvenDigitNumbers(a4)}")

    // Test 5: empty array
    print("Test 5: ")
    printArray(a5)
    println(s" -> Count = ${countEvenDigitNumbers(a5)}")

    // Test 6: large numbers
    print("Test 6: ")
    printArray(a6)
    println(s" -> Count = ${countEvenDigitNumbers(a6)}")
  }
}


/*
run:

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

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

...