【问题标题】:Is it efficient to perform individual, nested if statements?执行单独的嵌套 if 语句是否有效?
【发布时间】:2011-02-13 00:44:16
【问题描述】:

我正在解决以下问题:

你开得太快了,警察拦住了你。编写代码来计算结果,编码为 int 值:0=无票,1=小票,2=大票。如果速度小于等于 60,则结果为 0。如果速度介于 61 和 80 之间,则结果为 1。如果速度大于或等于 81,则结果为 2。除非是你的生日——那天,你的在所有情况下,速度都可以提高 5 倍。

我想出了以下代码:

def caught_speeding(speed, is_birthday):
    if is_birthday == True:
        if speed <= 65:
            return 0
        elif speed <= 85:
            return 1
        else:
            return 2
    else:
        if speed <= 60:
            return 0
        elif speed <= 80:
            return 1
        else:
            return 2

我觉得单独检查一个有点效率低下,还是可以?

【问题讨论】:

  • 不要做if thing == True: -- if thing: 就够了。

标签: python boolean-logic


【解决方案1】:

你一定会喜欢 bisect 模块。

def caught_speeding(speed, is_birthday):
    l=[60,80]
    if is_birthday:
        speed-=5
    return bisect.bisect_left(l,speed)

【讨论】:

    【解决方案2】:

    我对您的代码没有任何问题。可读性强。

    如果你想减少行数,那么你可以这样做:

    def caught_speeding(speed, is_birthday):
        adjustment = 5 if is_birthday else 0
        if speed <= 60 + adjustment:
            return 0
        elif speed <= 80 + adjustment:
            return 1
        else:
            return 2
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      def caught_speeding(speed, is_birthday):
      
          if is_birthday:
              speed = speed - 5
      
          if speed <= 60:
              return 0
          elif speed <= 80:
              return 1
          else:
              return 2
      

      执行is_birthday == True 意味着您还没有完全得到布尔值;-)

      【讨论】:

        【解决方案4】:

        检查这个。已优化:

        def caught_speeding(speed, is_birthday):
         if speed in range(0,66 if is_birthday else 61):
           return 0
         elif speed in range(0,86 if is_birthday else 81):
           return 1
         return 2
        

        【讨论】:

          【解决方案5】:

          假设速度是一个整数,而效率是指跑步的速度,而不是理解的速度:

          >>> def t(speed, is_birthday):
          ...     speed -= 5 * is_birthday
          ...     return speed // 61 + speed // 81
          ...
          >>> for s in xrange(58, 87):
          ...     print s, t(s, False), t(s, True)
          ...
          58 0 0
          59 0 0
          60 0 0
          61 1 0
          62 1 0
          63 1 0
          64 1 0
          65 1 0
          66 1 1
          67 1 1
          68 1 1
          69 1 1
          70 1 1
          71 1 1
          72 1 1
          73 1 1
          74 1 1
          75 1 1
          76 1 1
          77 1 1
          78 1 1
          79 1 1
          80 1 1
          81 2 1
          82 2 1
          83 2 1
          84 2 1
          85 2 1
          86 2 2
          >>>
          

          【讨论】:

            【解决方案6】:
            def caught_speeding(speed, is_birthday):
                speed -= 5 * is_birthday
                return 0 if speed < 61 else 2 if speed > 80 else 1
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-03-04
              • 1970-01-01
              • 2013-12-06
              • 1970-01-01
              • 1970-01-01
              • 2020-09-06
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多