Faster random number list generation

master
Abhinav Sarkar 2012-05-31 21:14:34 +05:30
parent fcfda903d2
commit 402d641f64
2 changed files with 17 additions and 10 deletions

View File

@ -1,14 +1,21 @@
import random
def k_uniq_rand_nums_of_n(n, k):
nums = [0]
for i in xrange(1, n):
nums.append(i)
nums = [0] * n
for i in xrange(n - 1, n - 1 - k, -1):
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__':
import sys

View File

@ -1,9 +1,9 @@
echo "Generating input"
echo `date +%T` "Generating input"
python problem4.py $1 $2 > input
echo "Generating output"
echo `date +%T` "Generating output"
cat input | python problem3.py $1 > output
echo "Sorting input"
echo `date +%T` "Sorting input"
sort -g input > input_sorted
echo "Diffing sorted input and output"
echo `date +%T` "Diffing sorted input and output"
diff -s input_sorted output
rm input input_sorted output