Issue
Why when I use the method contains in a java list don't return true when I match the string?
Im doing an exercise in HackerEarth and I can't pass a test. this is the text of exercise:
Arpasland has surrounded by attackers. A truck enters the city. The driver claims the load is food and medicine from Iranians. Ali is one of the soldiers in Arpasland. He doubts about the truck, maybe it's from the siege. He knows that a tag is valid if the sum of every two consecutive digits of it is even and its letter is not a vowel. Determine if the tag of the truck is valid or not.
We consider the letters "A","E","I","O","U","Y" to be vowels for this problem.
Input Format
The first line contains a string of length 9. The format is "DDXDDD-DD", where D stands for a digit (non zero) and X is an uppercase english letter.
Output Format
Print "valid" (without quotes) if the tag is valid, print "invalid" otherwise (without quotes)
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
// Write your code here
int sum = 0;
boolean flagIsNotVowel = false;
boolean flagIsEven = false;
String word = s.nextLine();
System.out.println(word);
List<String> dic = new ArrayList();
dic.add("A");
dic.add("E");
dic.add("I");
dic.add("O");
dic.add("U");
dic.add("Y");
System.out.println()
for(int i =0;i<word.length();i++){
boolean flag = Character.isDigit(word.charAt(i));
if(flag) {
sum+=Integer.parseInt(word.charAt(i)+ "");
flagIsEven = sum%2==0 ? true : false;
}
if(!flag && !dic.contains(word.charAt(i) ){
System.out.println(word.charAt(i) +"letter");
System.out.println(dic.contains(word.charAt(i)) + "true");
sum = 0;
flagIsNotVowel = true;
}
if(!flag && dic.contains(word.charAt(i))){
System.out.println(word.charAt(i)+ "ciao da dic");
sum = 0;
flagIsNotVowel = false;
}
}
if(flagIsEven ==true && flagIsNotVowel == true){
System.out.println("valid");
}
else{
System.out.println("invalid");
}
}}
this is one test that I can't pass : 13A357-22
Solution
If it is allowed to use regular expressions for this task, the following solution may be suggested (somewhat similar to that of @Cary Swoveland) with some changes:
validate the format of the tag to be
DDXDDD-DD
, whereD
is a non-zero digit,X
is a consonant
This can be done using a positive lookahead:
(?=[1-9]{2}[B-Z&&[^EIOUY]][1-9]{3}-[1-9]{2})
where[B-Z&&[^EIOUY]]
is a range of capital lettersB-Z
except for vowels[^EIOUY]
(A
is excluded)check the pairs of digit NOT to contain mix of odd and even digits:
public static String isValid(String str) {
return str.matches("(?=[1-9]{2}[B-Z&&[^EIOUY]][1-9]{3}-[1-9]{2})((?!.*(?:[2468][13579]|[13579][2468]))\\d{2,3}[-B-Z&&[^EIOUY]]?){3}")
? "valid"
: "invalid";
}
Checking for the sum of two consecutive digits to be even may be implemented as follows:
public static String isValid(String str) {
boolean hasValidFormat = str.matches("\\[1-9]][B-Z&&[^EIOUY]][1-9]{3}-[1-9]{2}");
if (hasValidFormat) {
out: for (String part : str.split("\\D")) {
int firstDigit = Character.getNumericValue(part.charAt(0));
for (int i = 1; i < part.length(); i++) {
int digit = Character.getNumericValue(part.charAt(i));
if ((digit + firstDigit) % 2 != 0) {
hasValidFormat = false;
break out;
}
}
}
}
return hasValidFormat ? "valid" : "invalid";
}
Tests:
List<String> data = Arrays.asList("13A357-22", "22X468-19", "222F444K555");
for (String test : data) {
System.out.println(test + " " + isValid(test));
}
Output:
13A357-22 invalid
22X468-19 valid
222F444K555 invalid
Answered By - Alex Rudenko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.