Logic – 2


makeBricks
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops.
makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true

public boolean makeBricks(int small, int big, int goal) {
       if (big >= goal / 5 && small >= goal % 5) {
           return true;
       } else if (goal - big * 5 <= small && goal - big * 5 > 0) {
           return true;
       }
       return false;
   }

loneSum
Given 3 int values, a b c, return 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 = 0;
       if (a != b && a != c) {
           sum += a;
       }
       if (b != c && b != a) {
           sum += b;
       }
       if (c != a && c != b) {
           sum += c;
       }
       return sum;
   }

luckySum
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
luckySum(1, 2, 3) → 6
luckySum(1, 2, 13) → 3
luckySum(1, 13, 3) → 1

public int luckySum(int a, int b, int c) {
       int sum = 0;
       if (a != 13) {
           sum += a;
       }
       if (a != 13 && b != 13) {
           sum += b;
       }
       if (a != 13 && b != 13 && c != 13) {
           sum += c;
       }
       return sum;
   }

noTeenSum
Given 3 int values, a b c, return their sum. However, if any of the values is a teen — in the range 13..19 inclusive — then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper “public int fixTeen(int n) {“that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. “decomposition”). Define the helper below and at the same indent level as the main noTeenSum().
noTeenSum(1, 2, 3) → 6
noTeenSum(2, 13, 1) → 3
noTeenSum(2, 1, 14) → 3

public int noTeenSum(int a, int b, int c) {
       int sum = 0;
       if (a < 13 || a > 19) {
           sum += a;
       }
       if (b < 13 || b > 19) {
           sum += b;
       }
       if (c < 13 || c > 19) {
           sum += c;
       }
       return sum + fixTeen(a) + fixTeen(b) + fixTeen(c);
   }

   public int fixTeen(int n) {
       if (n == 15 || n == 16) {
           return n;
       } else return 0;
   }

roundSum
For this problem, we’ll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper “public int round10(int num) {” and call it 3 times. Write the helper entirely below and at the same indent level as roundSum().
roundSum(16, 17, 18) → 60
roundSum(12, 13, 14) → 30
roundSum(6, 4, 4) → 10

public int roundSum(int a, int b, int c) {
       int sum = 0;
       sum += round10(a);
       sum += round10(b);
       sum += round10(c);
       return sum;
   }

   public int round10(int num) {
       if (num % 10 >= 5) {
           num += 10 - num % 10;
       }
       if (num % 10 < 5) {
           num -= num % 10;
       }
       return num;
   }

closeFar
Given three ints, a b c, return true if one of b or c is “close” (differing from a by at most 1), while the other is “far”, differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.
closeFar(1, 2, 10) → true
closeFar(1, 2, 3) → false
closeFar(4, 1, 3) → true

public boolean closeFar(int a, int b, int c) {
    if (Math.abs(a - b) <= 1 && Math.abs(c - b) >= 2 && Math.abs(a - c) >= 2) {
        return true;
    }
    if (Math.abs(a - c) <= 1 && Math.abs(b - a) >= 2 && Math.abs(b - c) >= 2) {
        return true;
    }
    return false;
}

blackjack
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

public int blackjack(int a, int b) {
    if(Math.max(a,b)  <= 21){
        return Math.max(a,b);
    }else if(Math.min(a,b) > 21){
        return 0;
    }
    return Math.min(a,b);
}

evenlySpaced
Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.
evenlySpaced(2, 4, 6) → true
evenlySpaced(4, 6, 2) → true
evenlySpaced(4, 6, 3) → false

public boolean evenlySpaced(int a, int b, int c) {
       if (a == b && b == c) {
           return true;
       }
       return max(a, b, c) - mid(a, b, c) == mid(a, b, c) - min(a, b, c);
   }

   public int max(int a, int b, int c) {
       int max = 0;
       if (a > b) {
           max = a;
       }
       if (b > a) {
           max = b;
       }
       if (c > a) {
           max = c;
       }
       return max;
   }

   public int min(int a, int b, int c) {
       int min = 0;
       if (Math.min(a, b) > c) {
           min = c;
       }
       if (Math.min(a, b) < c) {
           min = Math.min(a, b);
       }
       return min;
   }

   public int mid(int a, int b, int c) {
       int mid = 0;
       mid = (a + b + c) - (max(a, b, c) + min(a, b, c));
       return mid;
   }

makeChocolate
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can’t be done.

public int makeChocolate(int small, int big, int goal) {
       if (big >= goal / 5 && small >= goal % 5) {
           return goal % 5;
       }
       if (big <= goal / 5 && small >= goal - big * 5) {
           return goal - big * 5;
       }
       return -1;
   }

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *