admin管理员组

文章数量:1317915

So, I am trying to solve Compare the Triplets problem from HackerRank ().

My JavaScript solution passes all test cases, but Python3 fails some of them (HackerRank doesn't allow to see which ones). As you can see I followed same logic but in Python it doesn't work. What is the difference between my Python and JS code?

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)

So, I am trying to solve Compare the Triplets problem from HackerRank (https://www.hackerrank./challenges/pare-the-triplets/problem).

My JavaScript solution passes all test cases, but Python3 fails some of them (HackerRank doesn't allow to see which ones). As you can see I followed same logic but in Python it doesn't work. What is the difference between my Python and JS code?

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)
Share Improve this question asked Sep 16, 2017 at 22:56 vladyKastavladyKasta 411 silver badge3 bronze badges 1
  • 1 You decided not to use parentheses in the Python version for some reason…? The conditional operator still has lower precedence than +. (bool is also an int subclass, so you can just (a0 > b0) + (a1 > b1) + (a2 > b2), and the same in JS.) – Ry- Commented Sep 16, 2017 at 22:59
Add a ment  | 

7 Answers 7

Reset to default 2

Use parentheses after every 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)
def pareTriplets(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)

Fortunately, I got the logic which is same as of Geraldo Braho, but struggled to get the logic in "For Loop Expression", I got the answer that I need to use range before "len(a)/len(b)" as both have the same lengths in this case. Big thanks to Mr. Geraldo Braho for teaching me that we need to use "range(len(a))" in the "For Loop Expression".

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

You should use parenthesis after every if/else statement. because python bool need parenthesis for 100% accuracy. Comparison has a higher precedence than Boolean operators. here's you'r modified code below:

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)

Like what others answered above, you need to parenthesize your bool operations. But regarding your python code, this is a better way to write it.

def pareTriplets(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]

Each line in the alice and bob variable loops over a range (in this case we put the length of array a, since both a and b arrays have the same length) and will yield a value of 1 for each time the conditions,

if a[i] > b[i] and not a[i] == b[i] for alice, and

if a[i] and not a[i] == b[i] and not a[i] > b[i] for bob is satisfied,

which will then be summated as the integer value of each variable of alice and bob. The question in hacker rank asks you to display it in an array so you need to return it as array.

function pareTriplets(a, b) {
    const alice = a
    const bob = b
    let aliceTotal = 0
    let bobTotal = 0
    
    for (let i = 0; i < alice.length; i++) {
        if (alice[i] > bob[i]) {
            aliceTotal = aliceTotal + 1
        } else if (bob[i] > alice[i]) {
            bobTotal = bobTotal + 1
        } else {
            continue
        }
    }
    return [aliceTotal, bobTotal]
}

as you can see this function is looping the array input and paring the value of the same index of array and if the one get higher value the total is adding 1 point.

this is my answer i hope this can help.

i Hope, This Also helps

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)
def pareTriplets(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(pareTriplets(a, b))

本文标签: python 3xHackerRank Compare the Triplet JavaScript solution vs Python3Stack Overflow