I've made a program that takes a list of numbers and outputs the "triples." A triple is represented by x/y/z and x>=y>=z
For some reason, when executing my code, I'm getting a "memory error." I'm not sure why this is happening or how to solve it. I can only assume that it has something to do with managing memory incorrectly or making my program more efficiently.
This is lengthy, but I can't leave anything out because I have no Idea what the problem is.
So two questions... How do I fix this "memory error." And how can I make this program more efficient? (It's terribly slow as is.)
Program Output: [[6, 3, 1], [6, 2, 2], [6, 2, 1], [6, 1, 1], [5, 1, 1], [4, 2, 2], [4, 2, 1], [4, 1, 1], [3, 1, 1], [2, 2, 1], [2, 1, 1]] 11
l = [1,1,2,2,3,4,5,6]
def answer(l):
l.sort()
l.reverse()
triples = []
final = []
for first_main_counter in range(len(l)):
main_testing_number = l[first_main_counter]
divisors = []
divisors = [x for x in l if main_testing_number % x == 0]
del divisors[0]
for second_main_counter in range(len(divisors)):
second_main_testing_number = divisors[second_main_counter]
divisors2 = []
divisors2 = [x for x in divisors if second_main_testing_number % x == 0]
del divisors2[0]
for x in range(len(divisors2)):
triples.append([l[first_main_counter],divisors[second_main_counter],divisors2[x]])
seen = set()
for item in triples:
t = tuple(item)
if t not in seen:
final.append(item)
seen.add(t)
print(final)
print(len(final))
answer(l)
x/y/zmean? From your code I'm guessing you intend/to mean "divides". Also, you wrotez>=y>=z. Did you meanz>=y>=x?%%time-->Wall time: 572 µs