jeudi 13 août 2020

How to modify code to avoid Overflow error when applying random.sample() large integer inputs?

The program shows "OverflowError: Python int too large to convert to C ssize_t" for large integer inputs(which are mandatory to test the efficiency of the program for all the boundary cases).How do I deal with this error?


import random
import sys
 
sys.setrecursionlimit(10**6)

t=int(input())

N =[]
K =[]
B =[]
while 1<=t<=20 :
    n,k,b= input().split()
    n,k,b = [int(n), int(k),int(b)]
    t=t-1
    N.append(n)
    K.append(k)
    B.append(b)
if b >= 1 and b <= (10**5) and n >= 1 and k <= (10**18) and b <= k :
   i1=0
   for val in K:
        n=N[i1]
        k=K[i1]
        b=B[i1]
        i1=i1+1
        print('i entered for loop')
        if sum(list(range(1, k+1))) >= n:
            print(' i entered if loop')
            def possibilities():
                     p = random.sample(range(1, k+1), b)
                     if sum(p) == n:
                        for i in range(0,b):
                            print(p[i],end=" ")
                        print("\r")
                     else:
                         possibilities()
             
            possibilities()         
            
                        
        else:
          print(-1)

       



Aucun commentaire:

Enregistrer un commentaire