Problem:
Given 2 int values greater than 0, return whichever value is nearest to 21 without going over. Return 0 if they both go over.
blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19
Solution:
public int blackjack(int a, int b) { if (a > 21 && b > 21) { return 0; }else if (a > 21) { return b; } else if (b > 21) { return a; } int sumA = 21 - a; int sumB = 21 - b; if (sumA > sumB) { return b; } else { return a; } }
if(a>21 && b>21) return 0;
ReplyDeletereturn (a>21)? b : (b>21)? a : Math.max(a,b);
@Kico
DeleteGood solution. I didn't work those small tasks which are good for logic and thinking and now i have very bad and big solutions. You are in good form ;)
Nice solution
DeleteCan somebody explain how this works
ReplyDeletereturn (a>21)? b : (b>21)? a : Math.max(a,b);
?
if (a > 21) --> b
Deleteif (a <= 21 && b > 21) --> a
if (a <= 21 && b =< 21) --> Math.max(a,b)
return (a>21)? b : (b>21)? a : Math.max(a,b);
Deletehow do u write this in python?
im doing all java tasks in python cuz ive done all python tasks
if (a > 21 && b > 21)
ReplyDeletereturn 0;
if (Math.abs(21 - a) < Math.abs(21 -b) && a <= 21 || b > 21)
return a;
return b;
public int blackjack(int a, int b) {
ReplyDelete/* if (a > 21 && b > 21) {return 0;}
if (a > 21 || b==21) {return b;}
if (b > 21 || a==21) {return a;}
return (a < 21 && b < 21 && a > b)?a:b;
*/
//optimize
if (a > 21 && b > 21) {return 0;}
return (a>21)?b:(b>21)?a:Math.max(a,b);
}
public int blackjack(int a, int b) {
ReplyDelete/* if (a > 21 && b > 21) {return 0;}
if (a > 21 || b==21) {return b;}
if (b > 21 || a==21) {return a;}
return (a < 21 && b < 21 && a > b)?a:b;
*/
//optimize
if (a > 21 && b > 21) {return 0;}
return (a>21)?b:(b>21)?a:Math.max(a,b);
//explaining return (a>21)?b:(b>21)?a:Math.max(a,b);
// is '(a>21) ?' if yes than 'b' else':' check '(b>21)?' if yes than 'a' else':' get max of (a,b) using math.max function.
}
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. 넷마블 먹튀
ReplyDeleteThe most effective means bring in some form of e-book and enjoy this almost from anywhere is during the entire fascinating process involving mp 3 mp3 audio books. The true sound e-book outlets that always one thinks of as rapidly as. บาคาร่า
ReplyDeleteAre the Odds against you in this Online Casino Game? To read the latest game reviews Visit bintang9.com/bm/nova88-maxbet.aspx Unarguably Internet is the buzz word today, be it shopping, searching for information, or casino gambling.
ReplyDeleteA debt of gratitude is in order for offering this quality data to us. I truly delighted in perusing. Will without a doubt going to impart this URL to my companions. dg casino
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteAlso, the player has so many other options to take advantage of. The player can Double Down, Split Pairs, Take Insurance, and even Surrender a bad hand in some casinos. dig this
ReplyDeletepublic int blackjack (int a, int b) {
ReplyDeleteif(rtr(a)==0&&rtr(b)==0)
return 0;
else if(21-rtr(a) <21-rtr(b))
return a;
else
return b;
}
public int rtr(int a)
if(a>21)
return 0;
else return a;
}
public int blackjack(int a, int b) {
ReplyDeleteif (a > 21 && b > 21){
return 0;
}
if (a <= 21){
if (b > 21){
return a;
}
if (a > b){
return a;
}
}
return b;
}
dapatkan keuntungan ratusan juta setiap hari dengan bergabung bersama situs resmi terpercaya SARANAPELANGI
ReplyDeleteJust pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on. casino relevant Backlinks
ReplyDeleteif (a<=21 && b<=21) {
ReplyDeleteif (a < b) {
return b;
}
if (a>b){
return a;
}
}
if (a>21 && b<=21){
return b;
}
if (a<=21 && b>21){
return a;
}
else
return 0;
I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. สล็อตjokerฟรีเครดิต
ReplyDeletereturn a >21 && b> 21 ? 0 : a >= b && a<=21 || b > 21 ? a : b;
ReplyDelete