Faster random number list generation
This commit is contained in:
parent
fcfda903d2
commit
402d641f64
@ -1,14 +1,21 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
def k_uniq_rand_nums_of_n(n, k):
|
def k_uniq_rand_nums_of_n(n, k):
|
||||||
nums = [0]
|
nums = [0] * n
|
||||||
for i in xrange(1, n):
|
for i in xrange(n - 1, n - 1 - k, -1):
|
||||||
nums.append(i)
|
|
||||||
j = random.randint(0, i)
|
j = random.randint(0, i)
|
||||||
nums[i] = nums[j]
|
|
||||||
nums[j] = i
|
|
||||||
|
|
||||||
return nums[:k]
|
num_j = nums[j]
|
||||||
|
if num_j == 0:
|
||||||
|
num_j = j + 1
|
||||||
|
|
||||||
|
num_i = nums[i]
|
||||||
|
if num_i == 0:
|
||||||
|
num_i = i + 1
|
||||||
|
|
||||||
|
nums[i], nums[j] = num_j, num_i
|
||||||
|
|
||||||
|
return nums[-k:]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
echo "Generating input"
|
echo `date +%T` "Generating input"
|
||||||
python problem4.py $1 $2 > input
|
python problem4.py $1 $2 > input
|
||||||
echo "Generating output"
|
echo `date +%T` "Generating output"
|
||||||
cat input | python problem3.py $1 > output
|
cat input | python problem3.py $1 > output
|
||||||
echo "Sorting input"
|
echo `date +%T` "Sorting input"
|
||||||
sort -g input > input_sorted
|
sort -g input > input_sorted
|
||||||
echo "Diffing sorted input and output"
|
echo `date +%T` "Diffing sorted input and output"
|
||||||
diff -s input_sorted output
|
diff -s input_sorted output
|
||||||
rm input input_sorted output
|
rm input input_sorted output
|
Loading…
Reference in New Issue
Block a user