samedi 19 décembre 2015

pick a random number not in a list

I have a list:

> S = [1,8,93,3,8]

I need to pick a random number that is not in the list. I am more concerned about time complexity O(n).

import random

S=[1,8,93,3,8]
m = max(S)
for x in xrange(m):
  rand = random.randint(1,m)
  if rand not in S:
      print rand
  else:
      print "Number - %d found in the array" % rand 
      break

I did not try list comprehension




Aucun commentaire:

Enregistrer un commentaire