Mathematica 中的字符串验证

发布于 2024-10-20 01:28:14 字数 587 浏览 1 评论 0原文

我有一个想要优化的字符串验证函数。该字符串的长度为 2n,由 01 组成,例如 str="100001"。我想测试:

1)字符串中奇数索引位置的 1 的数量(必须不少于 1)是否等于偶数索引位置的数量

2)是否为每个StringTake[str,2*i],i1运行到n-1,字符串中奇数索引位置的 1 不等于偶数索引位置的 1

总之,我想测试位置 2n 是否第一次字符串中奇数索引位置中的 1 的数量是等于均匀索引位置的值。

"100001"101101 是一个很好的字符串,但不是 100100100000 也不是 000000代码>.

非常感谢。

I have a string validation function that I want to optimize. The string is of length 2n and composed of 0 and 1's, for example, str="100001". I want to test:

1) whether the number (has to be not less than 1) of 1's in oddly indexded positions in the string is equal to that in the evenly indexed positions

2) whether for every StringTake[str,2*i], i runs from 1 to n-1, the number of 1's in oddly indexded positions in the string is not equal to that in the evenly indexed positions.

In sum, I want to test whether the position 2n is the first time the number of 1's in oddly indexded positions in the string is equal to that in the evenly indexed positions.

"100001" and 101101 is a good string, but not 100100, 100000 nor 000000.

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-10-27 01:28:14

此代码不会测试无效字符串(字符不是“0”或“1”,长度也不是)。

goodString[str_String] := Module[
  {digits, cumdiffs, pos},
  digits = Transpose[Partition[
    IntegerDigits[ToExpression[str], 10, StringLength[str]], 2]];
  cumdiffs = Subtract @@ Accumulate /@ digits;
  pos = Position[cumdiffs, 0, 1, 1];
  Length[pos] == 1 && pos[[1, 1]] == Length[cumdiffs]
]

您的示例:

goodString /@ {"100001" , "101101", "100100", "100000", "000000"}

Out[302]= {True, True, False, False, False}

可能有更快的方法,例如使用 NestList。此外,如果速度是一个大问题并且字符串可能很长,您可以在预处理中拆分 IntegerDigits[ToExpression[...]] 并在其余部分使用 Compile 。

丹尼尔·利希布劳
沃尔夫勒姆研究公司

This code does not test for invalid strings (characters not "0" or "1", length not even).

goodString[str_String] := Module[
  {digits, cumdiffs, pos},
  digits = Transpose[Partition[
    IntegerDigits[ToExpression[str], 10, StringLength[str]], 2]];
  cumdiffs = Subtract @@ Accumulate /@ digits;
  pos = Position[cumdiffs, 0, 1, 1];
  Length[pos] == 1 && pos[[1, 1]] == Length[cumdiffs]
]

Your examples:

goodString /@ {"100001" , "101101", "100100", "100000", "000000"}

Out[302]= {True, True, False, False, False}

There might be faster ways e.g. with NestList. Also if speed is a big issue and strings are likely to be long, you could split out the IntegerDigits[ToExpression[...]] in preprocessing and use Compile on the rest.

Daniel Lichtblau
Wolfram Research

江心雾 2024-10-27 01:28:14

(对学生代码表示歉意,这是我第一次在 Mathematica 中使用字符串,所以我留下了我的想法以及一些注释掉的调试/跟踪值)

charcountsvec[s_String, c_String] := Table[
   If[StringTake[s, {i, i}] == c, 1, 0]
   , {i, 1, StringLength[s]}
];

oddchars[s_String] := StringTake[s, {1, -1, 2}]; (*pick out odd chars*)    
evenchars[s_String] := StringTake[s, {2, -1, 2}];

validatestr[str_String] :=     
 Block[{evencounts, oddcounts, answer1, answer2 (*, odds, evens*)},
  evencounts = Accumulate@charcountsvec[(*evens=*)evenchars[str], "1"];
  oddcounts = Accumulate@charcountsvec[(*odds=*)oddchars[str], "1"];
  (*my interpretation of "number of 1's in odd positions the same as in even positions"*)      
  answer1 = Last[oddcounts] == Last[evencounts]; 
  (*my interpretation of "for every..string...whether number of 1's in even/odd positions is not equal"*)      
  answer2 = Fold[And, True, 
    MapThread[Unequal, {Most[oddcounts], Most[evencounts]}]];

  {str, And[answer1, answer2](*,odds,evens,oddcounts,evencounts,answer1,answer2*)}
  ];

测试:

validatestr/@{"100001","101101","100100","100000","000000"}

{{“100001”,True }, {"101101", 真}, {"100100", 假}, {"100000",
False}, {"000000", False}}

validatestr["0000001"](*odd length string pukes, and returns False*)

在评估 (Local) In[428]:= MapThread::mptc 期间:MapThread 位置 {2, 1} 和 {2, 2} 处的对象尺寸不兼容[Unequal, {{0,0,0},{0,0}}];尺寸为 {3} 和 {2}。 >>>

(本地)输出[432]= {“0000001”,假}

(Apologies for the schoolboy-code, this is my first effort with Strings in Mathematica, so I left in my thinking-as-I-went and also some commented-out debug/tracing values)

charcountsvec[s_String, c_String] := Table[
   If[StringTake[s, {i, i}] == c, 1, 0]
   , {i, 1, StringLength[s]}
];

oddchars[s_String] := StringTake[s, {1, -1, 2}]; (*pick out odd chars*)    
evenchars[s_String] := StringTake[s, {2, -1, 2}];

validatestr[str_String] :=     
 Block[{evencounts, oddcounts, answer1, answer2 (*, odds, evens*)},
  evencounts = Accumulate@charcountsvec[(*evens=*)evenchars[str], "1"];
  oddcounts = Accumulate@charcountsvec[(*odds=*)oddchars[str], "1"];
  (*my interpretation of "number of 1's in odd positions the same as in even positions"*)      
  answer1 = Last[oddcounts] == Last[evencounts]; 
  (*my interpretation of "for every..string...whether number of 1's in even/odd positions is not equal"*)      
  answer2 = Fold[And, True, 
    MapThread[Unequal, {Most[oddcounts], Most[evencounts]}]];

  {str, And[answer1, answer2](*,odds,evens,oddcounts,evencounts,answer1,answer2*)}
  ];

Testing:

validatestr/@{"100001","101101","100100","100000","000000"}

{{"100001", True}, {"101101", True}, {"100100", False}, {"100000",
False}, {"000000", False}}

validatestr["0000001"](*odd length string pukes, and returns False*)

During evaluation of (Local) In[428]:= MapThread::mptc: Incompatible dimensions of objects at positions {2, 1} and {2, 2} of MapThread[Unequal,{{0,0,0},{0,0}}]; dimensions are {3} and {2}. >>

(Local) Out[432]= {"0000001", False}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文