起因

同学的朋友读计算机语言,然后有道题目。

具体题目要求:

需要用到的预设函数:

# bmrs
def bmrs(word, i=1):
    if not word.startswith('_'):
        word = '_' + word
    if -1 < i < len(word):
        return interpret(word, i) + bmrs(word, i+1)
    return ''

# predecessor
def p(word, i):
    if i <= 1:
        return 0
    else:
        return i-1

# successor
def s(word, i):
    if i >= len(word) - 1:
        return 0
    else:
        return i+1

# interpreter function
def interpret(word, i):
    if H_o(word, i):
        return 'H'
    else:
        if L_o(word, i):
            return 'L'
        else:
            return '0'

# input predicate for high tones
def H_i(word, i):
    return word[i] == 'H'

# input predicate for low tones
def L_i(word, i):
    return word[i] == 'L'

# input predicate for out of bounds symbol
def out_of_bounds_i(word, i):
    return word[i] == "_"

思路和想法

肯定是要通过def H_o和L_o还有interpreter进行输出移位。

题目意思就是给进来一个由'H'、'L'、'0' 组成的字符串,左边的 'H' 尽量向右移到倒数第二或者到 'L' 的左边;如果最后一位是 'H' ,'H' 就向左移一位,如果最后一位是 'H' ,倒数第二位是'L' 就把 'H' 变成 '0'。

我自己的思路就是:构造一个新的函数,先遍历找出'H'和'L'的位置,通过enum或者index再对'H'进行移位或者替换。在可能存在多个 'L' 的情况下,'H' 将尽可能向右移动到倒数第二或者到它向右接触的第一个 'L' 的左边。如果最后一位是 'H',则 'H' 向左移一位;如果最后一位是 'H' 且倒数第二位是 'L',则将 'H' 变成 '0'。

接着就是对代码编写

# Function to swap characters in a word
def Swap_Char(word):
    # Convert the input to a string in case it's not
    word = str(word)
    # Create a list of characters from the word
    word_list = list(word)
    # Initialize position for 'H' as None and list to keep track of 'L' positions
    h_pos = None
    l_positions = []

    # Find the positions of 'H' and all 'L's
    for i, char in enumerate(word_list):
        if char == 'H':
            h_pos = i
        elif char == 'L':
            l_positions.append(i)

    # Check if 'H' exists
    if h_pos is not None:
        # If 'H' is at the end and the second to last is 'L', change 'H' to '0'
        if h_pos == len(word_list) - 1:
            if len(word_list) > 1 and word_list[-2] == 'L':
                word_list[-1] = '0'
            else:
                # Move 'H' one position to the left
                word_list[-1], word_list[-2] = word_list[-2], word_list[-1]
        else:
            # If 'H' is not at the end, find the first 'L' to the right of 'H'
            next_l_pos = next((pos for pos in l_positions if pos > h_pos), None)
            # If there is an 'L' to the right of 'H'
            if next_l_pos:
                # Move 'H' to the left of this 'L'
                target_pos = next_l_pos - 1
            else:
                # Move 'H' to the second last position if it's not already there and not at the end
                target_pos = len(word_list) - 2
            # Swap 'H' with the target position if it's to the right
            if target_pos > h_pos:
                word_list[h_pos], word_list[target_pos] = word_list[target_pos], word_list[h_pos]

    return ''.join(word_list)


# Output predicate for high tones
def H_o(word,i):
    # Swap characters in the word first
    new_word = Swap_Char(word)
    # Check if the character at index i is 'H'
    if len(new_word) > i and new_word[i] == 'H':
        return True
    else:
        return False
# Output predicate for low tones    
def L_o(word,i):
    new_word = Swap_Char(word)
    if len(new_word) > i and new_word[i] == 'L':
        return True
    else:
        return False
    
# bmrs
def bmrs(word, i=1):
    if not word.startswith('_'):
        word = '_' + word
    if -1 < i < len(word):
        return interpret(word, i) + bmrs(word, i+1)
    return ''

# predecessor
def p(word, i):
    if i <= 1:
        return 0
    else:
        return i-1

# successor
def s(word, i):
    if i >= len(word) - 1:
        return 0
    else:
        return i+1

# interpreter function
def interpret(word, i):
    if H_o(word, i):
        return 'H'
    else:
        if L_o(word, i):
            return 'L'
        else:
            return '0'

# input predicate for high tones
def H_i(word, i):
    return word[i] == 'H'

# input predicate for low tones
def L_i(word, i):
    return word[i] == 'L'

# input predicate for out of bounds symbol
def out_of_bounds_i(word, i):
    return word[i] == "_"

# Example usage
input_string = "_H000L000" #the output should be "000HL000"
print(bmrs(input_string))

   


人贵有自知之明。