Skip to content

Commit 0d25933

Browse files
committed
update
1 parent 0b99586 commit 0d25933

File tree

14 files changed

+286
-0
lines changed

14 files changed

+286
-0
lines changed

CodingBat/Warmup-2/altPairs.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
3+
eg:
4+
altPairs("kitten") → "kien"
5+
altPairs("Chocolate") → "Chole"
6+
altPairs("CodingHorror") → "Congrr"
7+
*/
8+
9+
public String altPairs(String str) {
10+
String result = "";
11+
for(int i=0; i<str.length(); i+=4){
12+
int end = i + 2;
13+
if (end > str.length()){
14+
end = str.length();
15+
}
16+
result += str.substring(i,end);
17+
}
18+
return result;
19+
}

CodingBat/Warmup-2/array123.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
3+
Given an array of ints, return true if the sequence of numbers 1, 2, 3 appears in the array somewhere.
4+
eg:
5+
array123([1, 1, 2, 3, 1]) → true
6+
array123([1, 1, 2, 4, 1]) → false
7+
array123([1, 1, 2, 1, 2, 3]) → true
8+
*/
9+
10+
public boolean array123(int[] nums) {
11+
for(int i=0; i<nums.length-2; i++){
12+
if(nums[i]==1 && nums[i+1]==2 && nums[i+2]==3){
13+
return true;
14+
}
15+
}
16+
return false;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Given an array of ints, return the number of 9's in the array.
3+
eg:
4+
arrayCount9([1, 2, 9]) → 1
5+
arrayCount9([1, 9, 9]) → 2
6+
arrayCount9([1, 9, 9, 3, 9]) → 3
7+
*/
8+
9+
public int arrayCount9(int[] nums) {
10+
int count = 0;
11+
for(int i=0; i<nums.length; i++){
12+
if(nums[i]==9){
13+
count += 1;
14+
}
15+
}
16+
return count;
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
3+
Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length may be less than 4.
4+
eg:
5+
arrayFront9([1, 2, 9, 3, 4]) → true
6+
arrayFront9([1, 2, 3, 4, 9]) → false
7+
arrayFront9([1, 2, 3, 4, 5]) → false
8+
*/
9+
10+
public boolean arrayFront9(int[] nums) {
11+
int end = nums.length;
12+
if(end > 4) end = 4;
13+
14+
for(int i=0; i<end; i++){
15+
if(nums[i] == 9) return true;
16+
}
17+
return false;
18+
19+
/*
20+
if(nums.length < 4){
21+
for(int i=0; i< nums.length; i++){
22+
if(nums[i] == 9) return true;
23+
}
24+
return false;
25+
}
26+
for(int i=0; i<4; i++){
27+
if(nums[i] == 9) return true;
28+
}
29+
return false;
30+
*/
31+
}

CodingBat/Warmup-2/countXX.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx".
3+
eg:
4+
countXX("abcxx") → 1
5+
countXX("xxx") → 2
6+
countXX("xxxx") → 3
7+
*/
8+
9+
int countXX(String str) {
10+
int count = 0;
11+
for(int i=0; i<str.length()-1; i++){
12+
if(str.substring(i,i+2).equals("xx")){
13+
count += 1;
14+
}
15+
}
16+
return count;
17+
}

CodingBat/Warmup-2/doubleX.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, return true if the first instance of "x" in the string is immediately followed by another "x".
4+
eg:
5+
doubleX("axxbb") → true
6+
doubleX("axaxax") → false
7+
doubleX("xxxxx") → true
8+
*/
9+
10+
boolean doubleX(String str) {
11+
if (!str.contains("x")) return false;
12+
13+
int firstX = str.indexOf("x");
14+
if(firstX < str.length()-1){
15+
return str.substring(firstX, firstX+2).equals("xx");
16+
}else{
17+
return false;
18+
}
19+
}

CodingBat/Warmup-2/frontTimes.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;
4+
eg:
5+
frontTimes("Chocolate", 2) → "ChoCho"
6+
frontTimes("Chocolate", 3) → "ChoChoCho"
7+
frontTimes("Abc", 3) → "AbcAbcAbc"
8+
*/
9+
10+
public String frontTimes(String str, int n) {
11+
/*
12+
String front;
13+
String result = "";
14+
15+
if(str.length()<3){
16+
for(int i=0; i<n; i++){
17+
result += str;
18+
}
19+
}else{
20+
front = str.substring(0,3);
21+
for(int i=0; i<n; i++){
22+
result += front;
23+
}
24+
}
25+
return result;
26+
*/
27+
28+
int frontLen = 3;
29+
String front;
30+
String result = "";
31+
32+
if (frontLen > str.length()){
33+
frontLen = str.length();
34+
}
35+
36+
front = str.substring(0, frontLen);
37+
38+
for(int i =0; i<n; i++){
39+
result += front;
40+
}
41+
return result;
42+
}

CodingBat/Warmup-2/last2.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring).
3+
eg:
4+
last2("hixxhi") → 1
5+
last2("xaxxaxaxx") → 1
6+
last2("axxxaaxx") → 2
7+
*/
8+
9+
public int last2(String str) {
10+
if(str.length()<3) return 0;
11+
12+
int count = 0;
13+
String last = str.substring(str.length()-2);
14+
15+
for(int i=0; i<str.length()-2; i++){
16+
if(str.substring(i,i+2).equals(last)){
17+
count += 1;
18+
}
19+
}
20+
return count;
21+
}

CodingBat/Warmup-2/stringBits.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 made of every other char starting with the first, so "Hello" yields "Hlo".
3+
eg:
4+
stringBits("Hello") → "Hlo"
5+
stringBits("Hi") → "H"
6+
stringBits("Heeololeo") → "Hello"
7+
*/
8+
9+
public String stringBits(String str) {
10+
String result = "";
11+
12+
for(int i=0; i<str.length(); i+=2){
13+
result += str.charAt(i);
14+
}
15+
return result;
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
4+
eg:
5+
stringMatch("xxcaazz", "xxbaaz") → 3
6+
stringMatch("abc", "abc") → 2
7+
stringMatch("abc", "axc") → 0
8+
*/
9+
10+
public int stringMatch(String a, String b) {
11+
/*
12+
int count = 0;
13+
int length;
14+
if(a.length() > b.length()) length = b.length();
15+
else length = a.length();
16+
for(int i=0; i<length-1; i++){
17+
if(a.substring(i,i+2).equals(b.substring(i,i+2))){
18+
count += 1;
19+
}
20+
}
21+
return count;
22+
*/
23+
24+
int len = Math.min(a.length(), b.length());
25+
int count = 0;
26+
27+
for (int i=0; i<len-1; i++){
28+
if(a.substring(i,i+2).equals(b.substring(i,i+2))){
29+
count += 1;
30+
}
31+
}
32+
return count;
33+
}

0 commit comments

Comments
 (0)