在codewars裡升級吧~7:一道4級題程式碼及講解視訊
這一期講一道4級題。這道題跟C++沒什麼太大的關係,不過正則表示式在處理字串的時候是非常有用的工具,多學一項東西沒什麼壞處。順便預告下一期的codewars視訊講解是網站上僅有的5道C++1級題中的一道,解開的時候會十分有趣。
騰訊講解視訊連結
b站講解視訊連結
題 目
BINARAL MULTIPLE OF 3
In this kata, your task is to creat a regular exprassion capable of evaluating binary strings (strings with only 1s and 0s) and determing wether the given string represents a number divisible by 3.
Take into account that:
-
an empty string might be evaluated to true (it's not going to be tested, so you don't need to worry about it -unless you want.)
-
The input should consist only of binary digits - no spaces, other digits, alphanumeric characters, etc.
-
There might be leading 0 s.
Examples (Javascript)
-
multipleof3Rege.test('000') should be true
-
multipleof3Rege.test('001') should be false
-
multipleof3Rege.test('011') should be true
-
multipleof3Rege.test('110') should be true
-
multipleof3Rege.test(' abc ') should be false
You can check more in the example test cases.
Note
There's a way to develop an automate (FSM) that evaluates if strings representing numbers in a given base are divisible by a given number. You might want to check an example of an automate for doing this same paticular test here.
If you want to understand better the inner principles behind it, you might want to study how to get the modulo of an arbitrarily large number taking one digit at a time.
SOLUTION
解這道4級題只用了一行程式碼,但講了近30分鐘。先截圖示意一下,具體解題思路看視訊講解吧。