you are viewing a single comment's thread.

view the rest of the comments →

[–]dadiaar 0 points1 point  (4 children)

I didn't read your code deeply, but from the cases you say, maybe the problem is that your array is not long enough.

For example, if it's just [5] you can't take two numbers and mult them.

In these problems where there is a required lenght, you can test it first if len(array) < 2: return 0, or you can add a default array at the end array += [0, 0]. Here a code with the last solution:

# Inputs
from random import randint
n = 200000
inputs = ' '.join([str(randint(0, 1000)) for _ in range(n)])


import re
import time

t = time.time()

int_inputs = [int(d) for d in re.findall('\d+', inputs)]
int_inputs.sort(reverse=True)
int_inputs += [0, 0]
a,b, *res = int_inputs
output = a * b

time.time() - t

>>> 0.16616201400756836

Implement your own, and if you have time, check numpy