Example usage:

  Blog    |     February 01, 2026

To solve this problem, we need to determine the price of each item based on a hidden rule. The hidden rule states that the price of an item is the product of the number of vowels and the number of consonants in its name. Vowels are defined as 'a', 'e', 'i', 'o', 'u' (case-insensitive), and consonants are all other alphabetic characters. Non-alphabetic characters are ignored.

Approach

  1. Problem Analysis: The task involves processing each item's name to count vowels and consonants. The price is then calculated as the product of these two counts.
  2. Key Insight: For each character in the item's name, check if it is a vowel, a consonant, or a non-alphabetic character. Only vowels and consonants contribute to the price calculation.
  3. Algorithm Selection: Iterate through each character in the item name:
    • Convert the character to lowercase to handle case insensitivity.
    • Count vowels and consonants separately.
    • Non-alphabetic characters are skipped.
  4. Complexity Analysis: The solution processes each character in the item name once, resulting in a time complexity of O(n) per item, where n is the length of the item name. This is efficient for typical input sizes.

Solution Code

def calculate_price(item_name):
    item_name = item_name.lower()
    vowels = 'aeiou'
    vowel_count = 0
    consonant_count = 0
    for char in item_name:
        if char in vowels:
            vowel_count += 1
        elif char.isalpha():
            consonant_count += 1
    return vowel_count * consonant_count
items = ["Apple", "Banana", "Cherry", "Dragon Fruit", "Eggplant"]
prices = [calculate_price(item) for item in items]
print(prices)

Explanation

  1. Function calculate_price(item_name):
    • Converts the item name to lowercase to ensure case insensitivity.
    • Initializes counters for vowels and consonants.
    • Iterates over each character in the item name:
      • If the character is a vowel, increments the vowel count.
      • If the character is an alphabetic consonant, increments the consonant count.
      • Non-alphabetic characters are ignored.
    • Returns the product of vowel and consonant counts as the price.
  2. Example Usage:
    • Processes a list of items to compute their prices.
    • For "Apple": 2 vowels (a, e) and 3 consonants (p, p, l) → price = 2 * 3 = 6.
    • For "Banana": 3 vowels (a, a, a) and 3 consonants (b, n, n) → price = 3 * 3 = 9.
    • For "Cherry": 2 vowels (e, y) and 4 consonants (c, h, r, r) → price = 2 * 4 = 8.
    • For "Dragon Fruit": 5 vowels (a, o, u, i, u) and 8 consonants (d, r, g, n, f, r, t) → price = 5 * 8 = 40.
    • For "Eggplant": 2 vowels (e, a) and 6 consonants (g, g, p, l, n, t) → price = 2 * 6 = 12.
    • The output for the example list is [6, 9, 8, 40, 12].

This approach efficiently computes the hidden price for each item by leveraging simple character checks and arithmetic operations.


Request an On-site Audit / Inquiry

SSL Secured Inquiry