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

12 篇文章 0 订阅
8 篇文章 0 订阅

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值