Skip to content

Commit 08ecd5d

Browse files
committed
warmup-2 added
1 parent a7da15f commit 08ecd5d

17 files changed

+195
-0
lines changed

warmup-2/altPairs.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public String altPairs(String str) {
2+
String result = "";
3+
4+
for(int i = 0; i < str.length(); i+=4) {
5+
int index = i + 2;
6+
if (index > str.length()) {
7+
index = str.length();
8+
}
9+
result = result + str.substring(i, index);
10+
}
11+
return result;
12+
}
13+

warmup-2/array123.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public boolean array123(int[] nums) {
2+
for (int i=0; i < nums.length-2; i++) {
3+
if (nums[i] == 1 && nums[i+1] == 2 && nums[i+2] == 3 ){
4+
return true;
5+
}
6+
}
7+
return false;
8+
}

warmup-2/array667.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public int array667(int[] nums) {
2+
int count = 0;
3+
4+
for(int i = 0; i < nums.length-1; i++) {
5+
if(nums[i] == 6 && (nums[i+1] == 6 || nums[i+1] == 7)) {
6+
count += 1;
7+
}
8+
}
9+
return count;
10+
}

warmup-2/arrayCount9.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public int arrayCount9(int[] nums) {
2+
int count = 0;
3+
4+
for(int num : nums){
5+
if(num == 9){
6+
count += 1;
7+
}
8+
}
9+
return count;
10+
}

warmup-2/arrayFront9.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public boolean arrayFront9(int[] nums) {
2+
int length = nums.length;
3+
if (length > 4) {
4+
length = 4;
5+
}
6+
7+
for(int i = 0; i < length; i++){
8+
if(nums[i] == 9){
9+
return true;
10+
}
11+
}
12+
return false;
13+
}

warmup-2/countXX.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int countXX(String str) {
2+
int count = 0;
3+
4+
for(int i = 0; i < str.length()-1; i++){
5+
if(str.substring(i, i + 2).equals("xx")){
6+
count += 1;
7+
}
8+
}
9+
return count;
10+
}

warmup-2/doubleX.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
boolean doubleX(String str) {
2+
3+
int firstX = str.indexOf("x");
4+
5+
if(firstX == -1) {
6+
return false;
7+
}
8+
9+
String sub = str.substring(firstX);
10+
11+
return sub.startsWith("xx");
12+
}

warmup-2/frontTimes.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public String frontTimes(String str, int n) {
2+
String s;
3+
String result = "";
4+
if(str.length() < 3 ){
5+
s = str;
6+
} else {
7+
s = str.substring(0,3);
8+
}
9+
for(int i = 0; i < n; i++){
10+
result += s;
11+
}
12+
return result;
13+
}

warmup-2/has271.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public boolean has271(int[] nums) {
2+
3+
for(int i = 0; i < nums.length - 2; i++){
4+
//need to wrap nums[i]-1 in parentheses otherwise won't work
5+
if(nums[i] + 5 == nums[i+1] && Math.abs(nums[i+2] - (nums[i]-1)) <= 2) {
6+
return true;
7+
}
8+
}
9+
10+
return false;
11+
}

warmup-2/last2.java

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

warmup-2/noTriples.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public boolean noTriples(int[] nums) {
2+
for(int i = 0; i < nums.length - 2; i++){
3+
if(nums[i] == nums[i+1] && nums[i+1] == nums[i+2]) {
4+
return false;
5+
}
6+
}
7+
return true;
8+
}

warmup-2/stringBits.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public String stringBits(String str) {
2+
String result = "";
3+
4+
for(int i = 0; i < str.length(); i+=2) {
5+
result += str.charAt(i);
6+
}
7+
return result;
8+
}

warmup-2/stringMatch.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public int stringMatch(String a, String b) {
2+
3+
int length;
4+
5+
if(a.length() > b.length()) {
6+
length = b.length();
7+
} else {
8+
length = a.length();
9+
}
10+
11+
int count = 0;
12+
for(int i = 0; i < length-1; i++){
13+
if(a.substring(i, i+2).equals(b.substring(i, i+2))){
14+
count += 1;
15+
}
16+
}
17+
return count;
18+
}

warmup-2/stringSplosion.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public String stringSplosion(String str) {
2+
String result = "";
3+
4+
for(int i = 0; i <= str.length(); i++){
5+
result += str.substring(0, i);
6+
}
7+
return result;
8+
}

warmup-2/stringTimes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public String stringTimes(String str, int n) {
2+
String result = "";
3+
for(int i = 0; i < n; i++){
4+
result += str;
5+
}
6+
return result;
7+
}

warmup-2/stringX.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public String stringX(String str) {
2+
String result = "";
3+
for (int i = 0; i < str.length(); i++) {
4+
if (i > 0 && i < str.length() - 1 && str.substring(i, i+1).equals("x")) {
5+
continue;
6+
} else {
7+
result = result + str.charAt(i);
8+
}
9+
}
10+
return result;
11+
}

warmup-2/stringYak.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public String stringYak(String str) {
2+
String result = "";
3+
for(int i = 0; i < str.length(); i++){
4+
if(i+2 < str.length() && str.charAt(i) == 'y' && str.charAt(i+2) == 'k'){
5+
i = i+2;
6+
} else {
7+
result += str.charAt(i);
8+
}
9+
}
10+
return result;
11+
}

0 commit comments

Comments
 (0)