在codewars里升级吧~7:一道4级题代码及讲解视频

这一期讲一道4级题。这道题跟C++没什么太大的关系,不过正则表达式在处理字符串的时候是非常有用的工具,多学一项东西没什么坏处。顺便预告下一期的codewars视频讲解是网站上仅有的5道C++1级题中的一道,解开的时候会十分有趣。

腾讯讲解视频链接

https://v.qq.com/x/page/p0755xkvmun.html

b站讲解视频链接

https://www.bilibili.com/video/av34431298

题 目

 

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分钟。先截屏示意一下,具体解题思路看视频讲解吧。

在codewars里升级吧~7:一道4级题代码及讲解视频_第1张图片

在codewars里升级吧~7:一道4级题代码及讲解视频_第2张图片

 

你可能感兴趣的:(codewars,c++,video)