Skip to content

Suggestions #1

@Alf71

Description

@Alf71
# ORIGINAL #
def break_down_to_pow10(num):
    nums = []
    num_len = len(str(num)) - 1
    for pw in reversed(range(num_len)):
        nums.append(pow(10, pw))
    return nums
# RESULT: 
#For num = 67108864:
#Correct output: [60000000, 7000000, 100000, 8000, 800, 60, 4]
#Current output: [1000000, 100000, 10000, 1000, 100, 10, 1]

# NEW APPROACH 1: Base-10 Digit Decomposition
def break_down_to_pow10(num):
    nums = []
    num_str = str(num)
    num_len = len(num_str)
    for i, digit in enumerate(num_str):
        power = num_len - i - 1
        if digit != '0':
            nums.append(int(digit) * pow(10, power))
    return nums
# RESULT:
# For 67,108,864:
# [60000000, 7000000, 100000, 8000, 800, 60, 4]

# NEW APPROACH 2: Binary decomposition
def break_down_to_pow2(num):
    nums = []
    while num > 0:
        highest_power = 1 << (num.bit_length() - 1)
        nums.append(highest_power)
        num -= highest_power
    return nums
# RESULT:
# For 67,108,864:
# [33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions