你必須很努力

Day14 - Codewars 刷題

2019/09/23
字數統計: 219閱讀時間: 1 min

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


題目(Spacify)

1
2
3
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"

1
2
3
4
5
6
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)

1
2
3
4
5
6
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!

1
2
3
4
5
def createPhoneNumber(numbers)
#TODO
end

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


影片解題:


答案:

1
2
3
4
5
6
7
8
9
10
# 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/

原文連結:https://riverye.com/2019/09/23/Day14-Codewars-刷題/

發表日期:2019-09-23

更新日期:2022-12-21

CATALOG