| Dimitry RandBits 3459 | Сагура Николай, 392, сев. филиал RandBits 2890 | ||||
|---|---|---|---|---|---|
| t | 1 | import random | t | 1 | import random |
| 2 | 2 | ||||
| 3 | def randbits(p, n): | 3 | def randbits(p, n): | ||
| 4 | if n > p or n < 1: | 4 | if n > p or n < 1: | ||
| 5 | return 0 | 5 | return 0 | ||
| 6 | ones_positions = random.sample(range(p), n) | 6 | ones_positions = random.sample(range(p), n) | ||
| 7 | x = 0 | 7 | x = 0 | ||
| 8 | for pos in ones_positions: | 8 | for pos in ones_positions: | ||
| 9 | x |= 1 << pos | 9 | x |= 1 << pos | ||
| 10 | return x | 10 | return x | ||
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||