samedi 25 avril 2015

create and insert randomly duplicates

This is not probably a common question but I want to CREATE duplicates and INSERT it randomly in my String. Just like the following example.

I have this file :

AppleStrawberryBananaCitrusOrange

And I expected this kind of output :

trusppleStrawbeApplertrusryBananaCitrusOrangepple

In this case my program randomly select a substring of length '4' : 'pple' and 'trus' and duplicates him 'twice(2)' before the insertion.

I think that I could run the program by using the fonction copy with copy.copy() and copy.insert() but I don't really know how to use it randomly.

For the moment;I just write the part of the code for read and write and something else:

import copy

chain='*'
contain = ''

file= raw_input ('Filename:')


x = open(file,'r')

for line in x:
                
if not(chain in line):
        contain+=line
        e=copy.copy(contain[4:8])
        f=copy.copy(contain[8:12])


y = open('copi','w')


y.write(contain)
y.write(f)



x.close()

Result:

AppleStrawberryBananaCitrusOrange
awbe

As you can see; it doesn't really work like I want. :(

Thanks for your help




Aucun commentaire:

Enregistrer un commentaire