-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaChallenge.java
More file actions
115 lines (88 loc) · 3.24 KB
/
Copy pathJavaChallenge.java
File metadata and controls
115 lines (88 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Andrea
* @implNote Hacker Rank Java practice implementations
*
* https://www.hackerrank.com/domains/java
*/
public class JavaChallenge {
/*
* Complete the 'findDay' function below.
*
* The function is expected to return a STRING. The function accepts following
* parameters: 1. INTEGER month 2. INTEGER day 3. INTEGER year
*/
public static String findDay(int month, int day, int year) {
String[] strDays = new String[] { "SUNDAY", "MONDAY", "TUESDAY", "WEDNSDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year, month - 1, day);
return strDays[cal.get(java.util.Calendar.DAY_OF_WEEK) - 1];
}
/*
* Check if a BigInteger is probably a prime number
* https://www.hackerrank.com/challenges/java-primality-test/problem
*/
public static String isPrime(String number) {
return new java.math.BigInteger(number).isProbablePrime(100) ? "prime" : "not prime";
}
/**
* add and multiply huge numbers!
* https://www.hackerrank.com/challenges/java-biginteger/problem
*/
public static String calculateBigInteger(String numberOne, String numberTwo) {
java.math.BigInteger one = new java.math.BigInteger(numberOne);
java.math.BigInteger two = new java.math.BigInteger(numberTwo);
return one.add(two) + "\n" + one.multiply(two);
}
public static final String regularExpression = "([a-zA-Z])(\\w){7,29}";
/**
* https://www.hackerrank.com/challenges/valid-username-checker/problem
* @param args
*/
public static String usernameValidator(String s) {
if ( s.matches(regularExpression) ) {
return "Valid";
} else {
return "Invalid";
}
}
/**
* https://www.hackerrank.com/challenges/tag-content-extractor/problem
*
* Code challenge day #2
* @param line
*/
public static void tagContentExtractor(String line) {
boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(2));
matchFound = true;
}
if ( ! matchFound) {
System.out.println("None");
}
}
/**
* https://www.hackerrank.com/challenges/java-exception-handling/problem
*/
public static int power(int n, int p) throws Exception {
if ( n < 0 || p < 0 ) {
throw new Exception("n or p should not be negative.");
} else if ( n == 0 && p == 0 ) {
throw new Exception("n and p should not be zero.");
} else {
return (int) Math.pow(n, p);
}
}
public static void main(String[] args) {
// System.out.println(findDay(10, 9, 2019));
// System.out.println(isPrime("12312873127836127833"));
System.out.println(usernameValidator("aaaaaaa"));
// tagContentExtractor("<h1><h1>Sanjay has no watch</h1></h1><par>So wait for a while</par>");
}
}