# This code compares two lists of tokens and gets a sample of the largest one # Javier Aguado-Orea # First of all one has to say how many sampling processes are going to be run numberIterations = raw_input('Number of iterations? ') iteration = int(float(numberIterations)) print iteration iterationb = iteration for i in range(iteration): itestr = str(iterationb) outputdadfile = 'output_dad_'+itestr+'.txt' outputchifile = 'output_chi_'+itestr+'.txt' iterationb = iterationb - 1 outputdad = open(outputdadfile, 'w') outputchi = open(outputchifile, 'w') # Then, it will open the input file as many interactions as indicated input = open('input.txt', 'r') # The input file is very restrictive for syntax # A plain text file with the name input.txt is required # The first line is a number: the amount of VERBS in the sample (e.g. 5) # Then, a series of four lines has to be included per verb: # First: number of tokens provided by the child with FIVE DIGITS (e.g. 00010) # Second: number of tokens provided by the adult with FIVE DIGITS (e.g. 00100) # Third: name of the verb (e.g. abrir) # Fourth: twelve numbers, all five digits, six for child: 1s, 2s, 3s, 1p, 2p, 3p and six for the adult: 1s, 2s, 3s, 1p, 2p, 3p (e.g. 00000 00000 00012 00000 00000 00000 00002 00000 00016 00001 00000 00001) " " " # Now, the following code reads the 1st line (amount of verbs) and prints it to the console # It's just to check that things are going fine with the input.txt file verbos = input.readline() print "verbos= "+ verbos verbos = int(verbos) # ----- while (verbos > 0): verbos = verbos - 1 # Now, the following code reads the amount of tokens by adult and child # it prints the difference, since sampling will change depending on that totalchi = "" totaldad = "" childstr = input.readline(6) dadstr = input.readline(6) child = int(childstr) dad = int(dadstr) diferencia = dad - child # ATTENTION HERE, WHAT IF THE CHILD'S SAMPLE IS LARGER THAN THE ADULT ONE diferenciastr = str(diferencia) # ----- # Now, it reads the name of the verb (e.g. abrir) verbo = input.readline() verbo = verbo.rstrip('\n') msg = diferenciastr + " for " + verbo print msg # ----- # Now, it reads all 12 numbers for the amount of tokens per form (1s, 2s, 3s, 1p, 2p, 3p) # SIX FOR THE CHILD # And creates a list of tokens per form, and adds them to "totalchi" J1s = input.readline(6) J1s = int(J1s) list = "" if J1s > 0: VJ1s = verbo + "1s" for n in range(0, J1s): list = VJ1s + "," + list totalchi = totalchi + list J2s = input.readline(6) J2s = int(J2s) list = "" if J2s > 0: VJ2s = verbo + "2s" for n in range(0, J2s): list = VJ2s + "," + list totalchi = totalchi + list J3s = input.readline(6) J3s = int(J3s) list = "" if J3s > 0: VJ3s = verbo + "3s" for n in range(0, J3s): list = VJ3s + "," + list totalchi = totalchi + list J1p = input.readline(6) J1p = int(J1p) list = "" if J1p > 0: VJ1p = verbo + "1p" for n in range(0, J1p): list = VJ1p + "," + list totalchi = totalchi + list J2p = input.readline(6) J2p = int(J2p) list = "" if J2p > 0: VJ2p = verbo + "2p" for n in range(0, J2p): list = VJ2p + "," + list totalchi = totalchi + list J3p = input.readline(6) J3p = int(J3p) list = "" if J3p > 0: VJ3p = verbo + "3p" for n in range(0, J3p): list = VJ3p + "," + list totalchi = totalchi + list totalchi = totalchi[:-1] # SIX FOR THE ADULT (DAD) # And creates a list of tokens per form, and adds them to "totaldad" JD1s = input.readline(6) JD1s = int(JD1s) list = "" if JD1s > 0: VJD1s = verbo + "1s" for n in range(0, JD1s): list = VJD1s + "," + list totaldad = totaldad + list JD2s = input.readline(6) JD2s = int(JD2s) list = "" if JD2s > 0: VJD2s = verbo + "2s" for n in range(0, JD2s): list = VJD2s + "," + list totaldad = totaldad + list JD3s = input.readline(6) JD3s = int(JD3s) list = "" if JD3s > 0: VJD3s = verbo + "3s" for n in range(0, JD3s): list = VJD3s + "," + list totaldad = totaldad + list JD1p = input.readline(6) JD1p = int(JD1p) list = "" if JD1p > 0: VJD1p = verbo + "1p" for n in range(0, JD1p): list = VJD1p + "," + list totaldad = totaldad + list JD2p = input.readline(6) JD2p = int(JD2p) list = "" if JD2p > 0: VJD2p = verbo + "2p" for n in range(0, JD2p): list = VJD2p + "," + list totaldad = totaldad + list JD3p = input.readline(6) JD3p = int(JD3p) list = "" if JD3p > 0: VJD3p = verbo + "3p" for n in range(0, JD3p): list = VJD3p + "," + list totaldad = totaldad + list totaldad = totaldad[:-1] # It follows a bit of general information for the output file verbo = verbo+'\n' #In a previous version I was printing the verb before the actual tokens # outputdad.write(verbo) # outputchi.write(verbo) # it converts strings to lists listatotaldad = totaldad.split(',') listatotalchi = totalchi.split(',') # And it randomises the largest to the smallest size import random # Expected situation: more tokens in the adult sample if diferencia > 0 : # First it writes the total items for the child totalchi = totalchi+'\n' outputchi.write(totalchi) # Then it samples the adult tokens listacortadad = random.sample(xrange(len(listatotaldad)), diferencia) listacortadad = [listatotaldad[i] for i in listacortadad] cortadad = ','.join(listacortadad) outputdad.write(cortadad) # Unexpected but possible: more tokens in the adult sample if diferencia < 0 : # First it writes the total items for the adult totaldad = totaldad+'\n' outputdad.write(totaldad) # Then it samples the adult tokens diferenciab = abs(diferencia) listacortachi = random.sample(xrange(len(listatotalchi)), diferenciab) listacortachi = [listatotalchi[i] for i in listacortachi] cortachi = ','.join(listacortachi) outputchi.write(cortachi) # Same amount of tokens in both samples if diferencia == 0 : totalchi = totalchi+'\n' outputchi.write(totalchi) totaldad = totaldad+'\n' outputdad.write(totaldad) espacio = '\n'+'\n' outputchi.write(espacio) outputdad.write(espacio) else: print "END OF FILE" print "END OF ITERATIONS"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question