Example #1
0
def indc(f):
    '''Main driver to solve this problem. We use exact arithmetic for binomials
    instead of the betainc function to avoid round-off.'''
    n = 2 * ro.read_int(f)
    # return ro.join_list('%.5f' % (np.log10(1 - ro.cumbin(n, 0.5, k - 1)),) for k in xrange(1, n + 1))
    return ro.join_list('%.3f' % (x,) for x in np.log10(map(float, np.cumsum(it.islice(ro.binom(), n, n + 1).next())))[-2::-1] - n * np.log10(2))
Example #2
0
def sign(f):
    n = ro.read_int(f)
    print math.factorial(n) * 2 ** n
    for x in it.imap(lambda (x, y): tuple(x[i] * y[i] for i in xrange(len(x))), it.product(it.permutations(xrange(1, n + 1)), it.product(*((-1, 1) for _ in xrange(n))))): print ' '.join(map(str, x))
Example #3
0
'''
============================================================
http://rosalind.info/problems/inod/

A binary tree is a tree in which each node has degree equal to at most 3. The binary tree will be our main tool in the construction of phylogenies.

A rooted tree is a tree in which one node (the root) is set aside to serve as the pinnacle of the tree. A standard graph theory exercise is to verify that for any two nodes of a tree, exactly one path connects the nodes. In a rooted tree, every node v will therefore have a single parent, or the unique node w such that the path from v to the root contains {v,w}. Any other node x adjacent to v is called a child of v because v must be the parent of x; note that a node may have multiple children. In other words, a rooted tree possesses an ordered hierarchy from the root down to its leaves, and as a result, we may often view a rooted tree with undirected edges as a directed graph in which each edge is oriented from parent to child. We should already be familiar with this idea; it's how the Rosalind problem tree works!

Even though a binary tree can include nodes having degree 2, an unrooted binary tree is defined more specifically: all internal nodes have degree 3. In turn, a rooted binary tree is such that only the root has degree 2 (all other internal nodes have degree 3).

Given: A positive integer n (3<=n<=10000).

Return: The number of internal nodes of any unrooted binary tree having n leaves.
============================================================
'''
import rosalind.rosutil as ro

inod = lambda f: ro.read_int(f) - 2

if __name__ == "__main__":
    print inod('rosalind_inod_sample.dat')
    print inod('rosalind_inod.dat')
    
Example #4
0
def fibo(file_name):
    '''Main call.'''
    n = read_int(file_name) 
    print '%d' % (it.islice(fibonacci(), n - 1, n).next(),)
Example #5
0
'''
============================================================
As in the case of unrooted trees, say that we have a fixed collection of n taxa labeling the leaves of a rooted binary tree T. You may like to verify that (by extension of "Counting Phylogenetic Ancestors") such a tree will contain n-1 internal nodes and 2n-2 total edges. Any edge will still encode a split of taxa; however, the two splits corresponding to the edges incident to the root of T will be equal. We still consider two trees to be equivalent if they have the same splits (which requires that they must also share the same duplicated split to be equal).

Let B(n) represent the total number of distinct rooted binary trees on n labeled taxa.

Given: A positive integer n (n<=1000).

Return: The value of B(n) modulo 1,000,000.
============================================================
'''
import rosalind.rosutil as ro, rosalind.rosalind_cunr as rc

# Same counting argument as problem CUNR, plus the option to add a new root and the nth leaf
# under it. Hence 2*(n-1)-2 edges + 1 = 2*n-3, so the total number is (2*n-3)!!.
num_rooted_trees = lambda n, r = 1000000: rc.num_unrooted_trees(n + 1, r)

if __name__ == "__main__":
    print num_rooted_trees(ro.read_int('rosalind_root_sample.dat'))
    print num_rooted_trees(ro.read_int('rosalind_root.dat'))
Example #6
0
"""
============================================================
http://rosalind.info/problems/cunr

Given: A positive integer n (n<=1000).
Return: The total number of subsets of {1,2,...,n} modulo 1,000,000.
============================================================
"""
import rosalind.rosutil as ro

num_unrooted_trees = lambda n, r=1000000: ro.prod_mod(((2 * i - 1) for i in xrange(1, n - 1)), r)

if __name__ == "__main__":
    print num_unrooted_trees(ro.read_int("rosalind_cunr_sample.dat"))
    print num_unrooted_trees(ro.read_int("rosalind_cunr.dat"))
Example #7
0
'''
============================================================
http://rosalind.info/problems/sset

Given: A positive integer n (n<=1000).
Return: The total number of subsets of {1,2,...,n} modulo 1,000,000.
============================================================
'''
import rosalind.rosutil as ro
sset = lambda n, r = 1000000: pow(2, n, r)

if __name__ == "__main__":
    print sset(ro.read_int('rosalind_sset_sample.dat'))
    print sset(ro.read_int('rosalind_sset.dat'))