Problem:
You have a red lottery ticket showing ints a, b, and c, each of which is 0, 1, or 2. If they are all the value 2, the result is 10. Otherwise if they are all the same, the result is 5. Otherwise so long as both b and c are different from a, the result is 1. Otherwise the result is 0.
redTicket(2, 2, 2) → 10
redTicket(2, 2, 1) → 0
redTicket(0, 0, 0) → 5
Solution:
public int redTicket(int a, int b, int c) { if (a == 2 && b == 2 && c == 2) return 10; if ( a == b && b == c) return 5; if ( a != b && a != c) return 1; else return 0; }
I did it similarly, but if theyre all 2, theyre also all equal:
ReplyDeletepublic int redTicket(int a, int b, int c) {
if(a==b&&b==c)
{
if(a==2) return 10;
else return 5;
}
else if(b!=a&&c!=a) return 1;
else return 0;
}
a bit shorter lol
ReplyDeletepublic int redTicket(int a, int b, int c) {
return (a == 2 && b == 2 && c == 2) ? 10 : (a == b && b == c) ? 5 : (b != a && c != a) ? 1 : 0;
}
best solution it:
ReplyDeleteif(a==2 && b==2 && c==2) {
return 10;
}
else if(a==b && a==c) {
return 5;
}
else if(a!=b && a!=c) {
return 1;
}
else {
return 0;
}
best solution it:
ReplyDeleteif(a==2 && b==2 && c==2) {
return 10;
}
else if(a==b && a==c) {
return 5;
}
else if(a!=b && a!=c) {
return 1;
}
else {
return 0;
}
You don't need the "else"s in front of the "if"s when you have return statements within them because those exit the method.
Deleteprivate final int redTicket(int a, int b, int c) {
ReplyDeleteif (a == b && b == c) {
if (a == 2){
return 10;
}
return 5;
}
return (a != b && a != c)? 1: 0;
}
Hi can anybody do this using Python? Thanks a lot!
ReplyDeleteif((a==2) && (b==2) && (c==2))
ReplyDeletereturn 10;
if((a==b) && (a==c) && (b==c))
return 5;
if((a!=b) && (a!=c))
return 1;
else return 0;
if(a==b && b==c){
ReplyDeleteif(a==2) return 10;
return 5;
}else if(a!=b&&a!=c) return 1;
else return 0;
Cool👍
Deletepublic int redTicket(int a, int b, int c) {
ReplyDeleteif (a==2 && b==2 && c==2)
return 10;
else if (a==b && b==c && c==a)
return 5;
else if (a !=b && a != c)
return 1;
else
return 0;
}
public int redTicket(int a, int b, int c) {
ReplyDeleteif(a==2 && b == 2 && c==2)
return 10;
else if(a == b && b == c)
return 5;
else if(b !=a && c!= a)
return 1;
else return 0;
}