English 中文(简体)
找到了10个最大力量,这些力量甚至分成一个人数清单。
原标题:Finding the largest power of 10 that divides evenly into a list of numbers
  • 时间:2023-09-21 22:09:07
  •  标签:
  • python
  • math

I m试图缩小一组数字,以输入DP子集-总算法。 (如果数字太大,就会打击。) 具体来说,我需要找到10人的最大力量,能够在不失去精确性的情况下将人数分成几个部分。 我的工作是例行的,但由于工作往往在 lo中进行,我希望工作比我所介绍的简陋武力方法更快。 我的数字是 Dec。

from decimal import Decimal
import math

def largest_common_power_of_10(numbers: list[Decimal]) -> int:
    """
    Determine the largest power of 10 in list of numbers that will divide into all numbers
    without losing a significant digit left of the decimal point
    """
    min_exponent = float( inf )
    for num in numbers:
        if num != 0:
            # Count the number of trailing zeros in the number
            exponent = 0
            while num % 10 == 0:
                num //= 10
                exponent += 1
            min_exponent = min(min_exponent, exponent)

    # The largest power of 10 is 10 raised to the min_exponent
    return int(min_exponent)


decimal_numbers = [Decimal("1234"), Decimal("5000"), Decimal("200")]
result = largest_common_power_of_10(decimal_numbers)
assert(result == 0)
decimal_numbers = [Decimal(470_363_000.0000), Decimal(143_539_000.0000), Decimal(1_200_000.0000)]
result = largest_common_power_of_10(decimal_numbers)
assert(result == 3)
divisor = 10**result
# Later processing can use scaled_list
scaled_list = [x/divisor for x in decimal_numbers]
assert(scaled_list == [Decimal( 470363 ), Decimal( 143539 ), Decimal( 1200 )])
reconstituted_list = [x * divisor for x in scaled_list]
assert(reconstituted_list == decimal_numbers)
问题回答

如果您的名单上所有的人都感到愤怒,就可以立即与数学图书馆一起这样做。

import math

def largest_common_power_of_10(numbers):
    
    # get the greatest power of 10 that divides all numbers in list
    gcd_nums = math.gcd(*numbers)
    gcd = [x for x in range(1, len(str(gcd_nums))) if gcd_nums % math.pow(10, x) == 0]

    if len(gcd) == 0:
        return 0
    else:
        gcp = max(gcd)
        return gcp

Examples:

numbers = [11230000,125,44500000]
largest_common_power_of_10(numbers)
# 0

numbers = [11230000,1540,44500000]
largest_common_power_of_10(numbers)
# 1

numbers = [11230000,1540000,44500000]
largest_common_power_of_10(numbers)
# 4

注:

这只可能与3.9和更大范围的工作,因为在释放时,数学.gcd()明显接受名单。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签