必须输入字母A和B并通过翻转使其全为A的代码不适用于所有选项

时间:2019-06-11 06:23:11

标签: python

我编写了一个从用户输入A或B的行的代码,他们还输入了鳍状板可以翻转的行中有多少个煎饼,在这里输出了要使A或B的行全部都需要进行的翻转A。

如果字母行不能均匀翻转,则代码应输出“此操作无法完成”。

示例:如果用户输入的鳍状肢尺寸为BBBB 2,程序将输出两次翻转。

翻转1是字母1-2,是AABB,翻转2是字母3-4,所以现在是AAAA,需要两个翻转。

我已经为此编写了代码,并且在大多数情况下都可以使用,但是一个问题是,当我输入BBAABB时,它说不能用4的鳍状肢来完成,而实际上可以用第一个鳍状肢来完成从字母1-4完成,所以现在是AABBBB,第二次翻转是在字母3-6中完成的,所以现在是AAAAAA,成功了,翻转了2次,我不确定如何用我的代码解决这个问题。 / p>

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()

非常感谢您的帮助,因为我是编程的新手。

詹姆斯

2 个答案:

答案 0 :(得分:1)

您的代码的一个问题是,您必须像BBAABB一样将A转换为B的逻辑也可以将2转换为B,因为您只需将B转换为A即可,因此它将是AAAAAA,但是当您输入4,您将必须将A转换为B,而当前程序中还没有包含A。

答案 1 :(得分:1)

关键是要找到一个必须翻转的“ B”,然后根据第一个“ B”的翻转大小查看是否有足够的煎饼。如果可以,您可以进行翻转并增加翻转次数。您只是不断重复此操作,直到您无法以'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