Skip to content

Medium challenges #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/medium/Division.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package medium;

/**
* Have the function Division(num1,num2) take both parameters being passed
* and return the Greatest Common Factor.
* That is, return the greatest number that evenly goes into both numbers
* with no remainder.
* ---
* For example: 12 and 16 both are divisible by 1, 2, and 4 so the output should be 4.
* The range for both parameters will be from 1 to 10^3.
*/
public class Division {

/**
* Division function.
*
* @param num1 input number 1
* @param num2 input number 2
* @return the GCF
*/
private static int division(int num1, int num2) {
if (num1 == 0 || num2 == 0) {
return num1 + num2;
}

return division(num2, num1 % num2);
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
int result1 = division(20, 40);
System.out.println(result1);
int result2 = division(12, 16);
System.out.println(result2);
}

}
99 changes: 99 additions & 0 deletions src/medium/EvenPairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package medium;

/**
* Have the function EvenPairs(str) take the str parameter being passed
* and determine if a pair of adjacent even numbers exists anywhere
* in the string. If a pair exists, return the string true,
* otherwise return false.
* ---
* For example: if str is "f178svg3k19k46"
* then there are two even numbers at the end of the string, "46"
* so your program should return the string true.
* ---
* Another example: if str is "7r5gg812" then the pair is "812" (8 and 12)
* so your program should return the string true.
*/
public class EvenPairs {

/**
* Strips a string from non-numerical characters
* and splits the chunks into the array of strings
* e.g."5678dd12y" -> ["5678, "12"]
*
* @param str input string
* @return an array containing number groups
*/
private static String[] splitNumbers(String str) {
return str
.toLowerCase()
.replaceAll("([^0-9])", " ")
.replaceAll(" +", " ")
.trim().split(" ");
}

/**
* Appends N following zeroes to 1 where N = number of digits
* e.g. 1 -> 1, 2 -> 100, 3 -> 1000
*
* @param digits number of digits
* @return expanded number
*/
private static int addFollowingZeroes(int digits) {
int result = 1;
if (digits == 1) {
return result;
}
for (int i = 0; i < digits; i++) {
result *= 10;
}
return result;
}

/**
* Parses a number to determine if digits can form a pair of even number.
*
* @param num input number
* @return true if it's a pair of even numbers
*/
private static boolean isEvenPair(Integer num) {
// get a number of digits
int len = (int) (Math.log10(num) + 1);
for (int i = len - 1; i > 0; i--) {
int num1 = num / addFollowingZeroes(i);
int num2 = num - num1;
if (num1 % 2 == 0 && num2 % 2 == 0) {
return true;
}
}
return false;
}

/**
* Even Pairs function.
*
* @param str input string
* @return "true" if a pair exists
*/
public static String evenPairs(String str) {
String[] coll = splitNumbers(str);
for (String item : coll) {
if (item.length() > 1 && isEvenPair(Integer.parseInt(item))) {
return "true";
}
}
return "false";
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
String result1 = evenPairs("7r5gg812");
System.out.println(result1);
String result2 = evenPairs("f178svg3k19k46");
System.out.println(result2);
}

}
119 changes: 119 additions & 0 deletions src/medium/HtmlElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package medium;

import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Have the function HTMLElements(str) read the str parameter being passed
* which will be a string of HTML DOM elements and plain text.
* ---
* The elements that will be used are: b, i, em, div, p.
* For example: if str is "<div><b><p>hello world</p></b></div>" then this string of DOM elements
* is nested correctly so your program should return the string true.
* ---
* If a string is not nested correctly, return the first element encountered where,
* if changed into a different element, would result in a properly formatted string.
* ---
* If the string is not formatted properly,
* then it will only be one element that needs to be changed.
* For example: if str is "<div><i>hello</i>world</b>"
* then your program should return the string div
* because if the first <div> element were changed into a <b>,
* the string would be properly formatted.
*/
public class HtmlElements {

/**
* A stack helps to determine if elements are nested correctly.
*/
private static final Stack<String> stack = new Stack<>();

/**
* Process a string to prepare for further inspection.
* 1. convert to lowercase
* 2. remove text content between tags
* 3. add spaces between tags
* 4. remove multiple spaces
* 5. trim (remove leading and trailing spaces)
* 6. split to array of strings
*
* @param str input string
* @return parsed string
*/
private static String[] parseElements(String str) {
return str
.toLowerCase()
.replaceAll(">[^<]+", ">")
.replaceAll("([>])(?=[<])", "$1 ")
.replaceAll(" +", " ")
.trim().split(" ");
}

/**
* Checks if a string is an opening tag.
*
* @param tag a string to check
* @return true if a string is an opening tag
*/
private static boolean isOpeningTag(String tag) {
Pattern pattern = Pattern.compile("<[a-z]>|<[a-z][a-z1-9]+>");
Matcher matcher = pattern.matcher(tag);
return matcher.find();
}

/**
* Checks if a string is an closing tag.
*
* @param tag a string to check
* @return true if a string is a closing tag
*/
private static boolean isClosingTag(String tag) {
Pattern pattern = Pattern.compile("</[a-z]>|</[a-z][a-z1-9]+>");
Matcher matcher = pattern.matcher(tag);
return matcher.find();
}

/**
* Get an enclosed value.
*
* @param tag input tag with angle brackets
* @return the enclosed value of a tag
*/
private static String getTagValue(String tag) {
return tag.replaceAll("[></]", "");
}

/**
* HTML Elements function.
*
* @param str input string
* @return "true" if elements are nested correctly, or an enclosed tag value if not.
*/
private static String htmlElements(String str) {
String[] parsedTags = parseElements(str);
for (String tag : parsedTags) {
if (isOpeningTag(tag)) {
stack.push(tag);
} else if (isClosingTag(tag) && !stack.isEmpty()) {
if (getTagValue(stack.peek()).equals(getTagValue(tag))) {
stack.pop();
}
}
}
return stack.isEmpty() ? "true" : getTagValue(stack.peek());
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
String result1 = htmlElements("<div><b><p>hello world</p></b></div>");
System.out.println(result1);
String result2 = htmlElements("<div><b><p>hello world</b></div>");
System.out.println(result2);
}

}
57 changes: 57 additions & 0 deletions src/medium/PalindromeTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package medium;

/**
* Have the function PalindromeTwo(str) take the str parameter being passed
* and return the string true if the parameter is a palindrome,
* (the string is the same forward as it is backward)
* otherwise return the string false.
* ---
* The parameter entered may have punctuation and symbols,
* but they should not affect whether the string is in fact a palindrome.
* For example: "Anne, I vote more cars race Rome-to-Vienna" should return true.
*/
public class PalindromeTwo {

/**
* Check if a string is a palindrome.
*
* @param str input string
* @return true if a string is a palindrome
*/
private static boolean isPalindrome(String str) {
char[] strArr = str.toCharArray();
int len = strArr.length;
for (int i = 0; i < len / 2; i++) {
if (strArr[i] != strArr[len - i - 1]) {
return false;
}
}
return true;
}

/**
* Palindrome Two function.
*
* @param str input string
* @return "true" if a string is a palindrome
*/
public static String palindromeTwo(String str) {
String parsed = str.toLowerCase().replaceAll("[^a-z$]", "");
return isPalindrome(parsed) ? "true" : "false";
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
String result1 = palindromeTwo("Noel - sees Leon");
System.out.println(result1);
String result2 = palindromeTwo("A man, a plan, a canal – Panama.");
System.out.println(result2);
String result3 = palindromeTwo("No lemon, no melon!");
System.out.println(result3);
}

}
Loading