I'm working on a solution to the Following problem on Hackerrank, and I seem to have found a function that works for the purpose. However, I feel as though this is a very overdesigned solution to a problem that appears, at first glance, to be simple. This is what I've come up with so far:
This code takes in two arrays, finds every pair of numbers from 1 to 100, multiplies them together, and tests if the product is a factor of all elements in the first array. If so, it appends the product to the fact array. Then, it tests every element in the fact array and checks if the element evenly divides every element in the second array. If so, it loads that element into a new list. Finally, it returns the length of the new list
I've run the code through a few static analyzers but they became unhelpful after a point. I'm looking for ways to cut this code down and reduce its complexity and depth. Any comments or criticisms are welcome!
Apologies if I've made any mistakes in my question-asking etiquette here, feel free to let me know if this needs more context and thanks in advance!
def get_total_x(input_factors: int, test_numbers: int) -> int: fact = list() output = list() for mult1 in range(100): for mult2 in range(100): # find all multiples of mult1 and mult2 that can be factored # into all numbers in a, while ignoring zero. if all(v == 0 for v in [(mult1 * mult2) % factor for factor in input_factors]) and (mult1 * mult2 != 0): fact.append(mult1 * mult2) seen = set() seen_add = seen.add # remove duplicates, may be able to cut out? fact = [x for x in fact if not (x in seen or seen_add(x))] for test in fact: # check for all numbers from the previous loop that divide b cleanly. if all(w == 0 for w in [factor2 % test for factor2 in test_numbers]): output.append(test) return len(output) if __name__ == '__main__': arr = (2, 4) brr = (16, 32, 96) total = get_total_x(arr, brr) print(total)