Skip to content

Commit 0b99586

Browse files
committed
update
1 parent 4cb7411 commit 0b99586

24 files changed

+369
-0
lines changed

CodingBat/Warmup-1/backAround.java

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

CodingBat/Warmup-1/close10.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number.
3+
eg:
4+
close10(8, 13) → 8
5+
close10(13, 8) → 8
6+
close10(13, 7) → 0
7+
*/
8+
9+
public int close10(int a, int b) {
10+
int aDiff = Math.abs(a-10);
11+
int bDiff = Math.abs(b-10);
12+
13+
if(aDiff == bDiff){
14+
return 0;
15+
}else if(aDiff > bDiff){
16+
return b;
17+
}else{
18+
return a;
19+
}
20+
}

CodingBat/Warmup-1/delDel.java

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

CodingBat/Warmup-1/endUp.java

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

CodingBat/Warmup-1/everyNth.java

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

CodingBat/Warmup-1/front22.java

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

CodingBat/Warmup-1/front3.java

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

CodingBat/Warmup-1/frontBack.java

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

CodingBat/Warmup-1/hasTeen.java

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

CodingBat/Warmup-1/icyHot.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Given two temperatures, return true if one is less than 0 and the other is greater than 100.
3+
eg:
4+
icyHot(120, -1) → true
5+
icyHot(-1, 120) → true
6+
icyHot(2, 120) → false
7+
*/
8+
9+
public boolean icyHot(int temp1, int temp2) {
10+
return (temp1<0 && temp2>100) || (temp2<0 && temp1>100);
11+
}

0 commit comments

Comments
 (0)