【问题标题】:HackerRank Compare the Triplet. JavaScript solution vs. Python3HackerRank 比较三元组。 JavaScript 解决方案与 Python3
【发布时间】:2017-09-16 22:56:44
【问题描述】:

所以,我正在尝试解决来自 HackerRank (https://www.hackerrank.com/challenges/compare-the-triplets/problem) 的比较三胞胎问题。

我的 JavaScript 解决方案通过了所有测试用例,但 Python3 未能通过其中一些测试用例(HackerRank 不允许查看哪些测试用例)。如您所见,我遵循相同的逻辑,但在 Python 中它不起作用。我的 Python 和 JS 代码有什么区别?

JS:

function solve(a0, a1, a2, b0, b1, b2){
var alice = ( a0 > b0 ? 1 : 0 ) + ( a1 > b1 ? 1 : 0 ) + ( a2 > b2 ? 1 : 0 );
var bob = ( a0 < b0 ? 1 : 0 ) + ( a1 < b1 ? 1 : 0 ) + ( a2 < b2 ? 1 : 0 );

return [alice, bob];

}

Python3:

def solve(a0, a1, a2, b0, b1, b2):
alice = 1 if a0 > b0 else 0 + 1 if a1 >v b1 else 0 + 1 if a2 > b2 else 0
bob = 1 if a0 < b0 else 0 + 1 if a1 < b1 else 0 + 1 if a2 < b2 else 0
return (alice, bob)

【问题讨论】:

  • 出于某种原因,您决定不在 Python 版本中使用括号……?条件运算符的优先级仍然低于+。 (bool 也是int 的子类,所以你可以只使用(a0 &gt; b0) + (a1 &gt; b1) + (a2 &gt; b2),在JS 中也是如此。)

标签: javascript python-3.x


【解决方案1】:

在每个 if/else 之后使用括号

        alice = (1 if (a0>b0) else 0) + (1 if (a1>b1) else 0) + (1 if (a2>b2) else 0)
        bob = (1 if (a0<b0) else 0) + (1 if (a1<b1) else 0) + (1 if (a2<b2) else 0)

【讨论】:

    【解决方案2】:
    def compareTriplets(a, b):
        total_a = 0
        total_b = 0
        for i in range(len(a)):
            if a[i] > b[i]:
                total_a = total_a + 1
            elif a[i] < b[i]:
                total_b = total_b + 1
        return (total_a, total_b)
    

    幸运的是,我得到了与 Geraldo Braho 相同的逻辑,但很难在“For Loop Expression”中得到逻辑,我得到了我需要在“len(a)/len(b )”,因为在这种情况下两者的长度相同。非常感谢 Geraldo Braho 先生教我我们需要在“For 循环表达式”中使用“range(len(a))”。

    https://geraldo1993.github.io/articles/Compare-the-Triplets/

    【讨论】:

      【解决方案3】:

      您应该在每个 if/else 语句之后使用括号。因为 python bool 需要括号才能达到 100% 的准确度。比较的优先级高于布尔运算符。这是您修改后的代码:

      def solve(a0, a1, a2, b0, b1, b2):
        a = (1 if a0 > b0 else 0) + (1 if a1 > b1 else 0) + (1 if a2 > b2 else 0)
        b = (1 if a0 < b0 else 0) + (1 if a1 < b1 else 0) + (1 if a2 < b2 else 0)
        return (a,b)
      

      【讨论】:

        【解决方案4】:

        就像上面其他人的回答一样,您需要用括号括起来您的布尔运算。但是对于您的 python 代码,这是一种更好的编写方式。

        def compareTriplets(a, b):
            alice = sum(1 for i in range(len(a)) if a[i] > b[i] and not a[i] == b[i])
            bob = sum(1 for i in range(len(a)) if a[i] and not a[i] == b[i] and not a[i] > b[i])
            return [alice, bob]
        

        alice 和 bob 变量中的每一行都在一个范围内循环(在这种情况下,我们放入数组 a 的长度,因为 a 和 b 数组具有相同的长度)并且每次都会产生一个值 1条件,

        if a[i] &gt; b[i] and not a[i] == b[i] 代表爱丽丝,并且

        if a[i] and not a[i] == b[i] and not a[i] &gt; b[i] 表示 bob 很满意,

        然后将其相加为 alice 和 bob 的每个变量的整数值。黑客等级的问题要求您将其显示为数组,因此您需要将其作为数组返回。

        【讨论】:

        • 问题是两段代码之间的区别是什么。在为旧问题添加答案时,请务必注意您的答案所针对的问题的新方面。通常可以通过添加对它们的工作原理和原因的解释来改进纯代码的答案。
        【解决方案5】:

        我希望,这也有帮助

        a = list(map(int, input("Enter The First input").split()))
        b = list(map(int, input("Enter The Second input").split()))
        
        a_score = b_score = 0
        
        for i in range(len(a)):
            if a[i] > b[i]:
                a_score += 1
            elif a[i] < b[i]:
                b_score +=1
            else:
                pass
        
        print(a_score, b_score)
        

        【讨论】:

          【解决方案6】:
          def compareTriplets(a, b):
          d, g = (0, 0)
          for i in range(len(a)):
              if a[i] > b[i]:
                  d += 1
              elif b[i] > a[i]:
                  g += 1
          return d, g
          
          
          a = [int(i) for i in input('value of a').split(",")]
          b = [int(i) for i in input('value of a').split(",")]
          print(compareTriplets(a, b))
          

          【讨论】:

          • 用python比较三元组
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-16
          • 2018-09-13
          • 1970-01-01
          • 2020-05-14
          • 1970-01-01
          相关资源
          最近更新 更多