Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does modulo have to be performed during every iteration?
    primarykey
    data
    text
    <p>This is one of those questions where I stumbled upon the right answer, but I don't understand why it's the right one and Wikipedia didn't help. For Rosalind, I wrote a simple script for getting the number of all the possible RNA sequences from a protein string (modulo 1,000,000). I know it's not the most efficient possible code (partly because I recycle bits from previous things I've made), but here it is:</p> <pre><code>protein = """&lt;large protein string&gt;""" protein = ''.join(protein.split('\n')) translate = {'UUU' : 'F','CUU' : 'L','AUU' : 'I','GUU' : 'V','UUC' : 'F','CUC' : 'L','AUC' : 'I','GUC' : 'V','UUA' : 'L','CUA' : 'L','AUA' : 'I','GUA' : 'V','UUG' : 'L','CUG' : 'L','AUG' : 'M','GUG' : 'V','UCU' : 'S','CCU' : 'P','ACU' : 'T','GCU' : 'A','UCC' : 'S','CCC' : 'P','ACC' : 'T','GCC' : 'A','UCA' : 'S','CCA' : 'P','ACA' : 'T','GCA' : 'A','UCG' : 'S','CCG' : 'P','ACG' : 'T','GCG' : 'A','UAU' : 'Y','CAU' : 'H','AAU' : 'N','GAU' : 'D','UAC' : 'Y','CAC' : 'H','AAC' : 'N','GAC' : 'D','UAA' : 'Stop','CAA' : 'Q','AAA' : 'K','GAA' : 'E','UAG' : 'Stop','CAG' : 'Q','AAG' : 'K','GAG' : 'E','UGU' : 'C','CGU' : 'R','AGU' : 'S','GGU' : 'G','UGC' : 'C','CGC' : 'R','AGC' : 'S', 'GGC' : 'G','UGA' : 'Stop','CGA' : 'R','AGA' : 'R','GGA' : 'G','UGG' : 'W','CGG' : 'R','AGG' : 'R','GGG' : 'G', } aminos = translate.values() sample = [l for l in protein] + ['Stop'] score = [] for s in sample: c = aminos.count(s) score.append(c) import math result = reduce(lambda x, y: x*y, score) % 1000000 print result </code></pre> <p>This computes the total number of RNA sequences and takes the modulo of the final result (or so I think). I got the wrong answer twice before I decided to try this:</p> <pre><code>import math result = reduce(lambda x, y: x*y % 1000000, score) print result </code></pre> <p>This apparently produced the correct answer. Why does a modulo have to be performed at every x*y? Am I not understanding modulo or am I not understanding Python?</p> <p>EDIT: Sorry, typo.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload