More efficient solution to blackjack? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on the following problem from codingbat:
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
My solutions is:
public int blackjack(int a, int b) {
if (a>21){
if (b>21){
return 0;
}
return b;
}
else if(b>21) return a;
return Math.max(a,b);
}
Is there something in my logic that can be improved to make it more efficient? Am I doing something unnecessary?

This could be more efficient. At the very least it's another way of looking at the problem:
public int blackjack(int a, int b) {
if (a>21) a = 0;
if (b>21) b = 0;
if (a>b) {
return a;
else {
return b;
}
}

I wouldn't say this is more efficient, but I re-ordered some of the if statements and arrived at the code below. I think this is, at the very least, somewhat easier to follow:
public int blackjack(int a, int b) {
if (a <= 21 && b <= 21) return Math.max(a, b);
if (a <= 21) return a;
if (b <= 21) return b;
return 0;
}

Using ternary operator:
public int blackjack(int a, int b) {
a = a > 21 ? 0 : a;
b = b > 21 ? 0 : b;
return (a > b) ? a : b;
}

This might be pretty close;
public int blackjack(int a, int b) {
if(a > 21 && b > 21) return 0;
else if (a <= 21 && a > b || b > 21) return a;
return b;
}

Related

Determine whether a is a factor of b [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Given two integers, a and b, check to see whether a is a factor of b. If a is an integer of b, return true. If not, return false.
Inputs:
a and b - two integers, where a is a potential factor and b is an integer larger than or equal to a; a is going to always be less than or equal to b
Output:
boolean - true if a is a factor of b, false if not
Here's what I tried:
boolean factor( int a, int b ) {
int divider = a%b;
if (divider == 0)
return true;
else
return false;
}
You have your mod statement backwards if you want a to be the smaller number.
int divider = b%a;
To know if b can be divided by a (with 0 remainder):
boolean factor( int a, int b ) {
return b % a == 0;
}
public boolean isFactor(int potential, int number) {
return number%potential== 0;
}
This should solve your problem. You had the order in your modulus wrong.
Start with tests, then implement them. You want to test the 0 case.
#Test
public void test(){
assertFalse(factor(5,6));
assertTrue(factor(4,2));
assertFalse(factor(8,0));
assertTrue(factor(0,9));
}
public boolean factor(int a, int b){
if(b == 0){
return false;
}
return a % b == 0;
}

The implements of Integer numberOfTrailingZeros [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I found the The implements of Integer numberOfTrailingZeros method in java as follows
public static int numberOfTrailingZeros(int i) {
// HD, Figure 5-14
int y;
if (i == 0) return 32;
int n = 31;
y = i <<16; if (y != 0) { n = n -16; i = y; }
y = i << 8; if (y != 0) { n = n - 8; i = y; }
y = i << 4; if (y != 0) { n = n - 4; i = y; }
y = i << 2; if (y != 0) { n = n - 2; i = y; }
return n - ((i << 1) >>> 31);
}
It's take me a while to understand, here is my solution :
public static int numberOfTrailingZeros(int i) {
if (i == 0) return 32;
int num = 0;
while ((i & 1) == 0) {
i >>= 1;
num ++;
}
return num;
}
My question is what's the better solution? How can I come up with those implements such as in JDK Integer bitCount,highestOneBit,rotateLeft etc methods?
as I know the JDK's numberOfTrailingZeros use less + opeartor, and will be having higher performance when dealing with '0x70000000','0x60000000', and anything else?
What the java JDK is doing is sort of a binary search in the number to find the number of trailing zeros. It tries to shift the number left by powers of 2 to see if the shifted number becomes 0. If it doesn't, there are some set bits that are too far to the right, so the method keeps that shifted number, decrements the final count, and tries again with the next smallest power of 2.

Easier way to switch variables? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a simple method that goes like this:
public int gcd(int a, int b) {
while (a!=b) {
int q = b;
b = a%b;
a = q;
}
return a;
}
Is there an easier way to write a greatest common denominator? Particularly the three lines in the while loop, can they be simplified?
If you don't mind performance but just readability then choose a different approach:
public int gcd(int a, int b) {
BigInteger ba = BigInteger.valueOf(a);
BigInteger bb = BigInteger.valueOf(b);
return ba.gcd(bb).intValue();
}
Try this:
public static int gcd(int a, int b) {
int q = b;
b = a % b;
a = q;
return (a != b && a != 0 && b != 0) ? gcd(a, b) : a - b;
}

Simple blackjack

Here's the question: 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
What I have so far:
public int blackjack(int a, int b) {
if (a>21 && b>21){
return 0;
}
if (a<21 && b>21){
return a;
}
if (b<21 && a>21){
return b;
}
if (21-a < 21-b){
return a;
}
return b;
}
This question is from codingbat.com, and for all the tests that it shows, this code works, but when it finishes and displays "other tests", this code fails. I suppose there's a certain situation where this wouldn't work, but I can't think of it right now. Any thoughts?
public int blackjack(int a, int b) {
// if both a and b are outside the valid range
if (a > 21 && b > 21)
return 0;
// if a is within the valid range but b is not
if (a <= 21 && b > 21)
return a;
// if b is within the valid range but a is not
if (b <= 21 && a > 21)
return b;
// if both a and be are within the valid range
return (a-b >= 0) ? a : b;
// Alternative: return Math.max(a, b); ---as per SimonT in the comment
}
So I guess your issue is that you didn't include 21 in your conditions.
If a=21, b=22, then it will return b which is not correct.
You forgot to specify the = operation in your condition. Change 2nd and 3rd condition to :
if (a<=21 && b>21){
return a;
}
if (b<=21 && a>21){
return b;
}

How to get Nth Power of any number [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I faced an interview. where I was asked the following question
Write a function in any of programming language that computes the nth power of a number w/o using + * or ^ or declaring a new variable inside the function or using any library function (eg Math lib in java).
I have used pow function of java Math.pow(a, b)
Thanks
They're asking whether you understand recursion. Considering x ^ k for some integer k,
when k < 0, xk = xk+1 / x
when k = 0, xk = 1
when k > 0, xk = xk-1 * x
Turning this into code shouldn't be too bad. Let's use multiplication for now, and take it out later.
double recursivePower(double x, int k) {
if (k < 0) {
return power(x, ++k) / x;
} else if (k == 0) {
return 1;
} else {
return power(x, --k) * x;
}
}
Now, to get rid of the multiplication. Since n * m = n / (1/m), we can rewrite the last calculation as power(x, --k) / (1/x):
double recursivePower(double x, int k) {
if (k < 0) {
return recursivePower(x, ++k) / x;
} else if (k == 0) {
return 1;
} else {
return recursivePower(x, --k) / (1 / x);
}
}
Fractional exponents could probably be done in the same style. If they want irrational exponents to be handled in the same way, I'd ask for Google and a fair amount of time to think about the problem.
static public int power(int value, int pow){
if(pow == 0) return 1;
return value * power(value, pow -1);
}
Done in JavaScript:
function power(num,pow){
if (pow == 0) return 1
num /= 1/(power(num,--pow))
return num
}
Call it like:
power(2,0) // -> 1
power(5,2) // -> 25
power(7,3) // -> 343
I feel like inverse division is cheating the no * operator rule, but eh, maybe that's what they were looking for.
I am using java programming language. The interviewer restricted you to declare a new variable inside the method better you pass it to the function. The interviewer didnt restrict you to use division operator (/) so you can use that.
static double getNthPowerOfNumber(double originalNumber,
int power) {
if (power == 0) {
return 1;
}
if (originalNumber == 0) {
return 0;
} else {
originalNumber/=1/getNthPowerOfNumber(originalNumber, --power);
return originalNumber;
}
}
if you want to get 5th power of a number 3 then write System.out.println("4..double..." + getNthPowerOfNumber(4, 1));

Categories