需要迭代反向插入才能处理所有可能的参数

问题描述 投票:0回答:1

我有一个Python代码,它接受用户输入的字符串和用户输入的位置,并迭代地将该字符串插入到自身中。然后,它在所有迭代完成后向用户显示字符串。之后,它会执行相反的操作。它在迭代过程完成后获取字符串,并应该找到原始的用户输入的字符串。我的问题是,当用户输入的字符串位置之一不为 0 时,反向结果是错误的。如果用户输入的字符串中的字符之一重复,该代码还会给出不正确的反转。我需要代码能够接受任何输入并正常工作。



def iterative_insertion(S, positions):
    R = S
    for pos in positions:
        R = R[:pos] + S + R[pos:]
    return R

def find_longest_repeating_substring(S):
    longest_substring = ""
    for i in range(1, len(S) // 2 + 1):
        substring = S[:i]
        pos = S.find(substring, i)
        while pos != -1:
            if S[pos:pos+i] == substring:
                longest_substring = substring
                break
            pos = S.find(substring, pos + 1)
    return longest_substring

def reverse_iteration(R):
    S = R

    while True:
        repeating_substring = find_longest_repeating_substring(S)
        if repeating_substring:
            S = S.replace(repeating_substring, '', 1)
        else:
            break

    return S

def process_input():
    input_string = entry_string.get()
    positions_input = entry_positions.get()
    positions = [int(pos) for pos in positions_input.split(',')]

    result = input_string  
    for pos in positions:
        result = iterative_insertion(result, [pos])

    result_text.delete(1.0, tk.END)
    result_text.insert(tk.END, f"Output: {result}\nLength of Resulting String: {len(result)}")

def reverse_process_input():
    final_string = entry_final.get()

    try:
        reversed_result = reverse_iteration(final_string)
        result_label_reverse.config(text=f"Reversed Original String: {reversed_result}")
    except Exception as e:
        result_label_reverse.config(text=f"Error: {str(e)}")

def clear_reverse_input():
    entry_final.delete(0, tk.END)

这是我需要的输出:

输入:'abcd'

插入位置:0,1,1,13

插入输出:aaabcdabcdbcdaaabcdabcdbcdabcdabcdabcdbcdabcdabcdabcdabcdbcdabcd

反向输入:aaabcdabcdbcdaaabcdabcdbcdabcdabcdabcdbcdabcdabcdabcdabcdbcdabcd

反向输出:abcd

当输入字符串中有重复字符,或者0不是插入位置之一时,这不起作用

输入:“哈哈”

插入位置:1,2,3,4

插入输出:llllllllllllololollololollllololollolololol

反向输入:lllllllllllololollololollllololollolololol

反向输出:ol

正确的反向输出是:lol。

此外,当插入 0 不是插入点之一的字符串时,结果是错误的。

输入:abcd

插入位置:1,2,3,4

插入输出:aaaaaaaaaaabcdbcdbcdbcdabcdbcdbcdbcdaaabcdbcdbcdbcdabcdbcdbcdbcd

反向输入:aaaaaaaaaaabcdbcdbcdbcdabcdbcdbcdbcdaaabcdbcdbcdbcdabcdbcdbcdbcd

反向输出:abcdbcdbcdbcd

正确的反向输出是abcd。

python insert iteration reverse insertion-sort
1个回答
0
投票

如果有帮助,我认为这些方法可以执行您想要执行的正向插入和反向操作。也许他们可以帮助您继续使用当前的代码。

def process_forward(initial, indexes):
    final = initial
    for index in indexes:
        final = final[:index] + final + final[index:]
    return final

def process_backwards(initial, indexes):
    final = initial
    for index in reversed(indexes):
        chars_to_remove = len(final) // 2
        final = final[:index] + final[index+chars_to_remove:]
    return final

你可以尝试一下:

### ---------------------
initial = "abcd"
indexes = [0,1,1,13]
expected = "aaabcdabcdbcdaaabcdabcdbcdabcdabcdabcdbcdabcdabcdabcdabcdbcdabcd"

print(initial, indexes)

forward_result = process_forward(initial, indexes)
print(expected == forward_result, forward_result)

backward_result = process_backwards(forward_result, indexes)
print(initial == backward_result, backward_result)
### ---------------------

这应该给你:

abcd [0, 1, 1, 13]
True aaabcdabcdbcdaaabcdabcdbcdabcdabcdabcdbcdabcdabcdabcdabcdbcdabcd
True abcd

### ---------------------
initial = "lol"
indexes = [1,2,3,4]
expected = "lllllllllllolololollolololollllolololollolololol"

print(initial, indexes)

forward_result = process_forward(initial, indexes)
print(expected == forward_result, forward_result)

backward_result = process_backwards(forward_result, indexes)
print(initial == backward_result, backward_result)
### ---------------------

给予:

lol [1, 2, 3, 4]
True lllllllllllolololollolololollllolololollolololol
True lol
© www.soinside.com 2019 - 2024. All rights reserved.