Java > Logic-1 > redTicket (CodingBat Solution)

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


12 comments :

  1. I did it similarly, but if theyre all 2, theyre also all equal:

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

    ReplyDelete
  2. a bit shorter lol

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

    ReplyDelete
  3. best solution it:

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

    ReplyDelete
  4. best solution it:

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

    ReplyDelete
    Replies
    1. 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.

      Delete
  5. private final int redTicket(int a, int b, int c) {
    if (a == b && b == c) {
    if (a == 2){
    return 10;
    }
    return 5;
    }
    return (a != b && a != c)? 1: 0;
    }

    ReplyDelete
  6. Hi can anybody do this using Python? Thanks a lot!

    ReplyDelete
  7. if((a==2) && (b==2) && (c==2))
    return 10;
    if((a==b) && (a==c) && (b==c))
    return 5;
    if((a!=b) && (a!=c))
    return 1;
    else return 0;

    ReplyDelete
  8. if(a==b && b==c){
    if(a==2) return 10;
    return 5;
    }else if(a!=b&&a!=c) return 1;
    else return 0;

    ReplyDelete
  9. public int redTicket(int a, int b, int c) {
    if (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;
    }

    ReplyDelete
  10. public int redTicket(int a, int b, int c) {
    if(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;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact