Round 8.2 MCQ --- Python Loops

Feb 12, 2014 • ablackpz

For intermediates:

How many times will the loop execute?

x = 0

for x in range(0, 5):

  print x

  x += 1

 

a) 4

b) 5

c) 6

d) 7

 

To verify learning:

 

What will be the output from this code?

data = [‘ATTT’,’GGCC’,’CACT’]

for seq in data:

 count = 0.0

 gc = 0.0

 for nuc in seq:

  count += 1

  if nuc == ‘G’ or nuc == ‘C’:

   gc += 1

 print gc/count

 

a) 0 1 0

b) 0 1 1

c) 0 1 0.5

d) 0 4 2

 

Practical exercise:

 

Your collaborator gave you 250 samples with metadata saved in the file ‘master.txt’.  You then took those 250 samples and ran an experiment, which showed that only a few samples were of interest.  The identifiers for those samples you saved in the file ‘ids.txt’.  Now you need to pull the metadata for those few interesting samples from the master file.  The code below is almost correct.  Find and fix the errors.

outfile = open(‘selectedData’, ‘w’)
ids = []
while newId in open(‘ids.txt’):
  ids.append(newId.rstrip)

for record in open(‘master.txt’):
  for item in ids:
   if id in data:
    outfile.write(data)
outfile.close()