Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,236 questions

56,139 answers

573 users

How to count the numbers with an even number of digits in a vector with Rust

1 Answer

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

    Performance notes:
    ------------------
    - All operations are O(n) for n vector elements.
    - Digit counting uses integer division, avoiding string conversions.
    - No heap allocations beyond the vector itself.
    - Negative numbers handled safely.

    Security notes:
    ---------------
    - No unsafe blocks.
    - 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.
fn count_digits(value: i32) -> u32 {
    if value == 0 {
        return 1; // zero has one digit
    }

    let mut n = value.abs();
    let mut count = 0;

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

    count
}

/// Returns true if the number has an even number of digits.
fn has_even_digits(value: i32) -> bool {
    count_digits(value) % 2 == 0
}

/// Count how many numbers in a vector have an even number of digits.
/// Complexity: O(n)
fn count_even_digit_numbers(v: &[i32]) -> usize {
    v.iter().filter(|&&v| has_even_digits(v)).count()
}

/// Utility: print a vector in a readable format.
fn print_vec(v: &[i32]) {
    print!("[ ");
    for v in v {
        print!("{v} ");
    }
    print!("]");
}

fn main() {
    println!("=== Count Numbers with Even Number of Digits (Rust) ===\n");

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

    // Test 1
    print!("Test 1: ");
    print_vec(&a1);
    println!(" -> Count = {}", count_even_digit_numbers(&a1));

    // Test 2
    print!("Test 2: ");
    print_vec(&a2);
    println!(" -> Count = {}", count_even_digit_numbers(&a2));

    // Test 3
    print!("Test 3: ");
    print_vec(&a3);
    println!(" -> Count = {}", count_even_digit_numbers(&a3));

    // Test 4
    print!("Test 4: ");
    print_vec(&a4);
    println!(" -> Count = {}", count_even_digit_numbers(&a4));

    // Test 5: empty vector
    print!("Test 5: ");
    print_vec(&a5);
    println!(" -> Count = {}", count_even_digit_numbers(&a5));

    // Test 6: large numbers
    print!("Test 6: ");
    print_vec(&a6);
    println!(" -> Count = {}", count_even_digit_numbers(&a6));
}


/*
run:

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

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 2 hours ago by avibootz
...