| mannanovairat RandBits 2965 | Алексей Дворников, 633 RandBits 3051 | ||||
|---|---|---|---|---|---|
| f | 1 | import random | f | 1 | import random |
| 2 | 2 | ||||
| 3 | def randbits(p: int, n: int) -> int: | 3 | def randbits(p: int, n: int) -> int: | ||
| 4 | if n <= 0 or n > p: | 4 | if n <= 0 or n > p: | ||
| 5 | return 0 | 5 | return 0 | ||
| t | 6 | ones = random.sample(range(p), n) | t | 6 | bits = random.sample(range(p), n) |
| 7 | x = 0 | 7 | value = 0 | ||
| 8 | for pos in ones: | 8 | for bit in bits: | ||
| 9 | x |= 1 << pos | 9 | value |= 1 << bit | ||
| 10 | return x | 10 | return value | ||
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||