你必須很努力

Day17 - Codewars 刷題

2019/09/26
字數統計: 242閱讀時間: 1 min

秋意越來越濃,要爬起來也不容易呢 XD
刷個 Codewars 題目醒腦下 (更想睡


題目(Complementary DNA)

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

1
2
3
4
5
6
7
def DNA_strand(dna)
#your code here
end

Test.assert_equals(DNA_strand("AAAA"),"TTTT","String AAAA is")
Test.assert_equals(DNA_strand("ATTGC"),"TAACG","String ATTGC is")
Test.assert_equals(DNA_strand("GTAT"),"CATA","String GTAT is")

題目(Sum of Digits / Digital Root)

1
2
3
In this kata, you must create a digital root function.

A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.

1
2
3
4
5
6
def digital_root(n)
# ...
end

Test.assert_equals( digital_root(16), 7 )
Test.assert_equals( digital_root(456), 6 )


影片解題: Yes


答案:

1
2
3
4
5
6
7
8
9
10
# Complementary DNA
def DNA_strand(dna)
dna.tr('ATCG', 'TAGC')
end


# Sum of Digits / Digital Root
def digital_root(n)
n < 10 ? n : digital_root( n / 10 + n % 10)
end

本文同步發布於 小菜的 Blog https://riverye.com/

原文連結:https://riverye.com/2019/09/26/Day17-Codewars-刷題/

發表日期:2019-09-26

更新日期:2019-10-09

CATALOG