Skip to content

Commit a4463e9

Browse files
author
Alfredo Miranda
committed
Added Logic-2 problems
1 parent 04c9c71 commit a4463e9

File tree

9 files changed

+155
-0
lines changed

9 files changed

+155
-0
lines changed

java/logic-2/blackjack.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Given 2 int values greater than 0, return whichever value is nearest to 21
2+
* without going over. Return 0 if they both go over.
3+
*/
4+
public int blackjack(int a, int b) {
5+
if(a > 21 && b > 21)
6+
return 0;
7+
8+
if(a > 21)
9+
return b;
10+
11+
if(b > 21)
12+
return a;
13+
14+
return a > b ? a : b;
15+
}

java/logic-2/closeFar.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Given three ints, a b c, return true if one of b or c is "close"
2+
* (differing from a by at most 1), while the other is "far", differing from
3+
* both other values by 2 or more. Note: Math.abs(num) computes the absolute
4+
* value of a number.
5+
*/
6+
public boolean closeFar(int a, int b, int c) {
7+
return (isClose(a, b) && isFar(a, b, c)) ||
8+
(isClose(a, c) && isFar(a, c, b));
9+
}
10+
11+
public boolean isClose(int a, int b) {
12+
return Math.abs(a - b) <= 1;
13+
}
14+
15+
public boolean isFar(int a, int b, int c) {
16+
return Math.abs(a - c) >= 2 && Math.abs(b - c) >= 2;
17+
}

java/logic-2/evenlySpaced.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Given three ints, a b c, one of them is small, one is medium and one is
2+
* large. Return true if the three values are evenly spaced, so the
3+
* difference between small and medium is the same as the difference between
4+
* medium and large.
5+
*/
6+
public boolean evenlySpaced(int a, int b, int c) {
7+
int temp;
8+
if(a > b) {
9+
temp = a;
10+
a = b;
11+
b = temp;
12+
}
13+
14+
if(b > c) {
15+
temp = b;
16+
b = c;
17+
c = temp;
18+
}
19+
20+
if(a > b) {
21+
temp = a;
22+
a = b;
23+
b = temp;
24+
}
25+
26+
return b - a == c - b;
27+
}

java/logic-2/loneSum.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Given 3 int values, a b c, return their sum. However, if one of the values
2+
* is the same as another of the values, it does not count towards the sum.
3+
*/
4+
public int loneSum(int a, int b, int c) {
5+
int sum = 0;
6+
7+
if(a != b && a != c)
8+
sum += a;
9+
10+
if(b != a && b != c)
11+
sum += b;
12+
13+
if(c != a && c != b)
14+
sum += c;
15+
16+
return sum;
17+
}

java/logic-2/luckySum.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Given 3 int values, a b c, return their sum. However, if one of the values
2+
* is 13 then it does not count towards the sum and values to its right do
3+
* not count. So for example, if b is 13, then both b and c do not count.
4+
*/
5+
public int luckySum(int a, int b, int c) {
6+
int sum = 0;
7+
8+
if(a != 13) {
9+
sum += a;
10+
if(b != 13) {
11+
sum += b;
12+
if(c != 13) {
13+
sum += c;
14+
}
15+
}
16+
}
17+
18+
return sum;
19+
}

java/logic-2/makeBricks.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* We want to make a row of bricks that is goal inches long. We have a number
2+
* of small bricks (1 inch each) and big bricks (5 inches each). Return true
3+
* if it is possible to make the goal by choosing from the given bricks.
4+
*/
5+
public boolean makeBricks(int small, int big, int goal) {
6+
int count = 5 * big > goal ? goal - (goal % 5) : 5 * big;
7+
8+
return (goal - count) <= small;
9+
}

java/logic-2/makeChocolate.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* We want make a package of goal kilos of chocolate. We have small bars
2+
* (1 kilo each) and big bars (5 kilos each). Return the number of small bars
3+
* to use, assuming we always use big bars before small bars. Return -1 if it
4+
* can't be done.
5+
*/
6+
public int makeChocolate(int small, int big, int goal) {
7+
int count = 5 * big > goal ? goal - (goal % 5) : 5 * big;
8+
9+
if(goal - count <= small)
10+
return goal - count;
11+
12+
return -1;
13+
}

java/logic-2/noTeenSum.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Given 3 int values, a b c, return their sum. However, if any of the values
2+
* is a teen -- in the range 13..19 inclusive -- then that value counts as 0,
3+
* except 15 and 16 do not count as a teens. Write a separate helper
4+
* "public int fixTeen(int n) {"that takes in an int value and returns that
5+
* value fixed for the teen rule. In this way, you avoid repeating the teen
6+
* code 3 times (i.e. "decomposition"). Define the helper below and at the
7+
* same indent level as the main noTeenSum().
8+
*/
9+
public int noTeenSum(int a, int b, int c) {
10+
return fixTeen(a) + fixTeen(b) + fixTeen(c);
11+
}
12+
13+
public int fixTeen(int n) {
14+
if(13 <= n && n <= 19 && n != 15 && n != 16)
15+
return 0;
16+
17+
return n;
18+
}

java/logic-2/roundSum.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* For this problem, we'll round an int value up to the next multiple of 10
2+
* if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately,
3+
* round down to the previous multiple of 10 if its rightmost digit is less
4+
* than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of
5+
* their rounded values. To avoid code repetition, write a separate helper
6+
* "public int round10(int num) {" and call it 3 times. Write the helper
7+
* entirely below and at the same indent level as roundSum().
8+
*/
9+
public int roundSum(int a, int b, int c) {
10+
return round10(a) + round10(b) + round10(c);
11+
}
12+
13+
public int round10(int num) {
14+
int rd = num % 10;
15+
16+
if(rd >= 5)
17+
return num + 10 - rd;
18+
19+
return num - rd;
20+
}

0 commit comments

Comments
 (0)