-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution to Lab 01 assignment (#119)
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |