***The hashing function that will be used in this problem is as follows. Let f(x) be a function that takes a character and returns its decimal character code in the ASCII table. For instance f('a') = 97, f('B') = 66, and f('9') = 57. (You can find all ASCII character codes here: ASCII table.) Then, let h(s) be the hashing function that takes a string and hashes it in the following way, where p = 131 and M = 109+7:
h(s) := (s[0]p(n-1) + s[1]p(n-2) + s[2]p(n-3) + ... + s[n-2]p + s[n-1]) mod M
For instance, if s = "cAr1", then the formula would be as follows:
h(s) = (f('c')1313+f('A')1312 +f('r')131 + f('1')) mod 109+7 = 223691457
Your system will be tested on q event types, each of which will be one of the following:
setPassword(s) := sets the password to s
authorize(x) := tries to sign in with integer x. This event must return 1 if x is either the hash of the current password or the hash of the current password with a single character appended to it. Otherwise, this event must return 0.
Consider the following example. There are 6 events to be handled:
setPassword("cAr1")
authorize(223691457)
authorize(303580761)
authorize(100)
setPassword("d")
authorize(100)
As we know from the above example, h("cAr1") = 223691457, so the second event will return 1. The third event will also return 1 because 303580761 is the hash value of the string "cAr1a", which is equal to the current password with the character 'a' appended to it. The fourth event will return 0 because 100 is not a hash of the current password or of the current password with a single character appended to it. In the fifth event, the current password is set to "d", and the sixth event will return 1 because h("d") = 100. Therefore, the array you would return is [1, 10, 1], corresponding to the success or failure of the authorization events.*
**
import java.io.;
import java.math.;
import java.security.;
import java.text.;
import java.util.;
import java.util.concurrent.;
import java.util.function.;
import java.util.regex.;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static List<Integer> authEvents(List<List<String>> events) {
List<Integer> results = new ArrayList<>();
PasswordSystem passwordSystem = new PasswordSystem();
for (List<String> event : events) {
String eventType = event.get(0);
if (eventType.equals("setPassword")) {
passwordSystem.setPassword(event.get(1));
} else if (eventType.equals("authorize")) {
int value = Integer.parseInt(event.get(1));
int result = passwordSystem.authorize(value);
results.add(result);
}
}
return results;
}
static class PasswordSystem {
private String password = "";
private final int p = 131;
private final int M = 1000000007;
public void setPassword(String s) {
this.password = s;
}
public int authorize(int x) {
String var1 = password;
int hashValue = calculateHash(password);
if (hashValue == x) return 1;
System.out.println("Resulting string: " + var1+" hashValue " +hashValue+ " x value "+x);
char c = (char) 'a';
String tempVar = var1 + c;
hashValue = calculateHash(tempVar);
hashValue = hashValue - 97;
// Append all ASCII values from 0 to 127
for (int asciiValue = 0; asciiValue <= 127; asciiValue++) {
hashValue = hashValue + asciiValue;
if (hashValue == x) {
return 1;
}
hashValue = hashValue - asciiValue;
}
return 0;
}
private int calculateHash(String s) {
int n = s.length();
int hashValue = 0;
StringBuilder stringBuilder = new StringBuilder(s);
for (int i = 0; i < n; i++) {
hashValue = (int) ((hashValue + stringBuilder.charAt(i) * Math.pow(p, n - i - 1)) % M);
}
return hashValue;
}
}}
Compiled successfully.5/15 test cases passed
Use print or log statements to debug why your hidden test cases are failing. Hidden test cases are used to evaluate if your code can handle different scenarios, including corner cases.