简体   繁体   English

Codewars Challenge - The Deaf Rats of Hamelin - JavaScript - 如何按特定字符集拆分字符串?

[英]Codewars Challenge - The Deaf Rats of Hamelin - JavaScript - How to Split a String by a Certain Set of Characters?

Link to challenge 挑战链接

Essentially, the idea is that if a rat - O~ - is facing the Piper - P - then it's going the correct way.本质上,这个想法是,如果一只老鼠 - O~ - 面对 Piper - P - 那么它就会走正确的路。

Here is a rat going left: O~这是一只向左走的老鼠: O~

Here is a rat going right: ~O这是一只向右走的老鼠: ~O

We want to count how many 'deaf rats' exist in a string - how many are facing the wrong way.我们想计算一个字符串中有多少“聋老鼠”——有多少人正面​​临着错误的方向。

Here, there is 1 deaf rat: PO~ O~ ~OO~这里有1只聋鼠: PO~ O~ ~OO~


My logic is to first check for if the Piper is on the left or right side of the string. 我的逻辑是首先检查 Piper 是在字符串的左侧还是右侧。

If he's not, then we need to split the string based on where the Piper is located in the string, and then count how many rats on the left side are not facing him, and how many rats on the right side are not facing him.如果他不是,那么我们需要根据 Piper 在字符串中的位置拆分字符串,然后计算左侧有多少老鼠不面向他,以及右侧有多少老鼠不面向他。

 var countDeafRats = function(town) { town = town.replace(/[ ]/g, ''); let deafCount = 0; //if piper's on the left if (town[0] === 'P') { for (let i = 0; i < town.length; i++) { if (town[i] === '~O') { deafCount ++; } } } //if piper's on the right if (town[town.length - 1] === 'P') { for (let j = 0; j < town.length; j++) { if (town[j] === 'O~') { deafCount ++; } } } let rats = town.split('P'); console.log('ratssss', rats); let leftRats = []; let rightRats = []; return deafCount; } console.log(countDeafRats("~O~OP ~O~O"));

In the case above, here is the array of rats: [ '~O~O', '~O~O' ] So there are two rats on the right side that are going the wrong way (deaf rats).在上面的例子中,这是老鼠的数组: [ '~O~O', '~O~O' ]所以右边有两只老鼠走错了路(聋鼠)。

What I'd like to do, is to push the left and right rats into their own arrays, and then count the deaf rats in each individual array.我想要做的是,将左右老鼠推入各自的阵列,然后计算每个单独阵列中的聋鼠。

But I'm not able to split the string ~O~O by either ~O or O~ .但是我无法将字符串~O~O拆分为~OO~

I would like to get ['~O', '~O'] for the leftRats and ['~O', '~O'] for the rightRats .我想获得['~O', '~O']leftRats['~O', '~O']rightRats

I had tried:我试过:

leftRatsssss [ [ '', '', '' ] ]
rightRatssss [ [ '~', 'O' ] ]

But it's doing something very different from what was expected:但它正在做与预期非常不同的事情:

 leftRatsssss [ [ '', '', '' ] ] rightRatssss [ [ '~', 'O' ] ]

Split is not splitting the string by the string given as a parameter.拆分不是通过作为参数给出的字符串拆分字符串。 So that's the real point of my question, then - how can you split a string by a certain set of characters?所以这就是我的问题的真正重点,那么 - 你怎么能用一组特定的字符分割一个字符串?

Ie If I wanted to split the string 'the' by 'he' , I'd want 'the' to become ['t', 'he'] - I've tried regex for this case?即,如果我想将字符串'the'拆分为'he' ,我希望'the'变成['t', 'he'] - 我已经在这种情况下尝试过正则表达式?

 console.log("the".split(/[he]/gi));

And it seems to be doing the opposite of what I want.它似乎与我想要的相反。 Any suggestions, then, for splitting a string by a given set of characters?那么,对于按给定的字符集拆分字符串有什么建议吗?

You can split the string by the P regardless, then turn each segment (on the left and right of the P ) into an array of each 2-character chunk.无论如何,您都可以通过P拆分字符串,然后将每个段(在P的左侧和右侧)转换为每个 2 字符块的数组。 Eg ~O~OP ~O~O turns into ['~O', '~O'] for the left chunk, and ['~O', '~O'] for the right chunk.例如~O~OP ~O~O变成['~O', '~O']代表左边的块,而['~O', '~O']代表右边的块。 Then count up the number of occurrences of O~ in the first chunk, and the number of occurrences of ~O in the second chunk:然后统计第一个chunk中O~出现的次数,以及第二个chunk中~O出现的次数:

 var countDeafRats = function(town) { const [leftRats, rightRats] = town .replace(/[^O~P]/g, '') .split('P') .map(segment => segment.match(/.{2}/g) || []); return ( leftRats.reduce((a, rat) => a + (rat === 'O~'), 0) + rightRats.reduce((a, rat) => a + (rat === '~O'), 0) ); } console.log(countDeafRats("~O~OP ~O~O"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM