|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package coding.bat.solutions; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Aman Shekhar |
| 8 | + * |
| 9 | + */ |
| 10 | +public class LogicTwo { |
| 11 | + |
| 12 | + /** |
| 13 | + * @param args |
| 14 | + */ |
| 15 | + public static void main(String[] args) { |
| 16 | + // TODO Auto-generated method stub |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + // -------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | + // We want to make a row of bricks that is goal inches long. We have a number of |
| 23 | + // small bricks (1 inch each) and big bricks (5 inches each). Return true if it |
| 24 | + // is possible to make the goal by choosing from the given bricks. This is a |
| 25 | + // little harder than it looks and can be done without any loops. See also: |
| 26 | + // Introduction to MakeBricks |
| 27 | + // |
| 28 | + // |
| 29 | + // makeBricks(3, 1, 8) → true |
| 30 | + // makeBricks(3, 1, 9) → false |
| 31 | + // makeBricks(3, 2, 10) → true |
| 32 | + |
| 33 | + public boolean makeBricks(int small, int big, int goal) { |
| 34 | + return goal - big * 5 <= small && goal % 5 <= small; |
| 35 | + } |
| 36 | + |
| 37 | + // -------------------------------------------------------------------------------------------- |
| 38 | + |
| 39 | + // Given 3 int values, a b c, return their sum. However, if one of the values is |
| 40 | + // the same as another of the values, it does not count towards the sum. |
| 41 | + // |
| 42 | + // |
| 43 | + // loneSum(1, 2, 3) → 6 |
| 44 | + // loneSum(3, 2, 3) → 2 |
| 45 | + // loneSum(3, 3, 3) → 0 |
| 46 | + // |
| 47 | + public int loneSum(int a, int b, int c) { |
| 48 | + |
| 49 | + if ((a == b) && (b == c)) |
| 50 | + return 0; |
| 51 | + else if ((a == c)) |
| 52 | + return b; |
| 53 | + else if ((a == b)) |
| 54 | + return c; |
| 55 | + else if ((b == c)) |
| 56 | + return a; |
| 57 | + return a + b + c; |
| 58 | + } |
| 59 | + |
| 60 | + // -------------------------------------------------------------------------------------------- |
| 61 | + |
| 62 | + // Given 3 int values, a b c, return their sum. However, if one of the values is |
| 63 | + // 13 then it does not count towards the sum and values to its right do not |
| 64 | + // count. So for example, if b is 13, then both b and c do not count. |
| 65 | + // |
| 66 | + // |
| 67 | + // luckySum(1, 2, 3) → 6 |
| 68 | + // luckySum(1, 2, 13) → 3 |
| 69 | + // luckySum(1, 13, 3) → 1 |
| 70 | + |
| 71 | + public int luckySum(int a, int b, int c) { |
| 72 | + if (a == 13) |
| 73 | + return 0; |
| 74 | + if (b == 13) |
| 75 | + return a; |
| 76 | + if (c == 13) |
| 77 | + return a + b; |
| 78 | + return a + b + c; |
| 79 | + } |
| 80 | + |
| 81 | + // -------------------------------------------------------------------------------------------- |
| 82 | + |
| 83 | + // Given 3 int values, a b c, return their sum. However, if any of the values is |
| 84 | + // a teen -- in the range 13..19 inclusive -- then that value counts as 0, |
| 85 | + // except 15 and 16 do not count as a teens. Write a separate helper "public int |
| 86 | + // fixTeen(int n) {"that takes in an int value and returns that value fixed for |
| 87 | + // the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. |
| 88 | + // "decomposition"). Define the helper below and at the same indent level as the |
| 89 | + // main noTeenSum(). |
| 90 | + // |
| 91 | + // |
| 92 | + // noTeenSum(1, 2, 3) → 6 |
| 93 | + // noTeenSum(2, 13, 1) → 3 |
| 94 | + // noTeenSum(2, 1, 14) → 3 |
| 95 | + |
| 96 | + public int noTeenSum(int a, int b, int c) { |
| 97 | + int aT = ((a >= 13 && a < 15 || a > 16 && a <= 19) ? 0 : a); |
| 98 | + int bT = ((b >= 13 && b < 15 || b > 16 && b <= 19) ? 0 : b); |
| 99 | + int cT = ((c >= 13 && c < 15 || c > 16 && c <= 19) ? 0 : c); |
| 100 | + return aT + bT + cT; |
| 101 | + } |
| 102 | + |
| 103 | + // -------------------------------------------------------------------------------------------- |
| 104 | + |
| 105 | + // For this problem, we'll round an int value up to the next multiple of 10 if |
| 106 | + // its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round |
| 107 | + // down to the previous multiple of 10 if its rightmost digit is less than 5, so |
| 108 | + // 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded |
| 109 | + // values. To avoid code repetition, write a separate helper "public int |
| 110 | + // round10(int num) {" and call it 3 times. Write the helper entirely below and |
| 111 | + // at the same indent level as roundSum(). |
| 112 | + // |
| 113 | + // |
| 114 | + // roundSum(16, 17, 18) → 60 |
| 115 | + // roundSum(12, 13, 14) → 30 |
| 116 | + // roundSum(6, 4, 4) → 10 |
| 117 | + |
| 118 | + public int roundSum(int a, int b, int c) { |
| 119 | + int aT = (a % 10 < 5) ? (a / 10 * 10) : (a / 10 * 10 + 10); |
| 120 | + int bT = (b % 10 < 5) ? (b / 10 * 10) : (b / 10 * 10 + 10); |
| 121 | + int cT = (c % 10 < 5) ? (c / 10 * 10) : (c / 10 * 10 + 10); |
| 122 | + return aT + bT + cT; |
| 123 | + } |
| 124 | + |
| 125 | + // -------------------------------------------------------------------------------------------- |
| 126 | + |
| 127 | + // Given three ints, a b c, return true if one of b or c is "close" (differing |
| 128 | + // from a by at most 1), while the other is "far", differing from both other |
| 129 | + // values by 2 or more. Note: Math.abs(num) computes the absolute value of a |
| 130 | + // number. |
| 131 | + // |
| 132 | + // |
| 133 | + // closeFar(1, 2, 10) → true |
| 134 | + // closeFar(1, 2, 3) → false |
| 135 | + // closeFar(4, 1, 3) → true |
| 136 | + |
| 137 | + public boolean closeFar(int a, int b, int c) { |
| 138 | + return Math.abs(b - c) >= 2 |
| 139 | + && (Math.abs(a - b) <= 1 && Math.abs(a - c) >= 2 || Math.abs(a - c) <= 1 && Math.abs(a - b) >= 2); |
| 140 | + } |
| 141 | + |
| 142 | + // Given 2 int values greater than 0, return whichever value is nearest to 21 |
| 143 | + // without going over. Return 0 if they both go over. |
| 144 | + // |
| 145 | + // |
| 146 | + // blackjack(19, 21) → 21 |
| 147 | + // blackjack(21, 19) → 21 |
| 148 | + // blackjack(19, 22) → 19 |
| 149 | + // |
| 150 | + |
| 151 | + // -------------------------------------------------------------------------------------------- |
| 152 | + |
| 153 | + public int blackjack(int a, int b) { |
| 154 | + int aUpdated = Math.abs(21 - a); |
| 155 | + int bUpdated = Math.abs(21 - b); |
| 156 | + if (a > 21 && b > 21) |
| 157 | + return 0; |
| 158 | + if (a > 21) |
| 159 | + return b; |
| 160 | + if (b > 21) |
| 161 | + return a; |
| 162 | + if (aUpdated > bUpdated) |
| 163 | + return b; |
| 164 | + return a; |
| 165 | + } |
| 166 | + |
| 167 | + // -------------------------------------------------------------------------------------------- |
| 168 | + |
| 169 | + // Given three ints, a b c, one of them is small, one is medium and one is |
| 170 | + // large. Return true if the three values are evenly spaced, so the difference |
| 171 | + // between small and medium is the same as the difference between medium and |
| 172 | + // large. |
| 173 | + // |
| 174 | + // |
| 175 | + // evenlySpaced(2, 4, 6) → true |
| 176 | + // evenlySpaced(4, 6, 2) → true |
| 177 | + // evenlySpaced(4, 6, 3) → false |
| 178 | + |
| 179 | + public boolean evenlySpaced(int a, int b, int c) { |
| 180 | + int min = Math.min(Math.min(a, b), c); |
| 181 | + int mid = Math.max(Math.min(a, b), c); |
| 182 | + int mid2 = Math.min(Math.max(a, b), c); |
| 183 | + int max = Math.max(Math.max(a, b), c); |
| 184 | + return Math.abs(mid - min) == Math.abs(mid - max) || Math.abs(mid2 - min) == Math.abs(mid2 - max); |
| 185 | + } |
| 186 | + |
| 187 | + // -------------------------------------------------------------------------------------------- |
| 188 | + |
| 189 | + // We want make a package of goal kilos of chocolate. We have small bars (1 kilo |
| 190 | + // each) and big bars (5 kilos each). Return the number of small bars to use, |
| 191 | + // assuming we always use big bars before small bars. Return -1 if it can't be |
| 192 | + // done. |
| 193 | + // |
| 194 | + // |
| 195 | + // makeChocolate(4, 1, 9) → 4 |
| 196 | + // makeChocolate(4, 1, 10) → -1 |
| 197 | + // makeChocolate(4, 1, 7) → 2 |
| 198 | + // |
| 199 | + |
| 200 | + public int makeChocolate(int small, int big, int goal) { |
| 201 | + int maxBig = goal / 5; |
| 202 | + if (big > maxBig) |
| 203 | + return (goal <= 5 * maxBig + small) ? (goal - 5 * maxBig) : -1; |
| 204 | + return (goal <= 5 * big + small) ? (goal - 5 * big) : -1; |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments