- Practice
- Algorithms
- Warmup
- Compare the Triplets
- Discussions

# Compare the Triplets

# Compare the Triplets

sagarlakhia18 + 34 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 + 3 comments Thanks this worked in java 8

TriumphSan + 2 comments [deleted]sunnychegondi123 + 0 comments ok

sowmyakrishna043 + 0 comments r u working in any software company

srikalyanmarri91 + 8 comments i was working on c can get any help

chaitanya_gopis1 + 0 comments [deleted]moazashraf007 + 3 comments # include

void main() { int a[3], b[3]; int alice, bob; int i; for(i = 0; i < 3; i++) { scanf("%d",a[i]); } for(i = 0; i < 3; i++) { scanf("%d",b[i]); } alice = bob = 0; for(i = 0; i < 3; i++) { if(a[i] > b[i]) alice++; else if(b[i] > a[i]) bob++; } printf("%d %d",alice, bob); }

Aminson_Jets + 4 comments Its olsom doen't work, could you look at it?

moazashraf007 + 0 comments [deleted]moazashraf007 + 0 comments Go to this link and see the correct C program https://1drv.ms/u/s!AqMxrg-ZY3J0lu8xxXx53_C0T9869Q The prvious one could not be typed correctly

ashwini_indiaro1 + 2 comments y it doesnot work

tchaitanya_agm + 1 comment u shoud add a[i]==b[i] condition also

_abhishek_dua + 0 comments without this condition,it will work also

sunnychegondi123 + 0 comments i dont know

vivek2sahu + 0 comments as there are lot of synthax errors in it

sergrantwill + 0 comments You have coded in c language

krishnakantkhat1 + 2 comments Hello Sir, if a[3]==b[3] then what to do(which condition) to not to increment the point in any one of then;

neel9010 + 0 comments It will not count that and won't add any points

niteshkumart_cs1 + 0 comments you can use "continue" statement with this condition or u can use "a_count+=0" too

Trazire + 0 comments [deleted]anandrk1918 + 0 comments int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count) {

*result_count = 2; int x = 0, y = 0, i; for (i = 0; i < 3; i++) { if ((*(a + i)) > (*(b + i))) { x++; }`if ((*(a + i)) < (*(b + i))) { y++; } } int* z = (int *)malloc(4); *z =x; z++; *z=y; return(--z);`

}

yadlapallibhara2 + 1 comment u can get in google

dm_designpulp + 2 comments Compare The Triplets problem is very simple and similar to comparing marks of three subject of two students in a way like if subject1, student1 got high marks than student2 than student1 got 1 point and the same repeat for subject2, and subject3 at the end print the both student points. Maximum points can a student earn is 3 or minimum can be 0 or there may be the equal point if in subject1 both students got the same point. Check the explanation section for better understanding.

**Explanation:-**Let's take an example and compare with our logic, suppose two students are Bob and Alice and Bob got 56, 75, 26 marks and Alice got 84, 45, 26 Now Compare The TripletsBob Marks===> 56, 75, 26 Alice Marks==> 84, 45, 26

Bob Marks Alice Marks Bob Point Alice Point

56 < 84 0 1 75 > 45 1 0 26 = 26 0 0

So we can see that Bob and Alice got 1, 1 point in tale That is an answer print on the screen.

**Read Full Article**Compare The Tripletscoolrazor26 + 0 comments lol thats already written there ._.

navinchainani74 + 1 comment plz send code in java7

Kanahaiya + 0 comments Hi,

I hope it will help you to solve your query.

https://www.youtube.com/watch?v=rFUZAtwXiII&list=PLSIpQf0NbcCltzNFrOJkQ4J4AAjW3TSmA

I would really appreciate your comments and feedback on my video.

vikky4424 + 0 comments sir,which logic should be used.

meyopih224 + 0 comments # In C++

# Best solution

# In O(1)

vector<int> compareTriplets(vector<int> a, vector<int> b) { int al = 0, bo = 0; al += a[0] > b[0]; bo += b[0] > a[0]; al += a[1] > b[1]; bo += b[1] > a[1]; al += a[2] > b[2]; bo += b[2] > a[2]; return {al, bo}; }

devanshgoel13031 + 0 comments I wanted your help. Please, provide me the function definition Compare the Triplets problem. I am very annoyed while trying to solve it.

mr_kyawwaiyanly1 + 0 comments lol

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

sceleris + 5 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]akashnandhakuma1 + 0 comments Yeah bruh.. But I dont think so...Can u explain

klaas_tojkander + 7 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);

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

agrawalmayank26 + 1 comment what does var operator do?

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

gzoref + 0 comments Java version 11 now has dynamicc typing in some cases!

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 + 3 comments you will get all hackerrank solution at www.ask2exp.com

h170040061 + 0 comments thanks bro

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

m_esen22 + 0 comments I couldn't find any solution at this website. Can you tell us how?

shropshireclovis + 10 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; }

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; }5m0k3c0d3 + 0 comments such a bad code

chuucksc + 0 comments I would recommend to use one single segment of code for same functionality. Don't repeat your self.

christian_mosri + 0 comments Realy clean code and more easy to understand. Awesome job mate.

Bertram_Ray + 0 comments u can use while loop to simplify the code

Pafkers + 0 comments This is really bad code. In programming you should always have extending in mind when writing code. Now imagine you have to compare one hundred grades, with your approach you would have to use 200 variables

crownclown67 + 0 comments not really readable

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

itsallaboutfaz + 7 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)

Colby_Hepworth + 0 comments [deleted]mirkwood_ape + 9 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)

[deleted] + 0 comments [deleted][deleted] + 0 comments [deleted]jamesanon00000 + 8 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)) )

Daniel_Ng_XoR + 1 comment why nobody upvoted this? its really nice :D

k_mouratidis + 0 comments It shouldn't be. It goes over the two lists twice. What if they have 1M elements? This is not really readable, but should be faster. The bool translates ai-bi to 1 if there is a difference, or 0. The if/else gives (b,0) if Alice wins, and (0,b) if it is a draw or Bob wins, where b is 0 if the scores are equal, and 1 otherwise.

def compareTriplets(a, b): diff = [(bool(ai-bi),0) if (ai-bi)>0 else (0, bool(ai-bi)) for (ai,bi) in zip(a,b)] return list(map(sum, zip(*diff)))

Then again, for larger arrays using numpy and looping twice would be faster (and simpler) than the custom python solution:

def compareTriplets(a, b): return (a > b).sum(), (a < b).sum()

saihitesh123 + 2 comments i'm new to python. can someone explain this code to me .Thanks in advance

thangible + 0 comments I think this guy is using zip unwisely. There is no need to use expand, zip tuples like that. It is just confusing.

Khoga + 0 comments [deleted]Khoga + 0 comments unreadable code. This type of "clever" code always take long time to debug when there is a bug! It is not maintainable code.

mohit_creed + 0 comments [deleted]h809393860 + 0 comments good job!

danialidhamyahya + 0 comments I just did the simplest version lmao..

def compareTriplets(a, b): alice = 0 beam = 0 sizeOfList = len(a) for i in range(len(a)): if(a[i] > b[i]): alice = alice + 1 elif(a[i] < b[i]): beam = beam + 1 return alice,beam

Daniel_Ng_XoR + 0 comments looking for python solutions only xD nice one , nice usage of zip ;>

SATANNERGAL + 0 comments do you see it easy? this is not for beginner

seeon321 + 0 comments plz explain your code

billings_hugh97 + 0 comments I was wondering how it was done with zip, thanks for the enlightenment

gondaimgano + 0 comments beautiful!

pier_sora + 0 comments good job

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]

billings_hugh97 + 0 comments def compareTriplets(a, b): growth = [0,0] for i in range(0,3): if (a[i] > b[i]): growth[0] = growth[0] + 1 elif (a[i] < b[i]): growth[1] = growth[1] + 1 return growth

This was my Python 3 attempt

apurvanigam + 3 comments We can just use a single for loop and maintain two counters for points. something like this in Python2:

def compareTriplets(a, b): alice_points = 0 bob_points = 0 for i in range(3): if a[i] > b[i]: alice_points +=1 elif a[i] < b[i]: bob_points += 1 return [alice_points, bob_points]

sydvill226 + 0 comments Thanks even though I used the zip() function and didn't have to slice the indices - python 3

_savage_carol_ + 1 comment is it working properly

sydvill226 + 0 comments yes and it passed all he tests

robmcortez + 0 comments This is the ideal answer for readibility. However I would problably change your for loop to:

for i in range(len(a)): ...

This would allow for a number of scores other than 3.

prathamesh_bage1 + 0 comments def compare_triplets(a,b): alice = bob = 0 for i,j in zip(a,b): if i > j: alice += 1 elif i < j: bob += 1 return [alice,bob]

navinchainani74 + 0 comments why we use len(arr)-1

domjwinder + 3 comments Maybe too simple... static int[] solve(int[] a, int[] b) {

`int aliceResult = 0; int bobResult = 0; for(int i = 0; i < 3; i++) { if(a[i] > b[i]) { aliceResult++; } if(a[i] < b[i]) { bobResult++; } } return new int[]{ aliceResult, bobResult}; }`

dineshreddyatp2 + 1 comment heyy not executing man

meetparmar671 + 0 comments [deleted]

pushembekar + 0 comments I had a similar solution! Just to be a little nitpicky you can make the second "if" statement within the for loop as an "else if" so that the condition doesn't get evaluated if the first one is true already.

visheshsahni + 7 comments Similar solution, just make sure you make your programs dynamic so that you have minimilistic hardcoding. I hope you will understand comapring below and your's

why faster and scaleable? Linked list faster than array list, use of else if.

scalable because I haven't specified the hardcoded length for inputs

`static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) { int aWin = 0; int bWin = 0; List<Integer> win = new LinkedList<Integer>(); for (int i = 0; i < a.size(); i++) { if (a.get(i) > b.get(i)) ++aWin; else if (a.get(i) < b.get(i)) ++bWin; } win.add(aWin); win.add(bWin); return win; }`

swapnilkumar1102 + 0 comments Best Solution.Thanks

adiman64 + 0 comments bro u are a legend thanks

shuklav776 + 0 comments tnq broh

Ani_singh + 0 comments hey, ur code was nice,i did a lil modification...

int alice = 0; int bob = 0; List score = new LinkedList(); for (int i = 0; i < 3; i++) { if(a.get(i)==b.get(i)) continue; if(a.get(i) > b.get(i)) alice++; else bob++; }

`score.add(alice); score.add(bob); return score; } :-)`

solodkovvi + 0 comments In this case better to use ArrayList, we are working with this vars like an arrays and know their sizes. https://habr.com/ru/post/124909/ table show size in byte. (imho, good decision, did it likewise)

srushtibobade + 1 comment I think you havent covered the third condition a[i]=b[i]

pandeysdr16 + 0 comments no need cover the third condition.

goplu25 + 0 comments Hi, Can you explain how is this code dynamic in nature?

tony_arslan + 6 comments 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};`

jpa_beemsterboer + 2 comments 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; }

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

slawton3 + 1 comment This is much cleaner code for C#:

`var counta = ((a[0]>b[0])?1:0)+((a[1]>b[1])?1:0) + ((a[2]>b[2])?1:0); var countb = ((b[0]>a[0])?1:0)+((b[1]>a[1])?1:0)+((b[2]>a[2])?1:0); return new List<int> {counta, countb};`

neel9010 + 0 comments Sorry i have to disagree with you.

- Its hard to read.
- If there are more than 3 inputs in future , your code needs to be changed.

Now if you look at my code, it will work for more than 3 inputs without changing anything.

static List<int> compareTriplets(List<int> a, List<int> b) { var a_point = a.Count(x => x > b[a.IndexOf(x)]); var b_point = b.Count(x => x > a[b.IndexOf(x)]); return new List<int> {a_point, b_point}; }

tauseefAB + 0 comments your code not working on c#

lucaeprov + 0 comments Here is c#!

static List compareTriplets(List a, List b) { List score = new List( new int[]{0, 0}); int i =0;

`for(i=0; i<count(a);i++){ if(a[i]>b[i]){ score[0]+=1; }else if(a[i]==b[i]){ score[0]+=0; score[1]+=0; }else{ score[1]+=1; } } return score; } static int count(List<int> aList){ int counter = 0; foreach(var item in aList){ counter++; } return counter; }`

lucaeprov + 0 comments `List<int> score = new List<int>( new int[]{0, 0}); int i =0; for(i=0; i<count(a);i++){ if(a[i]>b[i]){ score[0]+=1; }else if(a[i]==b[i]){ score[0]+=0; score[1]+=0; }else{ score[1]+=1; } } return score; } static int count(List<int> aList){ int counter = 0; foreach(var item in aList){ counter++; } return counter; }`

krissna224 + 1 comment not working

neel9010 + 0 comments static List<int> compareTriplets(List<int> a, List<int> b) { var a_point = a.Count(x => x > b[a.IndexOf(x)]); var b_point = b.Count(x => x > a[b.IndexOf(x)]); return new List<int> {a_point, b_point}; }

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.

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 };

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

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 ?

priyankashawpas + 0 comments yes

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);`

pradeepj3 + 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.

sergiu_lucec + 0 comments these hipster scripts are funny though

wlsc1 + 6 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); } }

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;

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.

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).

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.

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

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

komalgoyal + 0 comments [deleted]unique_here + 2 comments [deleted]pradeepj3 + 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

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.

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

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

rishabhstc + 0 comments this method is nice thanx

jacksoftd61 + 0 comments This is verysimple code and clean. Thanks for sharing

shreeshreya17 + 0 comments in this case only 6 integers are taken.how we will use it it is not a triplet and many integers are to be entered.can you please help

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

kibarabara + 0 comments Ternary operator.

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

An00nymous + 1 comment these are called ternary operators

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

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

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

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

an0o0nym + 9 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)

dcmurray + 0 comments [deleted]vishwask1 + 0 comments Perfect!

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

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

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)

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)))

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

ChandraLegend + 0 comments i think else 0 is not needed

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

vanmathiravi4 + 0 comments [deleted][deleted] + 0 comments itss not working

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"??

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

tummaaditya + 0 comments what to return back?

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

dhimandhruv + 0 comments what about checking limit 0

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

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; }

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

qeedy03 + 1 comment this is wrong, because solve method need return int [] value

why my code wrong? but then output is right

static int[] solve(int[] a, int[] b) {

`int pointAlice = 0; int pointBob = 0; for(int i=0; i < ((a.length<=b.length)?a.length:b.length);i++){ pointAlice += ((a[i]>b[i])?1:0); pointBob += ((a[i]>b[i])?0:1); } return new int[] {pointAlice,pointBob};`

}

stephen_campbel1 + 0 comments `static int[] solve(int[] a, int[] b) { int aliceWins = 0; int bobWins = 0; for (int i = 0; i <= 2; i++) { if (a[i]> b[i]) { aliceWins++; } else if (b[i] > a[i]) { bobWins++; } } return new int [] {aliceWins, bobWins};`

reggie_greenfel1 + 0 comments This does not scale in case more awarding points comes into play...

officialgoutham1 + 0 comments her asking return statement .how will we can give it

shivendrathehero + 0 comments [deleted]zubairhanif6060 + 0 comments this is not for genral use.....

chuucksc + 0 comments This is great you are saving the time of the loop, and do applies since it's restricted to 3 games

anthonyjzaro + 1 comment This is not working in Java for me

moazashraf007 + 0 comments Try this one or better go to this link https://www.hackerrank.com/challenges/compare-the-triplets/submissions/code/82365752

Kanahaiya + 0 comments 100 % working code with video explanation -- All Test Case Passed..!! Here is the video explanation of my solution using ternary operator-

and you can find most of the hackerrank solutions with video explanation here-

https://github.com/Java-aid/Hackerrank-Solutions

and many more needs to be added.

Any comments and feedback will be appreciated.

Regards,

Kanahaiya Gupta

Git Hub URL | https://github.com/Java-aid/

LIKE US | https://www.facebook.com/javaaid/

SUBSCRIBE US | https://www.youtube.com/c/JavaAidTutorials

TELEGRAM LINK| http://t.me/javaaid

senali_mu + 1 comment This solution is not really a good one

Kanahaiya + 0 comments Hi @Senali_mu, May i know why this solution is not good?

utkarshGG + 0 comments Wow python3 has much sexier solution for this one.

michellerodri247 + 0 comments The page is always providing som best information best shared hosting. The explanation of the problem and coding of the problem is explained well on the problem section. And it is a really interesting and understanding one for the common people.

palankit35 + 0 comments [deleted]sairamguttula3 + 0 comments Thanks mam

addison_ + 5 comments 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!

blonkm + 0 comments this is perfect. Anything shorter becomes less readable. Using just 1 special function (zip) which is quite familiar in Python. Also, scalable to more items, and uses clear variable names.

One liners are not for professional coders, but for script kiddies showing off their code-fu.

arthurazs + 1 comment Great solution, I'd remove the

`if`

s though:`for a, b in zip(alice, bob): score[0] += a > b score[1] += a < b`

jurgen_bergmann + 0 comments very nice, so simple, i like how you leverage the boolean return from the comparison

ste17ste + 0 comments Thanks, this is great! Makes sense and it helped me write this after I saw your example:

def compareTriplets(a, b): score = [0,0] for i in range(0,3): if a[i] > b[i]: score[0] += 1 elif a[i] < b[i]: score[1] += 1 return score

rex_code + 0 comments You could just take a, b as arrays like below:

def solve(a, b): score = [0, 0] for a_point,b_point in zip(a, b):

marcell_made + 6 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.

kyoko86 + 0 comments Same problem here!

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

sppower + 3 comments 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!

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

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

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

naan_ace + 3 comments I got a same error so I tried to return array. Then it fixed.

`var result = []; var aScore = 0, bScore = 0; for (var i = 0; i < a.length; i++) { if (a[i] > b[i]) aScore++; if (a[i] < b[i]) bScore++; } result[0] = aScore; result[1] = bScore; return result;`

saraebroad + 0 comments var a = [a0, a1, a2]; var b = [b0, b1, b2]; var ascore = 0; var bscore = 0;

function compareTriplets(a0, a1, a2, b0, b1, b2) { var resultsArray = [];

for (var i = 0; i < a.length; i ++) {

`if (a[i] > b[i]) { ascore++ } if (a[i] < b[i]) { bscore++ }`

} resultsArray[0] = ascore; resultsArray[1] = bscore; return (ascore + "" + bscore) }

uguryildiz + 0 comments Wow! It really helped dude. Thank you.

muhamedelkady94 + 0 comments i have same problem, returned array done thank you

KarenPerez + 0 comments You have built the best script from python which is every difficult programming. But still it's failing in the execution process, which is the saddest thing. I have been trying to write a papersowl reviews article on this code based upon your articles. And trying to make a good script to make own website. I hope it turns well.

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.

karthikshindee + 0 comments - // Complete the compareTriplets function below.
- function compareTriplets(a, b) {
- let res = [0,0];
- for (let i = 0; i < a.length; i++) {
- if (a[i]!== b[i]) {
- a[i] > b[i] ? res[0]++ : res[1]++;
- }
- }
- return res;
- }

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;`

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?

leonteng91 + 1 comment 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; }

AryamanS73 + 0 comments What does "b+c" do in your else if code block?

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

veerabahu98 + 0 comments ohh kkk

Rehmanali + 0 comments Awesome!

SyedGhouri + 2 comments 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)

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; }`

Rainchai4240 + 0 comments wouldnt that be better to use loop instead of repeat the code? Just a suggestion

Sort 2093 Discussions, By:

Please Login in order to post a comment