iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 19
0
自我挑戰組

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

Day19 - Codewars 刷題

  • 分享至 

  • xImage
  •  

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

Codewars LV7


題目(Century From Year)

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')

影片解題:
Yes


答案:

# 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/


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

尚未有邦友留言

立即登入留言