即便是假日也閒不下來
不停地學習及做專案
再忙也要擠出一點時間鐵人賽一下
Codewars LV7
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!'
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')
答案:
# 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/