Python-简化“剪刀石头布”的逻辑条件

时间:2018-10-02 00:51:54

标签: python optimization

我正在解决一个问题,它说:

  

在《大爆炸理论》中,谢尔顿和拉吉创建了一个新游戏:“石头剪刀布-蜥蜴-斯波克”。

     

游戏规则为:

     
      
  • 剪刀剪纸;
  •   
  • 纸覆盖着岩石;
  •   
  • 岩石压碎了蜥蜴;
  •   
  • 蜥蜴毒害了Spock;
  •   
  • Spock砸了剪刀;
  •   
  • 剪刀使蜥蜴断头;
  •   
  • 蜥蜴吃纸;
  •   
  • 论文反驳了Spock;
  •   
  • Spock使岩石蒸发;
  •   
  • 岩石压碎剪刀。
  •   
     

在谢尔顿获胜的情况下,他会说:“ Bazinga!如果拉杰获胜,谢尔顿会宣布:“拉杰被骗”;在平局中,他会要求一个新游戏:“ Again!”。给定双方选择的选项,制作一个程序,打印出谢尔顿对结果的反应。

     

输入包含一系列测试用例。第一行包含一个正整数T(T≤100),它表示测试用例的数量。每个测试用例由输入的一行表示,其中分别包含Sheldon和Raj的选择,并用空格隔开。

我对此问题的代码是

T = int(input())

for i in range(T):
    Sheldon, Raj = input().split(' ')

    if(Sheldon == "scissors" and (Raj == "paper" or Raj == "lizard")):
        Win = True
    elif(Sheldon == "lizard" and (Raj == "paper" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "Spock" and (Raj == "rock" or Raj == "scissors")):
        Win = True
    elif(Sheldon == "paper" and (Raj == "rock" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "rock" and (Raj == "scissors" or Raj == "lizard")):
        Win = True
    elif(Raj == "scissors" and (Sheldon == "paper" or Sheldon == "lizard")):
        Lose = True
    elif(Raj == "lizard" and (Sheldon == "paper" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "Spock" and (Sheldon == "rock" or Sheldon == "scissors")):
        Lose = True
    elif(Raj == "paper" and (Sheldon == "rock" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "rock" and (Sheldon == "scissors" or Sheldon == "lizard")):
        Lose = True
    elif(Sheldon == Raj):
        Tie = True

    if(Win == True):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Lose == True):
        print("Case #{0}: Raj cheated!".format(i+1))
    elif(Tie == True):
        print("Case #{0}: Again!".format(i+1))

    Win = Lose = Tie = False

但是我认为时间太长了。有什么办法可以减少它?

3 个答案:

答案 0 :(得分:7)

首先,祝贺您尝试编写此内容!初次尝试时,您的逻辑就很好。

下一步是制作一个数据结构,您可以用相同的方式查询规则。一个合适的选择是dictionary

options = {
 'scissors': ('paper', 'lizard'),
 'paper': ('rock', 'spock'),
 'rock': ('lizard', 'scissors'),
 'lizard': ('spock', 'paper'),
 'spock': ('scissors', 'rock'),
}

然后,您可以查询它,而不必重复许多if

if raj == sheldon:
   print("Case #{0}: Again!".format(i+1))
elif raj in options[sheldon]:
   print("Case #{0}: Bazinga!".format(i+1))
else: 
   print("Case #{0}: Raj cheated!".format(i+1))

答案 1 :(得分:0)

T = int(input())

for i in range(T):
    Sheldon, Raj = input().split(' ')

    if(Sheldon == Raj):
        Tie = True
    elif((Sheldon == "scissors" and (Raj in ["paper","lizard"])) or
         (Sheldon == "lizard" and (Raj in ["paper","Spock"])) or
         (Sheldon == "Spock" and (Raj in ["rock","scissors"])) or
         (Sheldon == "paper" and (Raj in ["rock","Spock"])) or
         (Sheldon == "rock" and (Raj in ["scissors","lizard"]))
        ):
        Win = True
    else:
        Lose = True

    if(Win == True):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Lose == True):
        print("Case #{0}: Raj cheated!".format(i+1))
    elif(Tie == True):
        print("Case #{0}: Again!".format(i+1))

    Win = Lose = Tie = False

答案 2 :(得分:0)

尝试使用字典

T = int(input())

for i in range(T):

    rules=  {
        "rock":     {"rock":0, "paper":-1,"scissors":1,"lizard":1,"Spock":-1},
        "paper":    {"rock":1, "paper":0,"scissors":-1,"lizard":-1,"Spock":1},
        "scissors": {"rock":-1, "paper":1,"scissors":0,"lizard":1,"Spock":-1},
        "lizard":   {"rock":1, "paper":-1,"scissors":1,"lizard":0,"Spock":-1},
        "Spock":    {"rock":1, "paper":-1,"scissors":1,"lizard":-1,"Spock":0}
        }
    Sheldon, Raj = input().split(' ') 

    Result = rules[Sheldon][Raj]
    if(Result == 1):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Result == -1):
        print("Case #{0}: Raj cheated!".format(i+1))
    else:
        print("Case #{0}: Again!".format(i+1))