需要进一步澄清Codeeval的SEQUENCE TRANSFORMATION挑战

时间:2014-03-19 15:02:38

标签: python algorithm

CHALLENGE DESCRIPTION:

  

有两个序列。第一个序列由数字组成" 0"和   " 1",第二个由字母组成" A"和" B"。挑战是   确定是否可以转换给定的二进制文件   使用以下规则将序列序列化为字符串序列:   1." 0"可以转换成非空的字母序列" A" (" A"," AA"," AAA"等)   2." 1"可以转换成非空的字母序列" A" (" A"," AA"," AAA"等)或非空字母序列" B" (" B"," BB",   " BBB"等)。

     

对于每个测试用例打印出来"是"如果转变是可能的,   否则打印"否"。 E.g。

INPUT SAMPLE:

 1010 AAAAABBBBAAAA 
 00 AAAAAA
 01001110 AAAABAAABBBBBBAAAAAAA
 1100110 BBAABABBA

输出样本:

Yes
Yes
Yes
No

我对问题的理解

  

据我所知,问题陈述了两条规则,

     

规则1: A ^ n&lt ;-( 0)   规则2:(A / B)^ n&lt ;-( 1)

     

如果我解决给定的例子,使用这个逻辑我会得到是和否   分别满足这些例子。

     

示例1:

     
      
  1. 1010 AAAAABBBBAAAA - 是
  2.   
  3. 无&lt ;-(1)AAAAA&lt ;-( 0)BBBB&lt ;-(1)AAAA&lt ;-(0)
  4.   
  5. 这是肯定的,因为这不会破坏任何规则。
  6.         

    示例2:

         
        
    1. 1100110 BBAABABBA - NO
    2.   
    3. 无&lt ;-(1)BB&lt ;-(1)AA&lt ;-(0)B&lt ;-(0)A&lt ;-(1)BB&lt ;-(1)A&lt ;-(0)
    4.   
    5. 这是否,因为B&lt ;-( 0)不符合规则1
    6.   

但是当我在我的代码中实现这个逻辑时,结果不被接受。我的逻辑/对问题的理解有什么问题?

我的代码

import sys
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
  digits, word = test.split()
  seq = None
  result = None
  count  = 0
  word = word[::-1]
  digits = digits[::-1]
  for character in word:
    #print character, seq, count, digits, digits[count]
    if not seq:
      seq = character
      continue
    if seq != character:
        if seq == 'B' and digits[count] == '0':
          result = "No"
          break
        seq = character
        count+=1
  if result:
    print result
  else:
    print "Yes"

test_cases.close()

实时测试案例和结果通过我的代码

0000 AAA
Yes
111111000111001110001110101100100 AABBBBBBBBBBBAAAAAAAAAAAAAAABBBBBBBAAAAAAAAABBBAAABBBBBAAAAAAAAAAAAABBBBAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAA
No
1101110110011101000010101110110101000100011110001101010001 BBBBAABAABBAAAAAABABBBBBBAAAAAAAABAABAABBBABBAAABBBBBBBABBBBAAAAABBBABBBAABBA
No
0100011111110101101000100010011101010111110111101011000110111111100101011011101010111100101000010011110 AABAAAAAABAABBBBABAABAAAAABBBAABAAAAABABAABABBBBBAABBBAAABABBABAAABBABBABAABAABABBBBBAAAABAABABAAAABBBAAABA
No
000001010111001000110010000111101111000100000110011000010011000010110010101111110110111101111100101 BBABBBAAABABBAAABBABBBBAABBBBAAAAABAABBAABABBABABABBAAAABBAAAAAAAAAAAABBBAABAAABABAABAAAAABBABBBABBBA
No
1 B
Yes
00010000100010000111011110110001111001110110101000100010111110010 BBBABABABBABBAAABBBBABBBABBABABABBBAAABBAAABBBBABABBAABBBAAAABBBB
No
10 BBBBBAAAAAAA
Yes
10100000010110110100001011000011100000100010100001110 BBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAABBBAAABBBBBBAAABBAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAABBAAAAAAAAAAAAAAAAAABBBAAAAAAA
No
1111111000100100000110011110 AAAAAAABBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAABBBBBBBABAAA
No
000000101 AABBBABABAAAABABBBB
No
1000110111100110100000110111010100101011111100110001111000101100001 BBAAAAAAAAAAABBBBBBAAAAAAAAAABBBAAAAAABAAAAAAAAAAAAAAAAABBAAAAAAAAAAAAAAAAAAAAABBAAAAAABBBAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBAAAAAAAAAAAAABBBAAAAAAAAAAABBBBBBBBBBBBAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAB
No
00111011000111010101100001 AAAAAAABBBAAAAAAAAABBBBBBAAAAAAAAAAAAAAABBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB
No
010000101110000010110011001000110010000110111110101100100000011101011100100110001000000111100010010100011011100 ABBABAAABAABBAAAABBBAABAABABABBAAABBBAABBBABBAAAABAABABBBABABBAABABABBAABBABBBABBBBABBBABBBAABABBABAAABAAAAAABBBABA
No
000011 AAAAAAAAAAAAAAAAAAABBBBB
Yes
00110 AAAABBAA
Yes
1110011111001001110011001011100000110110001101000000100110101010000001101100010100001111011110110000100111000001100101001101100110011 BAABBBABABABBABAABBBBBBAABABAAAAAABBABBBBAAAAABABABABABAABABBAAAABBAABBAAABBBAAABABBBBBABBBABBAAABBABAAABBBBAAAABAAAAABBAABABABBBBBBABBBBBABABABABABBBAAAAAABABBBAAABBBBAABABABAABBBBBABAABBAABABBBAAABABBBBBBABA
No
01001111101000000001001110001100100011100011000010100011110111101111011010110101100001011001111101011100100011101010100011111 AAABAAAAAAABBBAAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAABBBBBBAAAAAAAAAAABBBAAAAAAAAAAAAAAAAAAAAABAAAAABBBBBAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAABBBBBBBBBAAAAAABBBAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAABBBBBBBBBBAAAAAAAAABBBBBBBAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAABBBBBAAAABBBBBBAAAAAABBBAAAAAAABBAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAABBAABBBAAAAABBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBA
No
11001000011111000111001001110011000001100101 BBBBBBAAAAAAAAABAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAABBBBAABBBBBBAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAB
No
1011100100010011110000111111111100101001010000011000011111001010111001011101010100000000 ABABAABABABABBAABABBBABABBBBABBAAABBBBABAAABABABBBABAAABAABBBAABAAAABBAAABBBBAAABAABABAABAAABBBBBABBBAA
No
10101100101000110 AABBBAAAABBAAAABABBBAABAABABAAAAAAABAABABAABBBBAAAAA
No
11010101101010010011000011100110011110101000001111010000000101010011001011111001000010101010110000000110100101100100110101000 AAAAAABBBBAAAAABBAAABBBAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAABBBBBBBAAAAAAAAAAABBBBBAAAAAAAAAAAAAABBBBBBAAAAAAABBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABBBBBBAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBAAAAAABBBBAAAAAAAAAAAAAABBAAAABBBAAAABBBAAAAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAAAAAAA
No
01 BBBBBAAAAAAA
Yes
1 BBBBBBAAAAAA
Yes
010111001100101011001101010010011111000101011111111000100000010010011101010011010011101101000000000000010111111010100110110101000101 AAAAAAAAAAAAAAABBBAAAAAAAAAABBBBBBBBBBAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAABBBBAAAAABBAAAAAAABBBBBBAAAAABBBAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAAAAAAAABBBBBAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAABBBBAAAAAAABBBBBBAAAAAAABBAAABBBBAAAAAAAAAAAAABBBBAAAAAABBBBAAAAAABBBBAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAAABBAAAAAABBBBAAAAAABAAAAAABBBBAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAABBAABBBBBAAAAAAAAABBBBBAAAAAA
No
0111011101011001110011000011110100100 ABBABBABABBABABABAABBBAAABAABBAAAAABAAABAAABABBBABABBBAAABAB
No
110101010100100111101010111011100011010001010110011111000011001011101110100011001111100001111111 BAAABABAAABBBBAAABAABBBABBBAABBBBAAAABABAAABAAAAABBAAAAABBBBBAAABABAAABBABABAABABAABAABBBBAAABBABBABB
No
11101111000011101110101001100110111011110101011100000010001100011110100111111100110010111011100001101011101110110100100 ABAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAABAAAAAAAAAAAAAAABBBBBBAAAAAAAAAABBBBAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAABBAABBBBAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBAAAAAAAAAAABBBAAAAAAAAAAAAABBBBBAAAAAABBBBBBAABBBBBBBBBBBBAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAABBBAAAABBBAAAAAABBBAAAAABBAAAAAABBBBBBAAAAAAAAAAAABBBAA
No
0111100001110101011101001110001101101010011100110100001011011010011101001000110000111110110100001010011001101110001 BAAABBAABBBBABBBBABABABBABABABAAAAAAAAAAAAAABBBAAABBABAAAABBAAABBBAABAAABABAAAABBABBABABBABAABBAABBBABBAAAAAAAABABBBBBAB
No
001010111001000001000101111100001100111011010100000010010010100101011011010110010110000 ABBAABBAAAAAAABABBAABBBABBAAABBABABABBAABABABAABAABAAABBAAABAABBAAAABBABBABBBAAAAAABABBBBAAABAABABAAABAABAAAAABBBBBABBBABBAABAABBBABBBBABAAABABBAAABBABA
No
001001001010100001101011111000001000100100000000011001001111001010001101 AAAAABAAAAAAAAAAAAAAAAABAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBAAAABAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB
No
00010010101110000100101 BBBAAAAABABABBAAAAABBBA
No
001011010111001010100011101011100000000000110110110101110001110110110010010100 AAAAAAAABBBBBBAAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBAAAAAAABBBBBBABBBBBBAAAAABAAAABBAAAAABBBBBBAAAAAAAAAAAAAAAABBBBAAAAABBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
No
00010101011100100000 AAAAAAABABBBBAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAA
No
0001001000000011 BAAABBBBBABABAAAABBBBBBBBBBAAAAAAABBBAABAABABABBB
No
00100010101001011111101011010110010111001100010000001001011101101001110000001100110010101101111000111101010001101 BABBBBABAAABAAAABABBABBBAAAAABBAAABBBBAABABBBAAAAAABABBABABBBBBAABBBBBBAAAABABABABAAABAAAABABAABAABAABBABBBAAABABAAAAAAABAAABAAAAABAAABBBBBBBBAABABABBBAABAAAAAAAABABBAABBBABBBABBABBBB
No
01010011101101010010000000110101010000100011010000011101100111010101101100111001100 AABBAAABABABBBBBBBBBBAABBBBABAAABABAAABABAABABAAAAABABBABABABBBAABBAABBBBAABBBAAAAAABBAABBABAABBBBBAAAAABBBAAAABABBBAA
No
0 A
Yes
101001010101 BBBBAABAABAABBAAAABAABABBAABABBBBABABBAABABABAABBBBBBBAABAABBAABAABBAAAABABABAABABABABBABAAABBBABA
No
000011111010110010100000011101001110110101001111110101001110110101100000100001100011011010011000001010101111101010010100011010111011011001 AAAAAAAAAAAAAAAAAAABAAABBBBBAAAAAAAABBAAABBBAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBAAAAAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAAAAAABAAAAAAAAAAAABBBBABBBBBBAAAAAAAAAAAAAABBBBBAAAAAAAAAAAAABAABAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAAAAAAAAAAABBBAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBAAAAABAAAAAAAAAABBBAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAABBBAAAAAAAABBBBBBAAAAAABBBBAAABAAAAAAAAAABBBBAAAABAAAAAAAAABBBBBB
No
1110010000010010001100101010011010101 BBBABBBAAAABAAAABBBAAAAABAAAAABABAAAA
No
00100101101111100001000110110111010100110111100001101101011101100100001100111 ABBAABABABABAABBAABBBBBAABABBABBAABBBBAAAABAAABBABBBABAABBBBBBABAAAABBAABAAAABBAA
No
001110010101100010111010111000001010100101011110000011110101011110100011011100010111011110 AAAAAAAAAABAAAAAAAAAABAABBBAAAAABBBAAAAAAAAAAAAAAAAAAAABBBBBAAABABBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAABBBBBBBAAABAABBAAAABBBBBAAAABBBBBABBBBBBBAABBBAAAAAAAAAAABBAAAAAAAAABBBBBAAAAAAAAAAAAAAAABBBBBBAAAAAAAAAAAAAABBBBAAAAAAAAAAAAAAAAAAAAABBBBBBAAAA
No
0000 AAAA
Yes
1100100011011000110101110110011100001101110000101111101110111011100010101010110100000110100110110010000101001101110110110101111010101100110 BBBAABBBABAABBABBAABBBAAAAAAAABBBABABBABABBBBAABBBBBAABAABBABAAABBABBABABBBABBABBBABAABBAABABAAAABABBABBBABAABAAABBABABAAAAABBBAAABABBABBAABBBAABABABBAAABAABAABBAAAAABBAABBAAABBABBBAABAABAB
No
000001000101110010110000100111100111001110110101110100001000001011111001001010010001111110000100011011010000110011010011010110 AABBAABBABBAAABAAABABAAAABBABAAABBAAABBAABBABAABABAABBBBBAABBAAABBAABAAABABABABAABAABAABABAAAABBBAAABBBBBBAAABAAAABABBAABBABBA
No
1011000100010001010010111100100011100101000001010010010011001100100011000001111000011000001100011001101000110010010010101011100010001110010 ABABBABAAABAABBAABBBBBABABBBBBBAABAAAAAAABBBAABBABAAAAAAAAABABAABABBAAAAAAABABBAAAABBBBBBBBBABBABBBABAABAABAABBBABBAABBBBAAAABAABBBBBBBBAAB
No
110011011010101110010010100110010010110011111000100011011101110000110111 AABABABBABBAABBABBABAAAABBBAABBBBBBBABABBBBBABABABBBAAABABBABABBABAABBBBABABB
No
01001 ABBBAB
No
00010110110 BABABBAABAABABBBABABABBABBABB
No 

1 个答案:

答案 0 :(得分:2)

1匹配一个或更多元素,AB

您的理解在这里是不正确的,因为您似乎认为匹配0次也是有效的:

  
      
  1. 1010 AAAAABBBBAAAA - 是
  2.   
  3. 无&lt ;-(1)AAAAA&lt ;-( 0)BBBB&lt ;-(1)AAAA&lt ;-(0)
  4.   

此处1匹配至少一个 A字符,然后0与剩余的A匹配字符,然后下一个1匹配B字符序列等:

  • A&lt ;-(1)AAAA&lt ;-(0)BBBB&lt ;-(1)AAAA&lt ;-(0)

结果,你的第一个样本已经错了:

0000 AAA

这必须与至少 4个字符匹配,3 A个字符与此不匹配,因为每个0必须至少匹配一个A。因此,答案必须是"否"。