Skip to content

Commit 3c8b519

Browse files
authored
Merge pull request ardallie#3 from ardallie/medium
Medium challenges
2 parents 3aeba10 + 356a4a4 commit 3c8b519

12 files changed

+1065
-0
lines changed

src/medium/Division.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package medium;
2+
3+
/**
4+
* Have the function Division(num1,num2) take both parameters being passed
5+
* and return the Greatest Common Factor.
6+
* That is, return the greatest number that evenly goes into both numbers
7+
* with no remainder.
8+
* ---
9+
* For example: 12 and 16 both are divisible by 1, 2, and 4 so the output should be 4.
10+
* The range for both parameters will be from 1 to 10^3.
11+
*/
12+
public class Division {
13+
14+
/**
15+
* Division function.
16+
*
17+
* @param num1 input number 1
18+
* @param num2 input number 2
19+
* @return the GCF
20+
*/
21+
private static int division(int num1, int num2) {
22+
if (num1 == 0 || num2 == 0) {
23+
return num1 + num2;
24+
}
25+
26+
return division(num2, num1 % num2);
27+
}
28+
29+
/**
30+
* Entry point.
31+
*
32+
* @param args command line arguments
33+
*/
34+
public static void main(String[] args) {
35+
int result1 = division(20, 40);
36+
System.out.println(result1);
37+
int result2 = division(12, 16);
38+
System.out.println(result2);
39+
}
40+
41+
}

src/medium/EvenPairs.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package medium;
2+
3+
/**
4+
* Have the function EvenPairs(str) take the str parameter being passed
5+
* and determine if a pair of adjacent even numbers exists anywhere
6+
* in the string. If a pair exists, return the string true,
7+
* otherwise return false.
8+
* ---
9+
* For example: if str is "f178svg3k19k46"
10+
* then there are two even numbers at the end of the string, "46"
11+
* so your program should return the string true.
12+
* ---
13+
* Another example: if str is "7r5gg812" then the pair is "812" (8 and 12)
14+
* so your program should return the string true.
15+
*/
16+
public class EvenPairs {
17+
18+
/**
19+
* Strips a string from non-numerical characters
20+
* and splits the chunks into the array of strings
21+
* e.g."5678dd12y" -> ["5678, "12"]
22+
*
23+
* @param str input string
24+
* @return an array containing number groups
25+
*/
26+
private static String[] splitNumbers(String str) {
27+
return str
28+
.toLowerCase()
29+
.replaceAll("([^0-9])", " ")
30+
.replaceAll(" +", " ")
31+
.trim().split(" ");
32+
}
33+
34+
/**
35+
* Appends N following zeroes to 1 where N = number of digits
36+
* e.g. 1 -> 1, 2 -> 100, 3 -> 1000
37+
*
38+
* @param digits number of digits
39+
* @return expanded number
40+
*/
41+
private static int addFollowingZeroes(int digits) {
42+
int result = 1;
43+
if (digits == 1) {
44+
return result;
45+
}
46+
for (int i = 0; i < digits; i++) {
47+
result *= 10;
48+
}
49+
return result;
50+
}
51+
52+
/**
53+
* Parses a number to determine if digits can form a pair of even number.
54+
*
55+
* @param num input number
56+
* @return true if it's a pair of even numbers
57+
*/
58+
private static boolean isEvenPair(Integer num) {
59+
// get a number of digits
60+
int len = (int) (Math.log10(num) + 1);
61+
for (int i = len - 1; i > 0; i--) {
62+
int num1 = num / addFollowingZeroes(i);
63+
int num2 = num - num1;
64+
if (num1 % 2 == 0 && num2 % 2 == 0) {
65+
return true;
66+
}
67+
}
68+
return false;
69+
}
70+
71+
/**
72+
* Even Pairs function.
73+
*
74+
* @param str input string
75+
* @return "true" if a pair exists
76+
*/
77+
public static String evenPairs(String str) {
78+
String[] coll = splitNumbers(str);
79+
for (String item : coll) {
80+
if (item.length() > 1 && isEvenPair(Integer.parseInt(item))) {
81+
return "true";
82+
}
83+
}
84+
return "false";
85+
}
86+
87+
/**
88+
* Entry point.
89+
*
90+
* @param args command line arguments
91+
*/
92+
public static void main(String[] args) {
93+
String result1 = evenPairs("7r5gg812");
94+
System.out.println(result1);
95+
String result2 = evenPairs("f178svg3k19k46");
96+
System.out.println(result2);
97+
}
98+
99+
}

src/medium/HtmlElements.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package medium;
2+
3+
import java.util.Stack;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
/**
8+
* Have the function HTMLElements(str) read the str parameter being passed
9+
* which will be a string of HTML DOM elements and plain text.
10+
* ---
11+
* The elements that will be used are: b, i, em, div, p.
12+
* For example: if str is "<div><b><p>hello world</p></b></div>" then this string of DOM elements
13+
* is nested correctly so your program should return the string true.
14+
* ---
15+
* If a string is not nested correctly, return the first element encountered where,
16+
* if changed into a different element, would result in a properly formatted string.
17+
* ---
18+
* If the string is not formatted properly,
19+
* then it will only be one element that needs to be changed.
20+
* For example: if str is "<div><i>hello</i>world</b>"
21+
* then your program should return the string div
22+
* because if the first <div> element were changed into a <b>,
23+
* the string would be properly formatted.
24+
*/
25+
public class HtmlElements {
26+
27+
/**
28+
* A stack helps to determine if elements are nested correctly.
29+
*/
30+
private static final Stack<String> stack = new Stack<>();
31+
32+
/**
33+
* Process a string to prepare for further inspection.
34+
* 1. convert to lowercase
35+
* 2. remove text content between tags
36+
* 3. add spaces between tags
37+
* 4. remove multiple spaces
38+
* 5. trim (remove leading and trailing spaces)
39+
* 6. split to array of strings
40+
*
41+
* @param str input string
42+
* @return parsed string
43+
*/
44+
private static String[] parseElements(String str) {
45+
return str
46+
.toLowerCase()
47+
.replaceAll(">[^<]+", ">")
48+
.replaceAll("([>])(?=[<])", "$1 ")
49+
.replaceAll(" +", " ")
50+
.trim().split(" ");
51+
}
52+
53+
/**
54+
* Checks if a string is an opening tag.
55+
*
56+
* @param tag a string to check
57+
* @return true if a string is an opening tag
58+
*/
59+
private static boolean isOpeningTag(String tag) {
60+
Pattern pattern = Pattern.compile("<[a-z]>|<[a-z][a-z1-9]+>");
61+
Matcher matcher = pattern.matcher(tag);
62+
return matcher.find();
63+
}
64+
65+
/**
66+
* Checks if a string is an closing tag.
67+
*
68+
* @param tag a string to check
69+
* @return true if a string is a closing tag
70+
*/
71+
private static boolean isClosingTag(String tag) {
72+
Pattern pattern = Pattern.compile("</[a-z]>|</[a-z][a-z1-9]+>");
73+
Matcher matcher = pattern.matcher(tag);
74+
return matcher.find();
75+
}
76+
77+
/**
78+
* Get an enclosed value.
79+
*
80+
* @param tag input tag with angle brackets
81+
* @return the enclosed value of a tag
82+
*/
83+
private static String getTagValue(String tag) {
84+
return tag.replaceAll("[></]", "");
85+
}
86+
87+
/**
88+
* HTML Elements function.
89+
*
90+
* @param str input string
91+
* @return "true" if elements are nested correctly, or an enclosed tag value if not.
92+
*/
93+
private static String htmlElements(String str) {
94+
String[] parsedTags = parseElements(str);
95+
for (String tag : parsedTags) {
96+
if (isOpeningTag(tag)) {
97+
stack.push(tag);
98+
} else if (isClosingTag(tag) && !stack.isEmpty()) {
99+
if (getTagValue(stack.peek()).equals(getTagValue(tag))) {
100+
stack.pop();
101+
}
102+
}
103+
}
104+
return stack.isEmpty() ? "true" : getTagValue(stack.peek());
105+
}
106+
107+
/**
108+
* Entry point.
109+
*
110+
* @param args command line arguments
111+
*/
112+
public static void main(String[] args) {
113+
String result1 = htmlElements("<div><b><p>hello world</p></b></div>");
114+
System.out.println(result1);
115+
String result2 = htmlElements("<div><b><p>hello world</b></div>");
116+
System.out.println(result2);
117+
}
118+
119+
}

src/medium/PalindromeTwo.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package medium;
2+
3+
/**
4+
* Have the function PalindromeTwo(str) take the str parameter being passed
5+
* and return the string true if the parameter is a palindrome,
6+
* (the string is the same forward as it is backward)
7+
* otherwise return the string false.
8+
* ---
9+
* The parameter entered may have punctuation and symbols,
10+
* but they should not affect whether the string is in fact a palindrome.
11+
* For example: "Anne, I vote more cars race Rome-to-Vienna" should return true.
12+
*/
13+
public class PalindromeTwo {
14+
15+
/**
16+
* Check if a string is a palindrome.
17+
*
18+
* @param str input string
19+
* @return true if a string is a palindrome
20+
*/
21+
private static boolean isPalindrome(String str) {
22+
char[] strArr = str.toCharArray();
23+
int len = strArr.length;
24+
for (int i = 0; i < len / 2; i++) {
25+
if (strArr[i] != strArr[len - i - 1]) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
/**
33+
* Palindrome Two function.
34+
*
35+
* @param str input string
36+
* @return "true" if a string is a palindrome
37+
*/
38+
public static String palindromeTwo(String str) {
39+
String parsed = str.toLowerCase().replaceAll("[^a-z$]", "");
40+
return isPalindrome(parsed) ? "true" : "false";
41+
}
42+
43+
/**
44+
* Entry point.
45+
*
46+
* @param args command line arguments
47+
*/
48+
public static void main(String[] args) {
49+
String result1 = palindromeTwo("Noel - sees Leon");
50+
System.out.println(result1);
51+
String result2 = palindromeTwo("A man, a plan, a canal – Panama.");
52+
System.out.println(result2);
53+
String result3 = palindromeTwo("No lemon, no melon!");
54+
System.out.println(result3);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)