我在coderbyte上做一道题,题目如下:
Using the JavaScript language, have the function
SimpleSymbols(str)
take thestr
parameter being passed and determine if it is an acceptable sequence by either returning the stringtrue
orfalse
. Thestr
parameter will be composed of+
and=
symbols with several letters between them (ie.++d+===+c++==a
) and for the string to betrue
each letter must be surrounded by a+
symbol. So the string to the left would befalse
. The string will not be empty and will have at least one letter.
我写的代码是这样的:
var SimpleSymbols = function(str){
var alpha = /[A-Za-z]/;
var symbolarr = str.split("");
for(var i = 0; i < symbolarr.length; i++) {
if(alpha.test(symbolarr[i])) {
if(symbolarr[i-1] != "+" & symbolarr[i+1] != "+") {
return false;
}
}
}
return true;
}
问题是,当我测试案例时 SimpleSymbols("+a=")
我得到了 true
我已经通读了我的代码几次并且不能'不要调试这个。谁能发现错误?
最佳答案
调整您的解决方案
将&
换成&&
,它们的意思完全不同。
次要评论:您不需要将字符串拆分为数组来访问其字符;您可以使用 str[i]
直接访问它们。
忽略说你需要范围检查的评论者:arr[-1]
将只返回 undefined
,它不等于 +
如您所料。
正则表达式解决方案
测试是否存在给出无效序列的正则表达式。如果存在,则该字符串是非法的。
function SimpleSymbols(str) {
return !/([^+]|^)[A-Za-z]([^+]|$)/).test(str);
}
英语:
Test for the presence of the following regexp:
Find any character other than +, or the beginning of the string
followed by a letter
followed by any character other than +, or the end of the string
If it is present, return false, else true
状态机方法
考虑编写此类程序的一种有用方法是将其视为状态机。我们将使用三种状态:start
表示开始,have_plus
表示刚看到加号,need_plus
表示需要加号在一封信之后。
function SimpleSymbols(str) {
var state = "start";
var alpha = /[A-Za-z]/;
for (var i = 0; i < str.length; i++) {
var type = alpha.test(str[i]) ? 'letter' : str[i] === '+' ? 'plus' : 'other';
switch (state) {
case "start":
switch (type) {
case 'letter': return false;
case 'plus' state = "have_plus"; break;
case 'other': break;
}
break;
case "need_plus":
switch (type) {
case 'letter': return false;
case 'plus': state = "have_plus"; break;
case 'other': return false;
}
break;
case "have_plus":
switch (type) {
case 'letter': state = "need_plus"; break;
case 'plus': break;
case 'other': state = "start"; break;
}
break;
}
}
if (state === "need_plus") return false;
return true;
}
它有点长,但它可能更具可读性和可维护性。但我们可以做得更好。我们将把状态逻辑封装在一个数据结构中,如下所示:
var transitions = {
start: {letter: "fail", plus: "have_plus", other: "start"},
need_plus: {letter: "fail", plus: "have_plus", other: "fail"},
have_plus: {letter: "need_plus", plus: "have_plus", other: "start"},
fail: {letter: "fail", plus: "fail", other: "fail"}
};
现在我们的程序只是通过状态机运行:
function SimpleSymbols(str) {
var state = "start",
alpha = /[A-Za-z]/;
for (var i = 0; i < str.length; i++) {
var type = alpha.test(str[i]) ? 'letter' : str[i] === '+' ? 'plus' : 'other';
state = transitions[state][type];
}
return state != "fail";
}
我们可以将其捆绑到一个状态机对象中:
function stateMachine(state, transitions) {
return {
go: function(type) { state = transitions[state][type]; }
state: function() { return state; }
};
}
现在我们可以将“SimpleSymbols”写成
function SimpleSymbols(str) {
var machine = stateMachine("start", transitions),
alpha = /[A-Za-z]/;
for (var i = 0; i < str.length; i++) {
var type = alpha.test(str[i]) ? 'letter' : str[i] === '+' ? 'plus' : 'other';
machine.go(type);
}
return machine.state() != "fail";
}
最好将 token 的分类与运行机器的逻辑分开:
function token_type(c) {
return /A-Za-z]/.test(str[i]) ? 'letter' : str[i] === '+' ? 'plus' : 'other';
}
然后就
function SimpleSymbols(str) {
var machine = stateMachine("start", transitions);
for (var i = 0; i < str.length; i++) { machine.go(token_type(str[i])); }
return machine.state() != "fail";
}
关于javascript - 在 Javascript 中检查字符串中的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331548/