Write a function text_filter that takes two lists of characters and checks to see if all the characters in the first list are included in the second list AND in the same order, BUT possibly with other characters in between.
Done in OCAML
For example text_filter ['a';'m';'z'] ['1';'a';'2';'m';'3';'z'] = true text_filter ['a';'m';'z'] ['1';'a';'3';'z'] = false text_filter ['a';'m';'z'] ['1';'z';'2';'m';'3';'a'] = false
let rec text_filter (xs:char list) (ys:char list) : bool = *)
(* Problem 3b. Rewrite the function above so that is is polymorphic, i.e., it should work on lists whose elements are any types. Give at least one test case (call your function at least once) with a type that is different from chars. *)
i have included 2 test cases here
in test case one the characters in first list are included in
the second list
in second test case the characters in second list contain the
characters but in different order
public class HelloWorld{
public static boolean RegEx(char array1[], char array2[]){
int flag=-1;
for(int i=0;i<array1.length;i++){
for(int j=flag+1;j<array2.length;j++){
if(array1[i]==array2[j]){
flag=j;
if(i==array1.length-1){
return true;
}
break;
}
if(j==array2.length-1){
return false;
}
}
}
return false;
}
public static void main(String []args){
//char array1[] = {'a','b','c'};
//char array2[] = {'1','a','2','b','3','c'};
char array1[] = {'b','a','c'};
char array2[] = {'1','a','2','b','3','c'};
System.out.println(RegEx(array1,array2));
}
}
Get Answers For Free
Most questions answered within 1 hours.