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