In [10]:
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [11]:
size = len(l)
size
Out[11]:
In [12]:
import random
In [18]:
## com repetidos
result = []
for i in range(len(l)):
position = int(random.random() * size)
result.append(l[position])
In [19]:
result
Out[19]:
In [22]:
## sem repetidos bigO(n**2)
result = []
for i in range(len(l)):
while True:
position = int(random.random() * size)
value = l[position]
if value not in result:
break
result.append(value)
In [23]:
result
Out[23]:
In [26]:
## sem repetidos bigO(n) space(n)
result = []
has_map = {}
for i in range(len(l)):
while True:
position = int(random.random() * size)
value = l[position]
if value not in has_map:
break
has_map[value] = True
result.append(value)
In [27]:
result
Out[27]:
In [30]:
## sem repetidos O(n)
result = l[:]
for i in range(2 * len(l), 0, -1):
p1 = int(random.random() * size)
p2 = int(random.random() * size)
aux = result[p1]
result[p1] = result[p2]
result[p2] = aux
result
Out[30]:
In [32]:
## sem repetidos O(n)
result = l[:]
for i in range(0, len(l)):
p = int(random.random() * size)
aux = result[i]
result[i] = result[p]
result[p] = aux
result
Out[32]:
In [ ]: