iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 17
1
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 17 篇

Day17 - Codewars 刷題

  • 分享至 

  • xImage
  •  

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


題目(Complementary DNA):

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 )

影片解題:
Yes


答案:

# 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/


上一篇
Day16 - Codewars 刷題
下一篇
Day18 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天 共 31 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言