-1

So I'm now learning about nested pools and I don't really get it. my task is to return all matching pairs in following format:

[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]

so this is my code:

def matching_codons(complements, poolA, poolB):
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
final = []
 for i in poolA:
  for z in poolB:
    if i and z in complements:
      final.append()
      return(final)

It's not working and I don't know why, I don't quite get it. How can I make a statement so that the poolA and the poolB will match according to the dictionary provided?

Robin Dun
  • 7
  • 2
  • *"It's not working"* is a profoundly unhelpful problem statement, but: http://stackoverflow.com/q/15112125/3001761 – jonrsharpe Sep 21 '15 at 06:12
  • Mistakes: 1) you append nothing to the list :) 2) > > > if i and z in complements - compares ONLY keys of dictionary, not the values. 3) `i` and `z` equal to strings like 'AAG', 'TAC', etc. And we must to compare chars (additional string). See my simple solution below. – Sdwdaw Sep 21 '15 at 08:44

2 Answers2

0

Try to search for the complements in the other pool, instead of walking through every elements in poolA and poolB.

def _complements(complements, str):
    return ''.join([complements[i] for i in str])

def matching_codons(complements, poolA, poolB):
    final = []
    for i in poolA:
        if _complements(complements, i) in poolB:
            final.append((i, _complements(complements, i)))
    return final
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
print matching_codons(complements, poolA, poolB)

output

[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]
luoluo
  • 5,353
  • 3
  • 30
  • 41
  • thank you but it is told that the print statement have to be print(matching_codons(complements, poolA, poolB)). It said ' it produced output during importing when it was not meant to. Your submission should not contain any top-level calls to the print function. ' – Robin Dun Sep 21 '15 at 06:29
  • What hell does ` Your submission` mean, what are you looking for? – luoluo Sep 21 '15 at 06:35
  • the submission as in when I tried to put in the code :) – Robin Dun Sep 21 '15 at 06:38
  • You have to write the submission code yourself, as I know nothing about what to submit. I thought my answer is clear enough to your question. You just need to do some kind of rearrange. If you are only waiting for a correct answer without any trying, It's not a good idea. – luoluo Sep 21 '15 at 06:47
  • Nah i'm not going to submit it as mine, is just that I want to test out the output. – Robin Dun Sep 21 '15 at 06:56
0
>>> final = []
>>> for a in poolA:
...     for b in poolB:
...         count = 0
...         for x in range(len(a)):
...             if complements[a[x]] == b[x]:
...                 count += 1
...             else:
...                 break
...             if count == 3:
...                 final.append((a,b))
... 
>>> final
[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]
Sdwdaw
  • 1,037
  • 7
  • 14