rosalind練習(xí)題二十六

# Perfect Matchings and RNA Secondary Structures

# Problem

# Figure 5. A perfect matching on the basepair edges is highlighted in red and represents a candidate secondary structure for the RNA strand.A matching in a graph G is a collection of edges of G for which no node belongs to more than one edge in the collection. See Figure 2 for examples of matchings. If G contains an even number of nodes (say 2n), then a matching on G is perfect if it contains n edges, which is clearly the maximum possible. An example of a graph containing a perfect matching is shown in Figure 3.

# First, let Kn denote the complete graph on 2n labeled nodes, in which every node is connected to every other node with an edge, and let pn denote the total number of perfect matchings in Kn. For a given node x, there are 2n?1 ways to join x to the other nodes in the graph, after which point we must form a perfect matching on the remaining 2n?2 nodes. This reasoning provides us with the recurrence relation pn=(2n?1)?pn?1; using the fact that p1 is 1, this recurrence relation implies the closed equation pn=(2n?1)(2n?3)(2n?5)?(3)(1).

# Given an RNA string s=s1…sn, a bonding graph for s is formed as follows. First, assign each symbol of s to a node, and arrange these nodes in order around a circle, connecting them with edges called adjacency edges. Second, form all possible edges {A, U} and {C, G}, called basepair edges; we will represent basepair edges with dashed edges, as illustrated by the bonding graph in Figure 4.

# Note that a matching contained in the basepair edges will represent one possibility for base pairing interactions in s, as shown in Figure 5. For such a matching to exist, s must have the same number of occurrences of 'A' as 'U' and the same number of occurrences of 'C' as 'G'.

# Given: An RNA string s of length at most 80 bp having the same number of occurrences of 'A' as 'U' and the same number of occurrences of 'C' as 'G'.

# Return: The total possible number of perfect matchings of basepair edges in the bonding graph of s.

# Sample Dataset

# >Rosalind_23

# AGCUAGUCAU

# Sample Output

# 12

# 本題要求計(jì)算RNA序列對(duì)應(yīng)的堿基配對(duì)圖中袋哼,所有含有k個(gè)堿基對(duì)的完美匹配數(shù)量。其中k為RNA序列的堿基數(shù)的一半楞件。一個(gè)完美匹配表示所有堿基都和其它一個(gè)堿基配對(duì),而且每個(gè)堿基只能匹配一次。我們可以使用公式 pn=(2n-1)(2n-3)...(3)(1) 來(lái)計(jì)算完美匹配的數(shù)量粒督,其中 n = k * 2民泵。因此皆撩,本題的步驟包括:根據(jù)給定的RNA序列生成堿基配對(duì)圖,計(jì)算該圖中所有含有 k 個(gè)堿基對(duì)的完美匹配的數(shù)量续誉。輸出計(jì)算結(jié)果莱没。

from mathimport factorial

def count_perfect_matchings(s):

# 計(jì)算A、C酷鸦、G和U在s中的出現(xiàn)次數(shù)

? ? counts = {'A':0, 'C':0, 'G':0, 'U':0}

for nucleotidein s:

counts[nucleotide] +=1

? ? ? ? print(counts)

# 檢查A的數(shù)量等于U的數(shù)量饰躲,C的數(shù)量等于G的數(shù)量

? ? if counts['A'] != counts['U']or counts['C'] != counts['G']:

return 0

? ? # 計(jì)算完美匹配的總數(shù)

? ? n = counts['A']

return factorial(n) * factorial(n -1)

s ="AGCUAGUCAU"

print(count_perfect_matchings(s))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市臼隔,隨后出現(xiàn)的幾起案子属铁,更是在濱河造成了極大的恐慌,老刑警劉巖躬翁,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焦蘑,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡盒发,警方通過(guò)查閱死者的電腦和手機(jī)例嘱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)宁舰,“玉大人拼卵,你說(shuō)我怎么就攤上這事÷瑁” “怎么了腋腮?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)壤蚜。 經(jīng)常有香客問(wèn)我即寡,道長(zhǎng),這世上最難降的妖魔是什么袜刷? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任聪富,我火速辦了婚禮,結(jié)果婚禮上著蟹,老公的妹妹穿的比我還像新娘墩蔓。我一直安慰自己梢莽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布奸披。 她就那樣靜靜地躺著昏名,像睡著了一般。 火紅的嫁衣襯著肌膚如雪阵面。 梳的紋絲不亂的頭發(fā)上葡粒,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音膜钓,去河邊找鬼嗽交。 笑死,一個(gè)胖子當(dāng)著我的面吹牛颂斜,可吹牛的內(nèi)容都是我干的夫壁。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼沃疮,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼盒让!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起司蔬,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤邑茄,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后俊啼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體肺缕,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年授帕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了同木。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡跛十,死狀恐怖彤路,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芥映,我是刑警寧澤洲尊,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站奈偏,受9級(jí)特大地震影響坞嘀,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜霎苗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一姆吭、第九天 我趴在偏房一處隱蔽的房頂上張望榛做。 院中可真熱鬧唁盏,春花似錦内狸、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至刽严,卻和暖如春昂灵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舞萄。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工眨补, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人倒脓。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓撑螺,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親崎弃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子甘晤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容