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

1 Answer

0 votes
# ============================================================
# Architecture notes:
# ------------------------------------------------------------
# This program demonstrates a modular design:
# - count_digits: counts digits using integer math.
# - even_digits?: checks if a number has an even digit count.
# - count_even_digit_numbers: 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.
# - Ruby integers are arbitrary precision, so no overflow concerns.
#
# Security notes:
# ------------------------------------------------------------
# - No unsafe operations.
# - 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.
def count_digits(value)
  return 1 if value == 0  # zero has one digit

  n = value.abs
  count = 0

  while n > 0
    n /= 10
    count += 1
  end

  count
end

# Returns true if the number has an even number of digits.
def even_digits?(value)
  count_digits(value).even?
end

# Count how many numbers in an array have an even number of digits.
# Complexity: O(n)
def count_even_digit_numbers(arr)
  arr.count { |v| even_digits?(v) }
end

# Print an array in a readable format.
def print_array(arr)
  print "[ "
  arr.each { |v| print "#{v} " }
  print "]"
end

# ============================================================
# Main: multiple test cases, including edge cases.
# ============================================================

puts "=== Count Numbers with Even Number of Digits (Ruby) ==="
puts

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

# Test 1
print "Test 1: "
print_array(a1)
puts " -> Count = #{count_even_digit_numbers(a1)}"

# Test 2
print "Test 2: "
print_array(a2)
puts " -> Count = #{count_even_digit_numbers(a2)}"

# Test 3
print "Test 3: "
print_array(a3)
puts " -> Count = #{count_even_digit_numbers(a3)}"

# Test 4
print "Test 4: "
print_array(a4)
puts " -> Count = #{count_even_digit_numbers(a4)}"

# Test 5: empty array
print "Test 5: "
print_array(a5)
puts " -> Count = #{count_even_digit_numbers(a5)}"

# Test 6: large numbers
print "Test 6: "
print_array(a6)
puts " -> Count = #{count_even_digit_numbers(a6)}"



=begin
run:

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

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

=end

 



answered Sep 21 by avibootz

Related questions

...