Skip to content

Commit

Permalink
Add solution to Lab 01 assignment (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
hspasov authored Oct 25, 2023
1 parent 75d8731 commit bc2f99a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 01-intro-to-java/lab/solution/BrokenKeyboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class BrokenKeyboard {

public static int calculateFullyTypedWords(String text, String brokenLetters) {
String[] allWords = text.split(" ");
int corruptedWordsCount = 0;

for (String word : allWords) {
if (word.isBlank()) {
corruptedWordsCount++;
continue;
}

for (char c : brokenLetters.toCharArray()) {
if (word.contains(Character.toString(c))) {
corruptedWordsCount++;
break;
}
}
}

return allWords.length - corruptedWordsCount;
}
}
36 changes: 36 additions & 0 deletions 01-intro-to-java/lab/solution/IPValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class IPValidator {

public static boolean validateIPv4Address(String str) {
String[] tokens = str.split("\\."); // The '.' (dot) is a special character in regex: it matches any single character except newlines. Therefore, we need to escape it with '\'. But '\' is also an escape character in Java literal strings, therefore two backslashes must be used.
if (tokens.length != 4) {
return false;
}
for (String token : tokens) {
if (!isValidIPv4Token(token)) {
return false;
}
}
return true;
}

private static boolean isValidIPv4Token(String token) {
if (token.isEmpty()) {
return false;
}
if (token.startsWith("0") && token.length() > 1) {
return false;
}

int octet = 0;

for (char digit : token.toCharArray()) {
if (digit < '0' || digit > '9') {
return false;
}
octet = octet * 10 + (digit - '0');
}

return octet < 256;
}

}
14 changes: 14 additions & 0 deletions 01-intro-to-java/lab/solution/JumpGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class JumpGame {

public static boolean canWin(int[] array) {
int reachable = 0;
for (int i = 0; i < array.length; i++) {
if (i > reachable) {
return false;
}
reachable = Math.max(reachable, i + array[i]);
}
return true;
}

}

0 comments on commit bc2f99a

Please sign in to comment.