Search code examples
pythonalgorithmdiscrete-mathematics

How to Determine List Repeats and Item Percentage for Desired Proportion in a New List


Working on a problem where I need to modify a list by adding another item, and I want this item to make up a specific percentage of the new total length of the list. I need help figuring out how to do this. Here’s the situation:

Have:

An existing list with a specific length (list_length). An additional item with a specific length (item_length) that I want to add. I want the added item to constitute exactly a certain percentage (target_percentage) of the new total length of the list.

Could someone help me with the following?

How do I calculate the total length of the new list so that the item makes up the exact percentage I want? How many times does the existing list need to be repeated to achieve this total length? How can I verify that the item actually makes up the desired percentage of the new list? How do I calculate the percentage of the existing list in the new list? Here’s an example of the values I’m using:

list_length = 1800 (the length of the existing list)

item_length = 30 (the length of the item to be added)

target_percentage = 0.05 (the item should make up 5% of the new list)

I tried the following Python code, but it didn’t work as expected:

import math

def calculate_list_and_item_details(list_length, item_length, target_percentage):
    # Convert target percentage to a fraction
    target_fraction = target_percentage / 100.0
    
    # Calculate the total length required for the item to be the target percentage of the total list
    required_list_length = (item_length * (1 - target_fraction)) / target_fraction
    
    # Calculate how many times the list needs to be repeated to reach the required length
    list_repeats = math.ceil(required_list_length / list_length)
    
    # Calculate the total length of the list with the repeated lists
    total_list_length = list_repeats * list_length
    total_length_with_item = total_list_length + item_length
    
    # Calculate the percentage of the item in the new total length
    item_percentage = (item_length / total_length_with_item) * 100
    
    # Calculate the percentage of the list in the new total length
    list_percentage = (total_list_length / total_length_with_item) * 100
    
    return {
        'list_repeats': list_repeats,
        'item_repeats': 1,  # The item is added once
        'item_percentage': item_percentage,
        'list_percentage': list_percentage
    }

# Example usage
list_length = 1800  # Length of the existing list
item_length = 30    # Length of the item to be added
target_percentage = 5  # Desired percentage of the item in the new total length

result = calculate_list_and_item_details(list_length, item_length, target_percentage)
print(f"List Repeats: {result['list_repeats']}")
print(f"Item Repeats: {result['item_repeats']}")
print(f"Item Percentage: {result['item_percentage']:.2f}%")
print(f"List Percentage: {result['list_percentage']:.2f}%")

Output:

List Repeats: 1

Item Repeats: 1

Item Percentage: 1.64%

List Percentage: 98.36%

The code calculates the number of times the existing list needs to be repeated but doesn’t properly ensure that the item actually constitutes the exact percentage of the total length. The issue arises because the total length needed for the item to meet the target percentage isn’t correctly matched with the number of repetitions of the existing list.

Can anyone provide guidance or ideas on how to fix this?


Solution

  • This comes down to a mathematical equation. If we define the length of the first list as 𝑎, and of the second 𝑏, and the required ratio 𝑟, then we need to get integer values 𝑥 and 𝑦 such that:

          𝑏𝑦 / (𝑎𝑥 + 𝑏𝑦) = 𝑟

    We can rewrite this as:

          𝑏y = 𝑟𝑎𝑥 + 𝑟𝑏𝑦

          𝑟𝑎𝑥 = (1−𝑟)𝑏𝑦

          𝑥/𝑦 = (1−𝑟)𝑏/𝑎𝑟

    If we assume that 𝑟 will have as precision 1 ten thousandth of a unit (i.e. 0.01%), we can use integer arithmetic when multiplying 𝑟 by 10000, and define:

          𝑥 = (1−𝑟)𝑏

          𝑦 = 𝑎𝑟

    ...which is a solution to the earlier equation. We can then find the greatest common divisor of 𝑥 and 𝑦 and divide both by that common divisor.

    Here is an implementation in Python:

    import math
    
    def solve(a, b, r):
        # convert r to an integer as units of 10000
        rn = round(10000 * r)
        # Number of times to repeat a:
        num = (10000 - rn) * b
        # Number of times to repeat b:
        denom = rn * a
        div = math.gcd(num, denom)  # Get common divisor to simplify fraction
        return num // div, denom // div
    

    With the example you have given in the question, the above function would be called as:

    x, y = solve(1800, 30, 0.05)
    

    The values 𝑥 and 𝑦 will be 19 and 60 respectively, meaning you should have 19 copies of the first list, and append 60 copies of the second list to achieve the desired ratio:

          60 * 30 / (19 * 1800 + 60 * 30) == 0.05