秋意越來越濃,要爬起來也不容易呢 XD
刷個 Codewars 題目醒腦下 (更想睡
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).
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):
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.
def digital_root(n)
# ...
end
Test.assert_equals( digital_root(16), 7 )
Test.assert_equals( digital_root(456), 6 )
答案:
# 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/