Skip to content

Commit 18c2bcd

Browse files
author
Alfredo Miranda
committed
Added remaining warmup 1 problems
1 parent 9c4a1e2 commit 18c2bcd

24 files changed

+246
-0
lines changed

java/warmup-1/backAround.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Given a string, take the last char and return a new string with the last
2+
* char added at the front and back, so "cat" yields "tcatt". The original
3+
* string will be length 1 or more.
4+
*/
5+
public String backAround(String str) {
6+
char last = str.charAt(str.length() - 1);
7+
return last + str + last;
8+
}

java/warmup-1/close10.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Given 2 int values, return whichever value is nearest to the value 10, or
2+
* return 0 in the event of a tie. Note that Math.abs(n) returns the absolute
3+
* value of a number.
4+
*/
5+
public int close10(int a, int b) {
6+
int distA = Math.abs(a - 10);
7+
int distB = Math.abs(b - 10);
8+
9+
if(distA == distB)
10+
return 0;
11+
12+
return distA < distB ? a : b;
13+
}

java/warmup-1/delDel.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Given a string, if the string "del" appears starting at index 1, return a
2+
* string where that "del" has been deleted. Otherwise, return the string
3+
* unchanged.
4+
*/
5+
public String delDel(String str) {
6+
if(str.length() >= 4 && str.substring(1, 4).equals("del"))
7+
return str.charAt(0) + str.substring(4);
8+
9+
return str;
10+
}

java/warmup-1/endUp.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Given a string, return a new string where the last 3 chars are now in
2+
* upper case. If the string has less than 3 chars, uppercase whatever is
3+
* there. Note that str.toUpperCase() returns the uppercase version of a
4+
* string.
5+
*/
6+
public String endUp(String str) {
7+
if(str.length() < 3)
8+
return str.toUpperCase();
9+
10+
return str.substring(0, str.length() - 3) +
11+
str.substring(str.length() - 3).toUpperCase();
12+
}

java/warmup-1/everyNth.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Given a non-empty string and an int N, return the string made starting
2+
* with char 0, and then every Nth char of the string. So if N is 3, use
3+
* char 0, 3, 6, ... and so on. N is 1 or more.
4+
*/
5+
public String everyNth(String str, int n) {
6+
int nSize = (int) Math.ceil((double) str.length() / n);
7+
char[] nchar = new char[nSize];
8+
int index = 0;
9+
10+
for(int i = 0; i < str.length(); i += n) {
11+
nchar[index] = str.charAt(i);
12+
index++;
13+
}
14+
15+
return new String(nchar);
16+
}

java/warmup-1/front22.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Given a string, take the first 2 chars and return the string with the 2
2+
* chars added at both the front and back, so "kitten" yields"kikittenki". If
3+
* the string length is less than 2, use whatever chars are there.
4+
*/
5+
public String front22(String str) {
6+
String front;
7+
if(str.length() < 2)
8+
front = str;
9+
else
10+
front = str.substring(0, 2);
11+
12+
return front + str + front;
13+
}

java/warmup-1/front3.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Given a string, we'll say that the front is the first 3 chars of the
2+
* string. If the string length is less than 3, the front is whatever is
3+
* there. Return a new string which is 3 copies of the front.
4+
*/
5+
public String front3(String str) {
6+
String front;
7+
if(str.length() < 3)
8+
front = str;
9+
else
10+
front = str.substring(0, 3);
11+
12+
return front + front + front;
13+
}

java/warmup-1/frontBack.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Given a string, return a new string where the first and last chars have
2+
* been exchanged.
3+
*/
4+
public String frontBack(String str) {
5+
if(str.length() <= 1)
6+
return str;
7+
8+
char first = str.charAt(0);
9+
char last = str.charAt(str.length() - 1);
10+
11+
return last + str.substring(1, str.length()-1) + first;
12+
}

java/warmup-1/hasTeen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* We'll say that a number is "teen" if it is in the range 13..19 inclusive.
2+
* Given 3 int values, return true if 1 or more of them are teen.
3+
*/
4+
public boolean hasTeen(int a, int b, int c) {
5+
return 13 <= a && a <= 19 ||
6+
13 <= b && b <= 19 ||
7+
13 <= c && c <= 19;
8+
}

java/warmup-1/icyHot.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Given two temperatures, return true if one is less than 0 and the other is
2+
* greater than 100.
3+
*/
4+
public boolean icyHot(int temp1, int temp2) {
5+
return (temp1 * temp2 < 0) && Math.abs(temp1 - temp2) >= 102;
6+
}

0 commit comments

Comments
 (0)