iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 14
0
自我挑戰組

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

Day14 - Codewars 刷題

喉嚨比昨天更脆弱了 QQ
請多多包涵 ~"~


題目(Spacify)

Modify the spacify function so that it returns the given string with spaces insertedbetween each character.

spacify("hello world") # returns "h e l l o   w o r l d"
def spacify(str)

end

Test.assert_equals(spacify("hello world"),"h e l l o   w o r l d");
Test.assert_equals(spacify("12345"),"1 2 3 4 5");

題目(Create Phone Number)

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example:
createPhoneNumber(Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns "(123) 456-7890"
The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parenthesis!
def createPhoneNumber(numbers)
  #TODO
end

Test.assert_equals(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), "(123) 456-7890");

影片解題:
Yes


答案:

# Spacify
def spacify(str)
  str.chars.join' '
end

# Create Phone Number
def createPhoneNumber(numbers)
  phone = numbers.join
  "(#{phone[0..2]}) #{phone[3..5]}-#{phone[6..-1]}"
end

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


上一篇
Day13 - Codewars 刷題
下一篇
Day15 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言