提问人:SJ Brigante 提问时间:11/1/2023 更新时间:11/2/2023 访问量:45
从插入代码中获取正确的输出匹配
Getting correct output match from insertion code
问:
我无法让iter_position_reverse中的测试用例字符串通过。我不确定我必须更改什么才能解决这个问题
这是iterrative_insertion代码:
def iterrative_insertion(S, positions):
R = S
offset = 0
for pos in positions:
R = R[:pos + offset] + S + R[pos + offset:]
offset + len(S)
return R
# Example usage
S = "abcdabd"
positions = [0, 1, 16]
result = iterrative_insertion(S, positions)
expected = 'aabcdabdbcdabdababcdabdcdabd'
print(expected)
print(result)
print(result == expected)
print(len(result),len(S), len(expected))
# abcdabd
# abcdabdabcdabd
# aabcdabdbcdabdabcdabd
# aabcdabdbcdabdababcdabdcdabd
这是我拥有iter_position_reverse代码:
import iterrative_insertion
def iter_position_reverse(R):
# Initialize variables to store S and positions
S = ""
positions = []
# Start iterating through R
i = 0
while i < len(R):
# Find the longest substring that matches the beginning of R
j = 0
while j < len(S) and i + j < len(R) and S[j] == R[i + j]:
j += 1
# If we found a match, add its position to the positions list
if j == len(S):
positions.append(i)
# If we didn't find a match, add the next character to S
S += R[i]
# Move to the next character in R
i += 1
return S, positions
# Test cases
test_strings = [
"abcdabdaabcdabdbcdabdabcdabd",
"aabcdabdbcdabdabcdabdabcdabd",
"aababababcdabdcdabdcabcdabddabdcdabdbcdabd",
"aaaaaaababababcdabdcdabdcabcdabddabdcdabdbcdabdababababcdabdcdabdcabcdabddabdcdabdbcdabdababababcdabdcdabdcabcdabddabdcdabdbcdabdbabababcdabdcdabdcabcdabddabdcdabdbcdabdababababcdabdcdabdcabcdabddabdcdabdbcdabd",
"ddodoremefasolasiremefasolasioremdoredoremefasolasimefasolasiefasolasi",
"abdaabcdabdbcdabdb"
]
for R in test_strings:
S, positions = iter_position_reverse(R)
print("Input String:", R)
print("S:", S)
print("Positions:", positions)
print()
if iterrative_insertion.iterrative_insertion(S,positions) == R:
print("We did it.")
else:
print(f"It did not work for {R}")
代码输出没有给出答案,无论是“我们做到了”还是“它对 {R} 不起作用”。
该代码尝试查找和重新构造与输入字符串开头匹配的输入字符串的子字符串,并存储这些匹配项的位置。然后,它检查重建的字符串在由 iterrative_insertion 函数处理时是否可以生成原始输入字符串。主要目标是测试和验证在 iter_position_reverse 中实现的字符串匹配和重建算法。
答:
0赞
JonSG
11/2/2023
#1
这里的诀窍是在原始字符串的长度范围内使用指针,指针将比较起始字符的扩展列表和该扩展列表之后的字符。如果这些列表匹配,那么我们将保存一对候选字符串。有很多机会可以优化它,例如,一旦你到达一半,继续寻找就没有意义了,但我会把它留给你:
请注意,我更改了您的测试数据,以便更轻松地查看测试用例,但您可以随意重用您的实际测试数据。
def iter_position_reverse(original_string):
for pointer in range(len(original_string)):
if original_string[:pointer] == original_string[pointer:2*pointer]:
best_pointer = pointer
return best_pointer
然后,您可以使用以下命令进行测试:
## -------------------
# Test cases
## -------------------
test_strings = [
"abcd", ## no matching initial sub-string
"aabc", ## "a" and "abc" reconstructs "aabc"
"abab", ## "ab" and "ab" reconstructs "abab"
"abab_abab_ab", ## "abab_" and "abab_ab" reconstructs "abab_abab_ab"
]
for test_string in test_strings:
split_pointer = iter_position_reverse(test_string)
starting_string = test_string[:split_pointer]
remaining_string = test_string[split_pointer:]
matches = test_string == starting_string + remaining_string
if matches:
print(f"We did it: \"{starting_string}\" + \"{remaining_string}\" --> \"{test_string}\"")
else:
print(f"It did not work for \"{test_string}\"")
## -------------------
这将为您提供:
We did it: "" + "abcd" --> "abcd"
We did it: "a" + "abc" --> "aabc"
We did it: "ab" + "ab" --> "abab"
We did it: "abab_" + "abab_ab" --> "abab_abab_ab"
请注意,我构建此答案是为了专门解决:需要帮助让迭代位置反向测试用例通过,该测试用例已作为此问题的副本关闭,因此我假设它本质上是相同的任务。
上一个:MPI 扫描已反转
评论
offset + len(S)
什么都不做。它计算加法,但丢弃结果。也许你的意思是?offset += len(S)
=
+=