gpt4 book ai didi

javascript - Coderbyte 挑战 : Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect

转载 作者:行者123 更新时间:2023-11-29 20:51:22 25 4
gpt4 key购买 nike

我正在解决 Coderbyte Challenge - Questions Marks当我在浏览器中运行我的代码时一切正常,但是,一旦我在 coderbyte 网站上运行它就会抛出错误。

挑战是:

Have the function QuestionsMarks(str) take the str string parameter,which will contain single digit numbers, letters, and question marks,and check if there are exactly 3 question marks between every pair oftwo numbers that add up to 10. If so, then your program should returnthe string true, otherwise it should return the string false. If therearen't any two numbers that add up to 10 in the string, then yourprogram should return false as well.

For example: if str is "arrb6???4xxbl5???eee5" then your programshould return true because there are exactly 3 question marks between6 and 4, and 3 question marks between 5 and 5 at the end of thestring.

Use the Parameter Testing feature in the box below to test your codewith different arguments.

测试用例是:

"arrb6???4xxbl5???eee5" true

"aa6?9" false

"acc?7??sss?3rr1??????5" true

我的解决方案是使用 RegExp 来解决这个问题。下面的代码在浏览器中运行时运行良好,但是 Coderbyte 控制台每次都会抛出错误:

/tmp/009904362/main.js:11 clean = clean.match(/d(???)d/gi); ^SyntaxError: Invalid regular expression: /d(???)d/

这是我的代码 -

function QuestionsMarks(str) { 

//create a "clean" array containing only the numbers and question marks from str
var result;
let clean = str.match(/[0-9?]/g);
// join() the array back in to the string
clean = clean.join("");

// use match() to return an array of pairs that match the pattern d???d
clean = clean.match(/d(\?\?\?)d/gi);

//create a function sumCheck() that converts first and last char of every array string to Number and checks if the sum of digits is 10
//using forEach() run the sumcheck() on all strings in the array
clean.forEach(sumCheck);

function sumCheck(string){
if((Number(string.charAt(0)) + Number(string.charAt(string.length - 1))) == 10){
result = true;
}else{
result = false;
}
}
return result;
}
QuestionsMarks("acc?7??sss?3rr1??????5");

最佳答案

问题似乎来自 Coderbyte,它无法正确解析正则表达式模式(文字或使用 RegExp 构造函数)中的转义字符。所以最简单的解决方案是替换转义序列:\d => [0-9], and \? => [ ?](正如@Saud 在评论中所建议的那样)。


关于您的方法:

... check if there are exactly 3 question marks between every pair of two numbers that add up to 10 ...

您更正后的模式 /[0-9][?]{3}[0-9]/g 是什么?
它查找由三个问号分隔的数字(和然后你检查两位数字的总和是否为 10)。即使此模式能够找到字符串中由三个问号分隔的所有数字对(情况并非如此(*)),它也不会检查是否有数字加起来为 10,并且没有恰好被 3 个问号分隔!

因此,目标是查找字符串中是否包含一对加起来等于 10 的数字,没有 3 个问号。如果此对存在,则函数返回 false


(*):为什么 /[0-9][?]{3}[0-9]/g 无法找到由 3 个问号分隔的所有数字对?

示例:1???2???3???4
因为你不能匹配同一个字符两次。该模式将找到:1???23???4 但不会找到 2???3,因为 2 已被第一个匹配项消耗。


一种可行的方法:

function QuestionsMarks(str) {
var state = { d1: 0, d2: 0, marks: 0,
init: function() { this.d1 = this.d2; this.marks = 0; },
check: function() { return this.d1 + this.d2 > 9 && this.marks != 3; }
};
var re = /[0-9?]/g;
var m;

while ( (m = re.exec(str)) !== null ) {
if ( m[0] == '?' ) {
state.marks++;
} else {
state.d2 = parseInt(m[0]);
if ( state.check() ) return false;
state.init();
}
}
return true;
}

关于javascript - Coderbyte 挑战 : Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com