Source

IntroToAlgorithm3ed / src / NCKUAlg / ProgHW5 / genCommands.py

Full commit
#! /usr/bin/python2.7
# -*- coding: UTF-8 -*-
'''
Copyright (C) 2011 Eric Chen(G+ page:HencriceFOSS, gmail:hencrice+FOSS+introtoalgorithm3ed@gmail.com)

This file is a part of introtoalgorithm3ed.

introtoalgorithm3ed is a free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this source file; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''
from random import randint,choice #10**6
possibleCommands=('m','f','u')
nodesAlreadyCreated=set() #add remove
numOfCmdGonnaGenerate=10**6
with open('./testLong.txt', 'w') as cmdFile:
    for i in xrange(numOfCmdGonnaGenerate):
        cmd=choice(possibleCommands)
        if cmd=='m':
            newValue=randint(1,numOfCmdGonnaGenerate)
            while newValue in nodesAlreadyCreated:
                newValue=randint(1,numOfCmdGonnaGenerate)
            cmdFile.write('{0} {1}\n'.format(cmd,newValue))
            nodesAlreadyCreated.add(newValue)
        elif cmd=='f' and len(nodesAlreadyCreated)>0:
            valWantToFind=choice(tuple(nodesAlreadyCreated))
            cmdFile.write('{0} {1}\n'.format(cmd,valWantToFind))
        elif len(nodesAlreadyCreated)>1:
            value1=choice(tuple(nodesAlreadyCreated))
            nodesAlreadyCreated.remove(value1)
            value2=choice(tuple(nodesAlreadyCreated))
            nodesAlreadyCreated.add(value1)
            cmdFile.write('{0} {1} {2}\n'.format(cmd,value1,value2))