## Compare the Triplets

- SL
sagarlakhia18 + 21 comments Java:

pointsAlice = ((a0>b0)?1:0)+ ((a1>b1)?1:0)+ ((a2>b2)?1:0) ; pointsBob = ((a0<b0)?1:0)+ ((a1<b1)?1:0)+ ((a2<b2)?1:0) ; System.out.println(pointsAlice+" " +pointsBob);

Anil03 + 1 comment Thanks this worked in java 8

TriumphSan + 0 comments [deleted]

- AS
Adrish + 5 comments This is giving wrong solution in c#

sceleris + 4 comments Well yeah; Java and C# are completely different.

lopezdp + 0 comments [deleted]TriumphSan + 0 comments [deleted]TriumphSan + 2 comments [deleted]TriumphSan + 1 comment [deleted]TriumphSan + 0 comments [deleted]

TriumphSan + 1 comment [deleted]TriumphSan + 0 comments [deleted]

TriumphSan + 0 comments [deleted]

- KT
klaas_tojkander + 6 comments Yeah, it needs some polish. :) In C# it would be:

var pointsAlice = ((a0>b0)?1:0)+ ((a1>b1)?1:0)+ ((a2>b2)?1:0); var pointsBob = ((a0<b0)?1:0)+ ((a1<b1)?1:0)+ ((a2<b2)?1:0); Console.WriteLine(pointsAlice+" " +pointsBob);

- NC
MobilityWins + 0 comments so this would allow pointsAlice to have split results? because it is asking to have the results split for who got the point for all 3 comparisons

- MA
agrawalmayank26 + 1 comment what does var operator do?

- BB
blakeneybk + 0 comments var is a dynamic type variable that the compiler infers the type based on the assignment. Can't be used in a method signature.

- MK
murugesh1407 + 0 comments so do v need to take 6 diiferent variables for points like a0,a1..and so on ? can we solve it taking points in array ?

kamal6789 + 2 comments you will get all hackerrank solution at www.ask2exp.com

- VM
h170040061 + 0 comments thanks bro

chiragmak9999 + 0 comments www.ask2exp.com sie does not has ssl certificate

shropshireclovis + 4 comments So funny how in programming you can have totally different roads to the same solution. Here's mine:

static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2) { // Complete this function int alexScore = 0; int bobScore = 0; if(a0 != b0) switch (a0 > b0) { case true: alexScore++; break; case false: bobScore++; break; default: break; } if(a1 != b1) switch (a1 > b1) { case true: alexScore++; break; case false: bobScore++; break; default: break; } if(a2 != b2) switch (a2 > b2) { case true: alexScore++; break; case false: bobScore++; break; default: break; } int[] scoreArr = { alexScore, bobScore }; return scoreArr; }

- AS
axelstewart + 1 comment I have created what might be the worst possible functional solution:

int compare(int a, int b) { if (a > b) { return 1; } else if (b > a) { return -1; } else { return 0; } } vector < int > solve(int a0, int a1, int a2, int b0, int b1, int b2){ int a = 0; // Difference of A and B's scores int b = 0; // Number of points awarded in total int c = 0; // Current return value int d = 0; // This is A's score int e = 0; // This is B's score c = compare(a0,b0); a += c; b += abs(c); c = compare(a1,b1); a += c; b += abs(c); c = compare(a2,b2); a += c; b += abs(c); d = a + (b-a)/2; e = b - d; vector < int > retvals = {d, e}; return retvals; }

Good intentions lead me to bad code

wcursino + 0 comments Well, it is definetely a "cleaner" solution, as you can see the steps to come up with the answer...but takes a ton of memory alocation...

ogkmasum + 0 comments great great great work... that is like a programmer...

kamaljanghel + 0 comments thanks bro its right approach..

kamaljanghel + 0 comments but my logic

static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ int alice = 0; int bob = 0;

if(a0!=b0){ int c0 = ((a0>b0)?1:0); switch(c0){ case 1: alice++; break; case 0: bob++; break; default : break; } } if(a1!=b1){ int c1 = ((a1>b1)?1:0); switch(c1){ case 1: alice++; break; case 0: bob++; break; default : break; } } if(a2!=b2){

int c2 = ((a2>b2)?1:0); switch(c2){ case 1: alice++; break; case 0: bob++; break; default : break; } } int [] scor = { alice, bob }; return scor; }

- AM
ahmaazouzi + 1 comment Too bad python can't have such an answer

itsallaboutfaz + 3 comments Far more elegant and simple python3 solution.

def solve(a0, a1, a2, b0, b1, b2): arr = [a0, a1, a2, b0, b1, b2] return compare(arr, 0, len(arr) - 1, [0, 0]) def compare(a, start, stop, result): if start >= stop: return str(result[0]) + str(result[1]) elif a[start] > a[stop]: result[0] += 1 elif a[start] < a[stop]: result[1] += 1 return compare(a, start + 1, stop - 1, result)

- CH
Colby_Hepworth + 0 comments [deleted] mirkwood_ape + 3 comments Very easy to understand but you might want to use some python features, i.e., list comprehension

def solve(a0, a1, a2, b0, b1, b2): A = [a0, a1, a2] B = [b0, b1, b2] C = sum([1 if x[0] > x[1] else 0 for x in zip(A,B)]) D = sum([1 if x[1] > x[0] else 0 for x in zip(A,B)]) return (C, D)

- [U
[deleted] + 0 comments [deleted] - [U
[deleted] + 0 comments [deleted] - NN
jamesanon00000 + 0 comments Python3 one-liner:

def solve(a, b): return map( lambda t: sum([x > y for x, y in zip(*t)]), ((a, b), (b, a)) )

Equivalent to:

def solve(a, b): def compare_sum(tuple_): return sum([x > y for x, y in zip(*tuple_)]) return map( compare_sum, ((a, b), (b, a)) )

- OK
owenklaiss + 0 comments For your solution to actually work on more than the default problem set I believe you need to set arr to be, otherwise you are not comparing the correct integers.

arr = [a0, a1, a2, b2, b1, b0]

- TA
tony_arslan + 1 comment Here is c# that works

`int pointsAlice = ((a0>b0)?1:0)+ ((a1>b1)?1:0)+ ((a2>b2)?1:0); int pointsBob = ((a0<b0)?1:0)+ ((a1<b1)?1:0)+ ((a2<b2)?1:0); return new int[] {pointsAlice, pointsBob};`

- JB
jpa_beemsterboer + 1 comment How about this one? Tuples are by far one of my favorite things in C#.

static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2) { int[] result = new int[2]; var tuples = new List<Tuple<int, int>>() { Tuple.Create(a0, b0), Tuple.Create(a1, b1), Tuple.Create(a2, b2) }; foreach(var tuple in tuples) { if (tuple.Item1 > tuple.Item2) result[0] += 1; else if (tuple.Item2 > tuple.Item1) result[1] += 1; } return result; }

- CA
cleytonnurani + 0 comments Boa soluÃ§Ã£o mas nÃ£o contempla a restriÃ§Ã£o de >= 1 e <= 100.

- AM
anurag_malani + 0 comments Try to convert input to 2 seprate arrays and loop them as per the given condition. Later append count values in return array.

- CR
carlosrobertode1 + 0 comments int alice = Convert.ToInt32(a0 > b0) + Convert.ToInt32(a1 > b1) + Convert.ToInt32(a2 > b2); int bob = Convert.ToInt32(a0 < b0) + Convert.ToInt32(a1 < b1) + Convert.ToInt32(a2 < b2);

return new int[] { alice, bob };

- AP
aditya6199 + 2 comments it doesn't checked constraints provided i.e numbers should lie between 1 & 100 (both included)

eavestn + 2 comments Don't know why this is downvoted? This question is raised by the problem. Int32 supports greater than 100 and is unbounded (so less than 0). +1

- AP
aphamx_mail + 0 comments Because they tell us the constraints of the values already.

jimmy1087 + 1 comment I had the same doubts, searched on FAQ section and on google about constraints in HackerRank but found little.

I was getting it wrong, the constraints have already been taken into consideration when building the test cases. So do not worry about checking constraints within your solution.

Actually checking constraint will add up delays that most likely will affect time solving in more difficult problems.

jimmy1087 + 0 comments Why is this downvoted ?

- PS
priyankashawpas + 0 comments yes

- C
gps_gaurav + 2 comments java :

int x = (a0 > b0? ascore++ : (a0 == b0 ? 0 : bscore++)) + (a1 > b1? ascore++ : (a1 == b1 ? 0 : bscore++)) + (a2 > b2? ascore++ : (a2 == b2 ? 0 : bscore++));

`System.out.println(ascore +" "+bscore);`

- PJ
pradeep3995 + 1 comment works in c# too

unique_here + 0 comments [deleted]

mehdi_mmik + 1 comment Good luck to maintain your code...I hope you are not codding like that everywhere.

- S
sergiu_lucec + 0 comments these hipster scripts are funny though

- WS
wlsc1 + 4 comments Complete solution in Java:

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a0 = in.nextInt(); int a1 = in.nextInt(); int a2 = in.nextInt(); int b0 = in.nextInt(); int b1 = in.nextInt(); int b2 = in.nextInt(); int pointsAlice = ((a0>b0)?1:0)+ ((a1>b1)?1:0)+ ((a2>b2)?1:0) ; int pointsBob = ((a0<b0)?1:0)+ ((a1<b1)?1:0)+ ((a2<b2)?1:0) ; System.out.println(pointsAlice+" " +pointsBob); } }

- AC
anisac + 3 comments Hello,

Thank you for sharing your solution. Can you please explain this line of code: int pointsAlice = ((a0>b0)?1:0)+ ((a1>b1)?1:0)+ ((a2>b2)?1:0) ;

Thank you for explaining.

abelardo + 6 comments These are ternary operators, so (a0>b0)?1:0 is essentially saying "evaluate a0>b0, if it's true evaluate to 1, otherwise 0". Basically this is the form:

(boolean expression)? answer if true : answer if false;

- AC
anisac + 0 comments Thank you so much for explaining this concept. Your expalanation is very clear and easy to understand. Thank you for your time.

leoman + 0 comments Thanks for making it understandable.

- RS
rahulsingh_r243 + 0 comments Thank you for explaining the concept of Ternary operators.

will96 + 1 comment Im confused, for the other answers how I'm reading this would work is essentially;Say these are both going to equal true, if (a0 > b0) you get aliceScore = 1. And then when you go to the next if (a1 > b1) you get aliceScore = 1 again. Instead of aliceScore += 1; Can you help me understand that part. Basically I'm not understanding where is the (if true +1), I'm only seeing (if true set it to 1).

- NR
nareshravlani007 + 0 comments Those + signs are included between all the conditions. So, it would be 0\1 + 0\1 + 0\1, depending on the outcome of the logical conditions.

- BB
blakeneybk + 0 comments sweet explanation of the syntax. Thanks.

- SK
saikrishsubbu + 0 comments How this particular code set works in c?

- KG
komalgoyal + 0 comments [deleted] unique_here + 2 comments [deleted]- PJ
pradeep3995 + 0 comments Ternary operator is same as if - else if we use ternary operator we can write the if(condition) do this; else do this; in same line using condition ? true do this : false do this

- MS
emsurowiec + 1 comment We use them. While the ternary operator is nice and concise in this particular solution, it will not scale as soon as you add more data. Just imagine the code you'd have to write if it turned out that its 10 or 100 scores that you need to compare - instead of 3.

- BB
blakeneybk + 0 comments You would just write a comparator method that would take two int variables and reuse it.

- BC
bcampbell_v3 + 1 comment With your line pointsAlice = ((a0>b0)?1:0)...

What is this type of expression called? I understand it's boolean expressions - but is there somewhere I can read more about this kind of syntax?

aditiravi96 + 0 comments Late reply, Sorry.

Its the conditional operator - " ?: ".It takes in 3 operators, usually the shorthand of If-else statement.

So if(a < b) x = a; else x = b;

could be written as ((a

ahmadkuedr + 0 comments This equation took from me two days, and you solved it just in tow line !! OMG

- RS
rishabhstc + 0 comments this method is nice thanx

- KK
kennethchoi19 + 1 comment That is actually pretty interesting can you tell me what is the name of this syntax?

- SK
kibarabara + 0 comments Ternary operator.

- PS
parag_s + 1 comment I used if and if else, and its very long, whereas your code seems very short and easy. What is this sort of code called? I want to read more about these shorter codes. Thank You

- SK
An00nymous + 1 comment these are called ternary operators

- AC
anisac + 2 comments Can you please explain more about ternary operators or provide some additional documentation? Thank you.

- BK
bhaskarkrishna_1 + 0 comments Ternary operator is same as if - else if we use ternary operator we can write the if(condition) do this; else do this; in same line using condition ? true do this : false do this

- SD
swapnilbilimale1 + 0 comments These are ternary operators, so (a0>b0)?1:0 is essentially saying "evaluate a0>b0, if it's true evaluate to 1, otherwise 0". Basically this is the form:

(boolean expression)? answer if true : answer if false

- SS
srivatsansridha1 + 0 comments how to return the values from this function?

- DP
an0o0nym + 7 comments Similar solution in Python3:

def solve(a0, a1, a2, b0, b1, b2): # Complete this function 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)

- DM
dcmurray + 0 comments [deleted] - VK
vishwask1 + 0 comments Perfect!

- KL
kevin_lin790 + 1 comment You can abuse Python type conversions, to shorten this even more :)

a = (a0 > b0) + (a1 > b1) + (a2 > b2)

- SC
cammywammy + 1 comment And to take it a step further, just do it in one line :-)

def solve(a0, a1, a2, b0, b1, b2):

`return (a0>b0)+(a1>b1)+(a2>b2),(b0>a0)+(b1>a1)+(b2>a2)`

leerun1409 + 2 comments Facepalm myself when i saw your answer...

def solve(a0, a1, a2, b0, b1, b2): # # Write your code here. # a = [a0,a1,a2] b = [b0,b1,b2] alice = [x for x in list(map(lambda x,y: x>y, a,b)) if x==True] bob = [x for x in list(map(lambda x,y: x>y, b,a)) if x==True] return len(alice),len(bob)

mirkwood_ape + 0 comments no worry brotha, yours is almost exactly the same with mine

def solve(a0, a1, a2, b0, b1, b2): A = [a0, a1, a2] B = [b0, b1, b2] C = sum([1 if x[0] > x[1] else 0 for x in zip(A,B)]) D = sum([1 if x[1] > x[0] else 0 for x in zip(A,B)]) return (C, D)

- RK
radhakrishnach + 0 comments I think this would a one liner.

return sum(list(map(lambda x,y: x > y, a,b))), sum(list(map(lambda x,y: x < y, a,b)))

- MS
mateenrehan + 0 comments The code doesnt check the constraints like a0 is between 1 and 100 and same with every other variables.

B14cK_3eaRD + 0 comments i think else 0 is not needed

- MR
hellomici + 0 comments [deleted] - R
redtigralj + 0 comments do you really need "else 0" here? what if you omit it?

- BC
bcampbell_v3 + 0 comments Is there a technical name for this type of comparison? I'd like to look more into this

akshu151112036 + 0 comments why its is showing this error -"missing return"??

- BK
bhaskarkrishna_1 + 0 comments the function returning integer pointer right? and this is received in main by a pointer result.we have to sent comparision values of two person,how is it possible to return two values to single pointer??can anyone explain plzzz

- AT
tummaaditya + 0 comments what to return back?

- DY
DanYoo940 + 0 comments Dang... short and good..

akashingole1 + 1 comment can u plzz tell why you have used + operator.

jh5372 + 0 comments the + operator is to add the results of each comparison so that pointsAlice and pointsBob contain the sum of all the comparison points earned

- DD
dhimandhruv + 0 comments what about checking limit 0

- HA
hitheshamin87 + 0 comments can you tell me how you solved this with explanation.

- SJ
sup_reeth + 0 comments tysm for helping me

akashingole1 + 0 comments can you please tell me its working.

jean_fmc + 0 comments Better:

static int boolToInt(boolean condition){ int retorno = (condition)?1:0; return retorno; } static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ int[] retorno = new int[2]; retorno[0] = 0; //Alice retorno[1] = 0; //Bob retorno[0] = boolToInt(a0>b0)+boolToInt(a1>b1)+boolToInt(a2>b2); retorno[1] = boolToInt(a0<b0)+boolToInt(a1<b1)+boolToInt(a2<b2); return retorno; }

- MA
mo_afolayan + 0 comments What a lovely solution. I never would've thought to look at it that way. What was your thought process?

- TH
dogejim + 3 comments static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ // Complete this function int[] solve = {0, 0}; int[] a = {a0, a1, a2}; int[] b = {b0, b1, b2}; for (int i = 0; i < 3; i++){ if (a[i] > b[i]) solve[0] += 1; if (a[i] < b[i]) solve[1] += 1; } return solve; }

This code worked for me in C#... Threw the variables into new arrays, guess would be easier just to toss it arrays? then compared each in the for loop.

kevingalloway + 1 comment That is almost exactly how I did it. haha Arrays seemed most logical to me.

`static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ // Complete this function int[] alice = {a0, a1, a2}; int[] bob = {b0, b1, b2}; int[] result = {0,0}; for(int i=0; i<3; i++){ if(alice[i] > bob[i]){ result[0]++; } else if(alice[i] < bob[i]) result[1]++; } return result;`

- JS
jonsultana + 2 comments You should never hard code numbers(0 is ok but any other number has no meaning). What is position 0 in the array? What is position 1 in the array? Create an enumeration to help give the positions within the array meaning.

public enum Names : byte { Alice, // Alice = 0 Bob // Bob = 1 } static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ // Complete this function int[] alice = {a0, a1, a2}; int[] bob = {b0, b1, b2}; int[] result = {0,0}; for(int i=0; i<3; i++){ if(alice[i] > bob[i]){ result[(int)Names.Alice]++; } else if(alice[i] < bob[i]) result[(int)Names.Bob]++; } return result;

akuma_majunia + 0 comments Very nice!

ben_cardinal + 1 comment Why no {} for the else if?

- LY
leonteng91 + 0 comments You don't have to, if it is only one line statement~ =D if there are multiple line, then you will have to put {}. For example:

if (a == 0) c++; else if (a == 1) c--; else if (a ==2) { b++; b+c; }

- P
prakashjoshi55 + 2 comments isn't it hard coded? solve can take only two elements

m_qasim_nu + 1 comment The problem is hard coded. "Triplets"

pankajbabu + 0 comments ya bro

- VP
veerabahu98 + 0 comments ohh kkk

Rehmanali + 0 comments Awesome!

- M
marcell_made + 5 comments I'm using Javascript and for me it's saying my output is correct, but it's not letting me move on because of a runtime error on line 47 with result.join being undefined.

- AS
kyoko86 + 0 comments Same problem here!

- PB
hackerrank673 + 0 comments [deleted] - VV
vitorviannaribe1 + 0 comments Same here :(

sppower + 1 comment same here. I kept losing my cool, thinking I did something wrong. I tried multiple solutions, arrived at the correct output 3 -way times, but getting a runtime error with result.join as you say. Same for python. LAME!

- VV
vitorviannaribe1 + 1 comment If you are using JS, just do it. Change the line 55 for "ws.write(result += "\n");"

- NS
codEinstein + 1 comment I'm having same problem. The code works for my test cases, but does not let me submit due to error of "join" not being recognized.

Victorviannaribe1 your solution doesnt work. Tenta de novo

- DF
d_fegan + 0 comments Is your function returning a string? If so, this is the issue. The join method expects an array, so you want to be returning something in the format [aScore, bScore].

Then, result.join will convert the array into the expected output format.

This at least worked for me, hope my explanation is of some use to you

erocheleau + 0 comments Yea if you chose the JavaScript (nodejs) template for the response, the solve method expects you to return an array.

[scoreAlice scoreBob]

But tbh you should be able to figure that out by reading the error ;) Good luck.

- AC
lumiere04 + 7 comments Hi, here is a c++ solution:

int a0; int a1; int a2; cin >> a0 >> a1 >> a2; int b0; int b1; int b2; cin >> b0 >> b1 >> b2; int alice = (a0 > b0)*1+(a1>b1)*1+(a2>b2)*1; int bob = (b0>a0)*1+(b1>a1)*1+(b2>a2)*1; cout << alice << " " << bob << endl;

Best

- MV
madhavverma1998 + 0 comments should use function

- ED
elvis_dukaj + 1 comment Even better c++ code:

#include <vector> #include <iterator> #include <algorithm> #include <numeric> #include <iostream> using namespace std; int main() { vector<int> a(3), b(3); copy_n(istream_iterator<int>(cin), 3, begin(a)); copy_n(istream_iterator<int>(cin), 3, begin(b)); cout << inner_product(begin(a), end(a), begin(b), 0, plus<int>(), greater<int>()) << ' ' << inner_product(begin(b), end(b), begin(a), 0, plus<int>(), greater<int>()) << endl; return 0; }

glennlopez + 0 comments works, why was this downvoted?

- RS
therealrishabhs1 + 1 comment Over here why have you multiplied the brackets by 1.I mean does this signify anything?

- JS
jonsultana + 1 comment I know this is late but in C++ booleans are defined as 0 or 1. In C++ you can write a conditional statement like if(0){ {// The condition is false} if(1){// the condition is true.} As (a1 > b1) stands is a boolean but by multiplying my 1 will cast the result to a int.

- S
sibeldeshong + 0 comments Even later but so couldn't you also just explicitly cast them to int instead. I'm new to C++ but I believe it is done by writing static_cast(a0 > b0) right?

- TA
tanveermtm + 0 comments super

- AV
amanvr2 + 0 comments how can we return value in this code?

- SS
itissandeep98 + 0 comments " int alice = (a0 > b0)*1+(a1>b1)*1+(a2>b2)*1; int bob = (b0>a0)*1+(b1>a1)*1+(b2>a2)*1;" the above line will show up a syntax error.

apschauhan700 + 0 comments simplest code in c here:

(https://github.com/akkshaychauhan/hacckerank-code/blob/master/Algorithms:Compare%20the%20Triplets)

- SG
SyedGhouri + 1 comment Python :

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

- JD
jagan_9_85 + 0 comments static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2) {

`int[] aScore = {a0,a1,a2}; int[] bScore = {b0,b1,b2}; int aliceScore=0, bobScore = 0; for(int i=0; i < 3; i++){ if(aScore[i] > bScore[i]){ aliceScore += 1; }else if (bScore[i] > aScore[i]){ bobScore += 1; } } int[] result = {aliceScore,bobScore}; return result; }`

- TW
gameplace123 + 4 comments I'm sure there's a much better way to do this with Python 2, but this is what I got

a = [int(x) for x in raw_input().split()] b = [int(x) for x in raw_input().split()] print sum([1 for x in range(3) if a[x] > b[x]]), sum([1 for x in range(3) if a[x] < b[x]])

- KJ
kedarj14 + 5 comments A = list(map(int, raw_input().strip().split())) B = list(map(int, raw_input().strip().split())) Ascore = len([1 for a,b in zip(A,B) if a>b]) Bscore = len([1 for a,b in zip(A,B) if b>a]) print Ascore, Bscore

Above code will work for any length of vector. You can use sum instead of len as well.

- DL
malanglove + 0 comments Ascore = sum(a>b for a, b in zip(A, B))

should work as well.

shBomberman + 1 comment Although the solution is nice, a question that comes to mind is how fast does the code above run for larger values of n compared to just the traditional single for loop pass, iterating through both lists at once - considering that you are given the initial size of the lists.

bryce_paul_guin1 + 0 comments Iterating through a loop twice and having one check versus iterting through a loop once and having two checks is still ~2n

- M
msambitkumar1991 + 0 comments @kedarj14 well perfect code. But how to justify the code in terms of time complexity? Is this the most optimum code?

bryce_paul_guin1 + 0 comments I can't decide if I like aggregating the lines together better or not.

import sys lines = (line.rstrip().split(' ') for line in sys.stdin) pairs = zip(*((int(elem) for elem in line) for line in lines)) cols = zip(*((a>b, a<b) for a,b in pairs)) print(' '.join(str(sum(col)) for col in cols))

- H
helderIO + 0 comments what about the constraits?

aurelien_monot + 0 comments Another python 2 solution. Not better, because less readable but works independantly of the range:

a = map(int,raw_input().split()) b = map(int,raw_input().split()) print ' '.join(map(str,map(sum,zip(*[[x>y,y>x] for x,y in zip(a,b)]))))

runcy + 0 comments A little modification for any length:

print(sum(1 for x in range(len(a)) if a[x] > b[x]), sum(1 for x in range(len(b)) if b[x] > a[x]))

- JT
Jlookup + 1 comment New to Python 3. Just learned about list comprehensions so I'm using them everywhere. Feedback please.

a = [int(x) for x in input().strip().split(' ')] b = [int(x) for x in input().strip().split(' ')] # 1 is a point for Alice, -1 a point for Bob score = [1 if x > y else 0 if x == y else -1 for x, y in zip(a, b)] print (score.count(1), score.count(-1))

edit: replaced sum() with .count

robipolli + 1 comment Nice but you're iterating 3 times:

- zip, count, count

A classical solution could be faster

In [7]: def f1(a,b): ...: score = [1 if x > y else 0 if x == y else -1 for x, y in zip(a, b)] ...: ...: (score.count(1), score.count(-1)) ...: In [8]: %timeit f1(a,b) 100000 loops, best of 3: 13.4 Âµs per loop In [11]: def f2(a,b): ...: s1,s2=0,0 ...: for x,y in zip(a,b): ...: if x>y: s1 += 1 ...: elif y<x: s2+= 1 ...: ...: In [12]: %timeit f2(a,b) 100000 loops, best of 3: 9.18 Âµs per loop

- AC
peter4bbit + 0 comments if x>y: s1 += 1 elif x<y: s2+= 1

elif condition is swapped

addison_ + 1 comment Here's another Python3 example. A bit lengthy, but gets the job done:

def solve(a0, a1, a2, b0, b1, b2): alice = [a0, a1, a2] bob = [b0, b1, b2] score = [0, 0] for a, b in zip(alice, bob): if a > b: score[0]+= 1 elif b > a: score[1]+=1 return score

emanweg + 0 comments this is what I aimed at for

The zip function is vital.

Good work!

- AS
15BCS077_PINK + 1 comment C++ Program for the default function given

`int alice = ((a0 > b0)?1:0) + ((a1>b1)?1:0) + ((a2>b2)?1:0); int bob = ((a0 < b0)?1:0) + ((a1<b1)?1:0) + ((a2<b2)?1:0); vector <int> v; v.push_back(alice); v.push_back(bob); return v;`

}

- KI
iyerkrithika21 + 1 comment can you please explain how vector v works ?

- AS
15BCS077_PINK + 0 comments Vector is a container just like an array and stores value just like an array in a contiguous memory. But the difference is that it use dynamically allocated array.Vector can be of type int, char, bool etc.

- D
djnme + 1 comment Any more elegant bash solutions?

a=0 b=0 read -a A_arr read -a B_arr for i in $( seq 0 2 ); do eval a$i=${A_arr[$i]} eval b$i=${B_arr[$i]} if (( $(echo $((a$i))) > $(echo $((b$i))) )); then (( a+=1 )) elif (( $(echo $((a$i))) < $(echo $((b$i))) )); then (( b+=1 )) fi done echo $a $b

- NS
nitins12353 + 0 comments [deleted]

- RP
motta + 3 comments - RR
ramMSD + 0 comments very interesting

- RA
raseedahamed98 + 0 comments [deleted] - RR
ramMSD + 0 comments

Sort 1031 Discussions, By:

Please Login in order to post a comment