Question

Write a function text_filter that takes two lists of characters and checks to see if all...

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. *)

Homework Answers

Answer #1

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));
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a LISP function that accepts two lists as it's only arguments. The function should return...
Write a LISP function that accepts two lists as it's only arguments. The function should return a new list with the arguments as it's elements. So, if you pass the function (a b c) and (1 2 3), the function should return ((a b c) (1 2 3)).
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers...
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers representing vectors, and produces their dot product (or reports their incompatibility). E.g., (dotProduct '(1 2) '(3 4) ) -> 11 (dotProduct '(1 2 3) '(4 5 6)) -> 32 (dotProduct '(1 2 3) '(4 5)) -> *incompatible*
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M,...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. Example: intersect([3, 7, 9, 12, 14], [1, 2, 5, 7, 10, 11, 12]) => [7, 12] Hint: As the title hints at, you are...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle....
I'm trying to write a solver that can find a single solution to a cryptarithmetic puzzle. I need help writing the other function from this code. /* ExhaustiveSolve * --------------- * This is the "not-very-smart" version of cryptarithmetic solver. It takes * the puzzle itself (with the 3 strings for the two addends and sum) and a * string of letters as yet unassigned. If no more letters to assign * then we've hit a base-case, if the current letter-to-digit...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT