你必須很努力

Day19 - Codewars 刷題

2019/09/28
字數統計: 209閱讀時間: 1 min

即便是假日也閒不下來
不停地學習及做專案
再忙也要擠出一點時間鐵人賽一下

Codewars LV7


題目(Century From Year)

1
2
3
4
5
6
7
8
9
10
11
12
13
Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.

Your task is to write a function maskify, which changes all but the last four characters into '#'.

Examples
maskify('4556364607935616') # should return '############5616'
maskify('64607935616') # should return '#######5616'
maskify('1') # should return '1'
maskify('') # should return ''

# "What was the name of your first pet?"
maskify('Skippy') # should return '##ippy'
maskify('Nananananananananananananananana Batman!') # should return '####################################man!'

1
2
3
4
5
6
7
def maskify(cc)
# your beautiful code goes here
end

Test.assert_equals(maskify('4556364607935616'), '############5616')
Test.assert_equals(maskify('1'), '1')
Test.assert_equals(maskify('11111'), '#1111')


影片解題:


答案:

1
2
3
4
5
6
7
8
9
# Credit Card Mask
def maskify(cc)
# 方法1
return cc if cc.length <= 4
'#' * (cc.length - 4) + cc[-4..-1]

# 方法2
cc.gsub(/.(?=....)/, '#')
end

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

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

發表日期:2019-09-28

更新日期:2022-12-21

CATALOG