假日來塊蛋糕
複習下二進制及 Ruby 內建好用方法
Codewars LV7
Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.
The binary number returned should be a string.
def add_binary(a,b)
#your code here
end
Test.assert_equals(add_binary(1,1),"10")
Test.assert_equals(add_binary(0,1),"1")
Test.assert_equals(add_binary(1,0),"1")
Test.assert_equals(add_binary(2,2),"100")
Test.assert_equals(add_binary(51,12),"111111")
答案:
# Binary Addition
def add_binary(a,b)
(a + b).to_s(2)
end
本文同步發布於 小菜的 Blog https://riverye.com/