Python 必须输入字母A和B的代码,然后将其全部设为A';通过翻动它们来改变它不';我不适合所有的选择

Python 必须输入字母A和B的代码,然后将其全部设为A';通过翻动它们来改变它不';我不适合所有的选择,python,Python,我编写了一个代码,从用户那里输入一行a或B,他们还输入翻页器可以翻动的一行中的多少个煎饼,并输出使a或B的一行全部为a所需的翻动次数 如果字母行不能均匀翻转,代码应输出“这无法完成” 示例:如果用户输入的BBBB的翻页大小为2,则程序将输出翻页2次 翻页1来自字母1-2,它是AABB,翻页2来自字母3-4,所以现在需要两个翻页的地方是AAAA 我已经为此编写了代码,在大多数情况下它都能工作,但有一个问题是,当我输入BBAABB时,它说它不能用4号翻页纸来完成,而实际上可以用字母1-4来完成第一个

我编写了一个代码,从用户那里输入一行a或B,他们还输入翻页器可以翻动的一行中的多少个煎饼,并输出使a或B的一行全部为a所需的翻动次数

如果字母行不能均匀翻转,代码应输出“这无法完成”

示例:如果用户输入的BBBB的翻页大小为2,则程序将输出翻页2次

翻页1来自字母1-2,它是AABB,翻页2来自字母3-4,所以现在需要两个翻页的地方是AAAA

我已经为此编写了代码,在大多数情况下它都能工作,但有一个问题是,当我输入BBAABB时,它说它不能用4号翻页纸来完成,而实际上可以用字母1-4来完成第一个翻页,所以现在是aabbb,第二个翻页是用字母3-6来完成的,所以现在是aaaa,它成功了并进行了2次翻转,我不确定如何用我的代码修复此问题

while True:
    pancakes = input('Enter the row of the pancakes (A/B): ') 
    flipper = int(input('How many pancakes can be flipped at one time? ')) 

    flips, possible = 0, True 
    for row in pancakes.split('A'):
        count, remainder = divmod(len(row), flipper) 
        if remainder != 0:
            possible = False 
            break 
        flips += count 

    if possible: 
      if flips == 1:
        print('It took 1 flip.') #print out how many flips it took
        play = input('Would you like to run this program again? ') 
      else:
        print('It took', flips, 'flips.')
        play = input('Would you like to run this program again? ') 
    else: #if possible is false
      print("IMPOSSIBLE.") #printing that the burgers can't be flipped with the flipper the size inputted in 'flipper'
      play = input("Would you like to run this program again? ") 
    if play not in ['Yes', 'yes', 'Y', 'y']: 
      exit()
非常感谢您的帮助,因为我对编程相当陌生


James

你的代码的一个问题是,你必须得到翻转逻辑来将A翻转到B,就像BBAABB一样,在BBAABB中可以用2来完成,因为你只需要将B转换为A,所以它将是AAAAA,但是当你输入4时,你必须将A翻转到B,而B目前没有包含在你的程序中。

你的代码有一个问题代码是,你必须获得翻转逻辑,也可以像BBAABB一样将A翻转到B,在BBAABB中可以使用2,因为你只需要将B转换为A,所以它将是AAAAA,但当你输入4时,你必须将A翻转到B,而B目前不包括在你的程序中。

关键是要寻找一个必须翻转的“B”,然后看看在第一个“B”之后是否有足够多的煎饼可以按照翻页大小翻动。如果可以,可以进行翻转并增加翻转次数。你只需不断重复这一点,直到你不能从a“B”开始做一个完整的翻转,在这一点上你不能得到所有的a。但如果只要你能找到一个“B”,你就可以翻转“flipper”煎饼,你最终就能得到所有的“a”:

def flip(str, pos, count):
    r = str[:pos]
    for i in range(pos, pos + count):
        r += 'A' if str[i] == 'B' else 'B'
    r += str[pos + count:]
    return r

while True:
    pancakes = input('Enter the row of the pancakes (A/B): ')
    flipper = int(input('How many pancakes can be flipped at one time? '))

    flips, possible = 0, True
    while True:
        try:
            i = pancakes.index('B')
        except ValueError:
            # no 'B's, so we did it!
            break
        if (i > len(pancakes) - flipper):
            # not enough pancakes left to flip starting with a 'B', so we can't do it.
            possible = False
            break
        else:
            # Can do a flip, starting with a 'B', so do it
            pancakes = flip(pancakes, i, flipper)
            flips += 1

    if possible:
        if flips == 1:
            print('It took 1 flip.')  # print out how many flips it took
        else:
            print('It took', flips, 'flips.')
    else:  # if possible is false
        print("IMPOSSIBLE.")  # printing that the burgers can't be flipped with the flipper the size inputted in 'flipper'
    play = input("Would you like to run this program again? ")
    if play not in ['Yes', 'yes', 'Y', 'y']:
        exit()
结果:

Enter the row of the pancakes (A/B): BBAABB
How many pancakes can be flipped at one time? 4
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBAABBAAA
How many pancakes can be flipped at one time? 4
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): AAAA
How many pancakes can be flipped at one time? 2
It took 0 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): BBBB
How many pancakes can be flipped at one time? 2
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): BBBB
How many pancakes can be flipped at one time? 4
It took 1 flip.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBB
How many pancakes can be flipped at one time? 2
IMPOSSIBLE.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBB
How many pancakes can be flipped at one time? 4
IMPOSSIBLE.
Would you like to run this program again? n

Process finished with exit code 0

关键是要寻找一个必须翻转的“B”,然后看看在第一个“B”之后是否有足够的煎饼按照翻转器的大小翻转。如果可以,可以进行翻转并增加翻转次数。你只需不断重复这一点,直到你不能从a“B”开始做一个完整的翻转,在这一点上你不能得到所有的a。但如果只要你能找到一个“B”,你就可以翻转“flipper”煎饼,你最终就能得到所有的“a”:

def flip(str, pos, count):
    r = str[:pos]
    for i in range(pos, pos + count):
        r += 'A' if str[i] == 'B' else 'B'
    r += str[pos + count:]
    return r

while True:
    pancakes = input('Enter the row of the pancakes (A/B): ')
    flipper = int(input('How many pancakes can be flipped at one time? '))

    flips, possible = 0, True
    while True:
        try:
            i = pancakes.index('B')
        except ValueError:
            # no 'B's, so we did it!
            break
        if (i > len(pancakes) - flipper):
            # not enough pancakes left to flip starting with a 'B', so we can't do it.
            possible = False
            break
        else:
            # Can do a flip, starting with a 'B', so do it
            pancakes = flip(pancakes, i, flipper)
            flips += 1

    if possible:
        if flips == 1:
            print('It took 1 flip.')  # print out how many flips it took
        else:
            print('It took', flips, 'flips.')
    else:  # if possible is false
        print("IMPOSSIBLE.")  # printing that the burgers can't be flipped with the flipper the size inputted in 'flipper'
    play = input("Would you like to run this program again? ")
    if play not in ['Yes', 'yes', 'Y', 'y']:
        exit()
结果:

Enter the row of the pancakes (A/B): BBAABB
How many pancakes can be flipped at one time? 4
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBAABBAAA
How many pancakes can be flipped at one time? 4
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): AAAA
How many pancakes can be flipped at one time? 2
It took 0 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): BBBB
How many pancakes can be flipped at one time? 2
It took 2 flips.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): BBBB
How many pancakes can be flipped at one time? 4
It took 1 flip.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBB
How many pancakes can be flipped at one time? 2
IMPOSSIBLE.
Would you like to run this program again? y
Enter the row of the pancakes (A/B): ABBB
How many pancakes can be flipped at one time? 4
IMPOSSIBLE.
Would you like to run this program again? n

Process finished with exit code 0

如果余数!=0:看起来不正确。抱歉,本来不应该在那里的!这是一个错误。如果它是4,那么JamesIt就不起作用了,因为它实际上是带BBAABB的can,第一个翻转是AAABB,第二个翻转是AAAAA。我已经检查过了,它可以使用2而不是4。谢谢。我的错,锯2而不是4,因为你的不可能条件太基本,所以它不起作用,它只考虑把B变成A,谢谢你的建议。我是编程新手,我真的不知道如何用我的代码将A翻转到B,如果我能做到这一点,我认为这将解决问题。詹姆士如果余数!=0:看起来不正确。抱歉,本来不应该在那里的!这是一个错误。如果它是4,那么JamesIt就不起作用了,因为它实际上是带BBAABB的can,第一个翻转是AAABB,第二个翻转是AAAAA。我已经检查过了,它可以使用2而不是4。谢谢。我的错,锯2而不是4,因为你的不可能条件太基本,所以它不起作用,它只考虑把B变成A,谢谢你的建议。我是编程新手,我真的不知道如何用我的代码将A翻转到B,如果我能做到这一点,我认为这将解决问题。詹姆斯