iT邦幫忙

2025 iThome 鐵人賽

DAY 26
0
Software Development

clojure 30 days系列 第 26

clojure 30 days - day 26

  • 分享至 

  • xImage
  •  

Problem Description

Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.

If you want to know more: http://en.wikipedia.org/wiki/DNA

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". Your function receives one side of the DNA (string, except for Haskell); you need to return the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).

More similar exercise are found here: http://rosalind.info/problems/list-view/ (source)

Example: (input --> output)

"ATTGC" --> "TAACG"
"GTAT" --> "CATA"

Note

  • keywords
    • let: creates local binding for the complements hash-map
    • map: applies hash-map lookup to each character in the string
    • *apply str: converts sequence of characters back to string
    • hash-map as function: {\A \T} works as a lookup function

Implementation

; implement
(defn dna-strand [dna]
  (let [complements {\A \T \T \A \C \G \G \C}]
    (apply str (map complements dna))))

; test
(defn tester [arg exp]
  (= (dna-strand arg) exp))

; args & exception
(comment
  (tester "ATTGC" "TAACG"))


上一篇
clojure 30 days - day 25
下一篇
clojure 30 days - day 27
系列文
clojure 30 days30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言