CodingBat : LoneSum optimization - java

I have completed this exercise on codingbat successfully, but I want to know is there a better/faster or more elegant solution?
Given 3 int values, a b c, returns their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.
loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0
public int loneSum(int a, int b, int c)
{
int sum = a + b + c;
if ( a == b) { sum = sum - a - b; }
if ( a == c) { sum = sum - a - c; }
if( b == c) { sum = sum - b - c; }
if (( a == b ) && (b==c)) { sum = 0; }
return sum;
}

Maybe this code?
public static int loneSum(int a, int b, int c) {
if (a == b && b == c) {
a = 0;
b = 0;
c = 0;
}
if (a == b) {
a = 0;
b = 0;
}
if (a == c) {
a = 0;
c = 0;
}
if (b == c) {
b = 0;
c = 0;
}
return a + b + c;
}

You can measure the time ... this seems like less sub ops so might be faster
public int loneSum(int a, int b, int c) {
int sum = 0;
boolean ab = a != b;
boolean ac = a != c;
boolean bc = b != c;
if(ab && ac) sum += a;
if(ab && bc) sum += b;
if(bc && ac) sum += c;
return sum;
}

public int loneSum(int a, int b, int c) {
if ( (a==b) && (b==c) && (c==a) ) return 0;
else
{
if (a==b) return c ;
if (b==c) return a;
if (a==c) return b;
else return a+b+c;
}
}

public int loneSum(int a, int b, int c) {
int sum = a+b+c;
if (a==b) sum -= a*2;
if (b==c) sum -= b*2;
if (c==a) sum -= c*2;
if (sum<0) sum=0;
return sum;
}
This take 4 comparisons.
Initialize sum to a+b+c
Compare numbers to each other and if same, decrease sum by num*2.
If multiple IF's are hit (meaning a==b==c), the sum becomes 0. Return 0 in that case.

if (a==b && b==c) return 0;
if (a==b) return c;
if (a==c) return b;
if (b==c) return a;
return a+b+c;

def lone_sum(a, b, c):
sum=0
if a==b==c:
sum=0
elif a==b:
sum=c
elif a==c:
sum=b
elif b==c:
sum=a
else:
sum=a+b+c
return sum
This is for python
public int loneSum(int a, int b, int c)
{
int sum = a + b + c;
if ( a == b) { sum = c; }
if ( a == c) { sum = b; }
if( b == c) { sum = a; }
if (( a == b ) && (b==c)) { sum = 0; }
return sum;
}
This is for java

I sending you another approach using ternary operators. I hope it can be useful in your exercises and learning process.
public int noTeenSum(int a, int b, int c) {
a = (a >= 13 && a <= 19) ? (a == 15 || a == 16) ? a : 0 : a;
b = (b >= 13 && b <= 19) ? (b == 15 || b == 16) ? b : 0 : b;
c = (c >= 13 && c <= 19) ? (c == 15 || c == 16) ? c : 0 : c;
return a+b+c;
}

I think this is easier to read
def lone_sum(a, b, c):
if a == b == c:
return 0
if a == b:
return c
if b == c:
return a
if a == c:
return b
else:
return a + b + c

Here's my answer:
def lone_sum(a, b, c):
if a!=b and b!=c and c!=a:
return a+b+c
elif a==b==c:
return 0
elif a==b:
return c
elif b==c:
return a
elif c==a:
return b

public int loneSum(int a, int b, int c) {
int sum = a+b+c;
if(a==b)
sum -= (a+b);
if(a==c)
sum -= (a + c);
if(b==c)
sum -= (b+c);
return sum > 0? sum: 0;
}

def lone_sum(a, b, c):
tempSum, arr2 = a+b+c, list(set([a,b,c]))
if (a==b==c): return 0
return sum(arr2) - (tempSum - sum(arr2))

Please see the below function :
def su(a,b,c):
total=0
for x in (a,b,c):
if x==(x+1):
y=0
else:
y=1
total+=x*y
return total
print(su(3,2,3))

Related

How to rewrite my code so that I dont repeat myself, i.e. as in the DRY principle

if (c < n) {
while (c != n) {
p *= b;
c++;
}
}else if (c > n){
while (c != n) {
p *= b;
c--;
}
p = 1.0/p;
}
I think boolean might help but I have no idea how to implement it.
A boolean would work, or you can set an int adjuster for c and kill two birds with one stone:
if (c != n) {
int x = c < n ? 1 : -1;
while (c != n) {
p *= b;
c += x;
}
if (x == -1) {
p = 1.0/p;
}
}

Euler Brick imlementation in Java

Hello to all who are taking the time out of their day to attempt and help solve my problem. I am truly stuck for the first time in a while and would really appriciate some help. But enough exposition; here is the what I have implemented thus far:
public class EulerBrick {
private int a, b, c;
private int d, e, f;
private Random random;
public EulerBrick(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
this.d = (int) Math.sqrt((a * a) + (b * b));
this.e = (int) Math.sqrt((a * a) + (c * c));
this.f = (int) Math.sqrt((b * b) + (c * c));
this.random = new Random();
if (verify(a, b, c, random.nextInt())) {
if (a % 3 != 0) {
if (b % 3 != 0) return;
if (c % 3 != 0) return;
}
if (a % 4 != 0) {
if (b % 4 != 0) return;
if (c % 4 != 0) return;
}
if (a % 11 != 0) {
if (b % 11 != 0) {
if (c % 11 != 0) return;
}
}
} else return;
printDimensions();
}
public boolean verify(double a, double b, double c, int k) {
a *= k;
b *= k;
c *= k;
if (a % 3 != 0) {
if (b % 3 != 0) return false;
if (c % 3 != 0) return false;
}
if (a % 4 != 0) {
if (b % 4 != 0) return false;
if (c % 4 != 0) return false;
}
if (a % 11 != 0) {
if (b % 11 != 0) {
if (c % 11 != 0) return false;
}
}
return true;
}
public void printDimensions() {
System.out.println("Side A: " + a + ", Side B: " + b + ", Side C: " + c + ", Side D: " + d + ", Side E: " + e + ", Side F: " + f);
}
}
And here is the main class I am using to test it:
public class Main {
public static void main(String[] args) throws InterruptedException {
int i = 0, j = 0, k = 0;
while (true) {
new EulerBrick(i += 3, j += 4, k += 11);
Thread.sleep(1000);
}
// EulerBrick brick = new EulerBrick(44, 117, 240);
}
}
Again any help would be greatly appreciated. Also this isn't for a course/class just an exercise that Ive stumped myself on.

Divide 2 numbers without using / ,* or %

Let's say we assume a=16 b=3
First I am trying to find the x to which when I multiply 3 and then subtract is from 16 will get the min difference
16-3<<0 =>16-(3*1) -- 16-3<<1 =>16-(3*2) -- 16-3<<2 =>16-(3*4) -- 16-3<<3 =>16-(3*8)
at x=3 while loop will fail
res = 4 and a will become 4
Now again while loop will start
4-3<<0 =>4-(3*1) -- 4-3<<1 =>4-(3*2)
at x=1 while loop will fail
res = 4+1
Please help me with the complexity as my time limit is exceeding
public class Solution {
public int divide(int dividend, int divisor) {
if(dividend == Integer.MIN_VALUE && divisor == -1){
return Integer.MAX_VALUE;
}
int a = Math.abs(dividend);
int b = Math.abs(divisor);
int res = 0;
while(a - b >= 0){
int x = 0;
while( (a - (b << x)) >= 0){
x++;
}
res += 1 << (x-1);
a -= b << (x-1);
// System.out.println(res+" "+x+" "+a);
}
return (dividend >= 0) == (divisor >= 0) ? res :-res;
}
}
One way to simplify the program would be to search for x only once and then reduce x by 1 every loop:
public int divide(int dividend, int divisor) {
if(dividend == Integer.MIN_VALUE && divisor == -1){
return Integer.MAX_VALUE;
}
int a = Math.abs(dividend);
int b = Math.abs(divisor);
int res = 0;
int x = 0;
while(a - (b << x) >= 0) {
x++;
}
x--;
while(a >= b) {
int c = a - (b << x);
if(c >= 0) {
res += 1 << x;
a = c;
}
x--;
}
return (dividend >= 0) == (divisor >= 0) ? res :-res;
}

Trying to figure out Class and method declaration in Util classes java

I've been having some issues trying to solve this code that my professor sent to me for studying purposes. I usually can solve the isPrime and getPrime no problem but my issue lies with the gcd class. Would love to get some input on this. Thanks :) Sorry for the formatting since i'm new to the site
import java.util.*;
public class Util3
{
public ??? getSmaller(???)
{
int smaller;
if (???)
smaller = ???;
else
smaller = ???;
return smaller;
}
public ??? gcd(??? a, ??? b)
{
int g, smaller;
smaller = ???(a, b);
g = smaller;
for (int i = ???; i >= 1; i++) { // from smaller to 1
if (a % i == 0 ??? ???) {
g = i;
???;
}
}
return g;
}
public ??? isPrime(int p)
{
if (p < 2)
return false;
int i;
for (i = ???; i <= ???; i++) // from 2 to p-1
if (??? == 0) // if p is divisible by i
break;
if (i == ???)
return ???;
else
return ???;
}
public ??? getPrime(int n)
{
boolean b;
int p;
for (p = n+1; ; p++) { // n+1, n+2, ...
b = ???(p); // check if p is a prime number
if (???)
break;
}
return p;
}
}
You can use a naive solution:
public static int gcd(int a, int b) {
int current_gcd = 1;
for(int d = 2; d <= a && d <= b; ++d) {
if (a % d == 0 && b % d == 0) {
if (d > current_gcd) {
current_gcd = d;
}
}
}
return current_gcd;
}
Or a recursive one:
public static int GCD(int a, int b) {
if (b == 0) {
return a;
}
}
return GCD(b, a % b);
}

How to find the GCD of three numbers within a single method

I've got to ensure that the GCD between 3 numbers is no greater than 1.
Here's the code I have so far for the method:
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
the return 1 was already there when I started working on the lab. How can I make sure that the GCD is no more than 1? And return all three integers?
Here's the remainder of the code if it helps in figuring out what needs to be done:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
public String toString()
{
String output="";
int max = number;
for(a = 1; a <= max; a++)
{
for(b = a +1; b <= max; b++)
{
for(c = b + 1; c <= max; c++)
{
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
}
}
}
}
return output+"\n";
}
}
UPDATE
Here is my new coding for the same lab:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
{
if(a%2<=1 && b%2<=1 && c%2<=1)
{
return 1;
}
}
}
}
}
}
}
return 1;
}
public String toString()
{
String output="";
output = greatestCommonFactor(a, b, c);
return output+"\n";
}
}
You can use Euclid's algorithm to calculate the GCD of a and b. Call the result d. Then the GCD of a, b, and c is the GCD of c and d; for that, you can use Euclid's algorithm again.
Here's a brute-force way if you don't care about efficiency:
private int greatestCommonFactor(int a, int b, int c)
{
limit = Math.min(a, b);
limit = Math.min(limit, c);
for(int n = limit; n >= 2; n--)
{
if ( (a % n == 0) && (b % n == 0) && (c % n == 0) ) {
return n;
}
}
return 1;
}
Explanation:
You can save some work by only checking up to the minimum of (a, b, c). Any number greater than that definitely won't be a GCD of all 3.
You need to start your loop at n = limit instead of n = 0 and count backwards.
As soon as we come across a number that produces zero remainder for (a, b, c), that must be the GCD.
If nothing is found within the loop, GCD defaults to 1.

Categories

Resources