Skip to content

Commit

Permalink
Comute Catalan Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
weka511 committed Oct 7, 2017
1 parent 069e236 commit 4c7782d
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 302 deletions.
34 changes: 34 additions & 0 deletions cat.py
@@ -0,0 +1,34 @@
# Copyright (C) 2017 Greenweaves Software Pty Ltd
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>
#
# cat Catalan Numbers and RNA Secondary Structures

from rosalind import RosalindException,verify_counts_complete_graph

def CatalanNumbers(N):
cn=[1]
def helper(n):
while len(cn)<n+1:
cn.append(sum([cn[k-1]*cn[n-k] for k in range(1,n+1)]))
for n in range(0,N+1):
helper(n)
return cn

def CountNonCrossingMatches(string):
counts=verify_counts_complete_graph(string)

if __name__=='__main__':
#print (CountNonCrossingMatches('AUAU'))
print (CatalanNumbers(12))
21 changes: 5 additions & 16 deletions pmch.py
Expand Up @@ -15,22 +15,11 @@
#
# pmch Perfect Matchings and RNA Secondary Structures

from rosalind import RosalindException
from rosalind import RosalindException,factorial,verify_counts_complete_graph
def NumberPerfectMatchings(string):
def factorial(n):
return n*factorial(n-1) if n>1 else 1
counts={
'A':0,
'U':0,
'C':0,
'G':0
}
for c in string:
counts[c]+=1
if counts['A']==counts['U'] and counts['C']==counts['G']:
return factorial(counts['A'])*factorial(counts['G'])
else:
raise RosalindException('Mismatched counts {0}/{1} or {2}/{3}'.format(counts['A'],counts['U'],counts['C'],counts['G']))


counts=verify_counts_complete_graph(string)
return factorial(counts['A'])*factorial(counts['G'])

if __name__=='__main__':
print (NumberPerfectMatchings('CGCUUGCAGGACAAGGUGCAUAUCUUCACCAUCAGCUAAACGAUAGCCGCUCACGCGGGAUGGUGCGUUCCGUG'))
19 changes: 18 additions & 1 deletion rosalind.py
Expand Up @@ -277,4 +277,21 @@ class RosalindException(Exception):
'''
Provide a common class for all exceptions thrown by the library
'''
pass
pass

def factorial(n):
return n*factorial(n-1) if n>1 else 1

def verify_counts_complete_graph(string):
counts={
'A':0,
'U':0,
'C':0,
'G':0
}
for c in string:
counts[c]+=1
if counts['A']==counts['U'] and counts['C']==counts['G']:
return counts
else:
raise RosalindException('Mismatched counts {0}/{1} or {2}/{3}'.format(counts['A'],counts['U'],counts['C'],counts['G']))

0 comments on commit 4c7782d

Please sign in to comment.