String Processing
9 questions · Fundamental Engineering
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array indexes start at 1.
The function isIsomorphic compares the two character arrays s1 and s2 that are given as arguments. Both s1 and s2 have one or more elements.
Two character arrays are considered isomorphic if the characters in s1 can be consistently replaced to obtain s2 without changing the order of characters. Each character in s1 must map to exactly one character in s2, and no two characters in s1 may map to the same character in s2. A character may map to itself.
If s1 and s2 do not have the same number of elements, the function returns false. Otherwise, it returns true if s1 and s2 are isomorphic and false if they are not.
The table lists examples of s1 and s2 given to the function isIsomorphic and the return values.
Table Examples of s1 and s2 given to the function isIsomorphic and the return values
Answer group
From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array indexes start at 1.
The function extractEmails receives text as an argument, extracts email addresses from the text, and returns all collected email addresses. Here, a string in the format “name@domain” is considered to be an email address, where “name” and “domain” comprise only alphanumeric characters and the characters “.” (dot), “_” (underscore), and “-” (hyphen-minus). The table shows the description of the functions used in the function extractEmails.
Table Functions used in the function extractEmails
extractEmails( returns {"john.. In addition, extractEmails( returns {"a@b", "b@c"}.
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
The function containsSubstring receives two-character arrays str and seq as arguments, and returns a boolean value indicating whether the character array str contains the sequence seq as a substring. The character array str contains seq as a substring if every sequence of consecutive characters in seq exists somewhere in the sequence of characters in str, in the same order. For example, containsSubstring( returns true, and containsSubstring( returns false. Assume that the number of elements in str and seq is one or more.
Answer group
From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array index starts at 1.
The function hammingDistance compares the two-character arrays s1 and s2 that are given as arguments. s1 and s2 have one or more elements. If s1 and s2 do not have the same number of elements, the function returns -1. Otherwise, it returns the number of indices where two arrays have different element values at the same index. The figure shows an example of two character arrays. APPLE and APRIE have different element values at two indices.
Figure Example of two-character arrays
The table lists examples of s1 and s2 given to the function hammingDistance and the return values. In the program, areas outside of the arrays must not be referenced.
Table Examples of s1 and s2 given to the function hammingDistance and the return values
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
Given two strings, str1 and str2, the task is to determine the length of the longest common subsequence (LCS), that is, the length of the longest subsequence present in both strings. For instance, if str1 is “ABXDZ” and str2 is “ABCD”, the LCS between str1 and str2 will be “ABD” and the length of the LCS will be 3.
The function lcs( takes two strings str1 and str2 and two integers m and n as arguments. str1 and str2 are the strings on which the length of the LCS is calculated. m and n are indexes addressing the target characters in str1 and str2, respectively. On the first call, m and n represent the lengths of str1 and str2, respectively. The function returns an integer containing the value of the length of the LCS of str1 and str2. For instance, the function may be called as lcs(.
Another function max( is also used. The function takes two integers a and b as arguments. It compares these two integers and returns the integer value of whichever is the maximum between a and b.
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
The function isPalindrome determines whether the character array s given as argument is a palindrome. Palindromes are words when read backward will still be the same such as “noon” and “madam.”
If character array s is a palindrome, it returns the value true and if not, the function returns false.
The table lists examples of s given to the function isPalindrome and the return values. In the program, areas outside of the arrays must not be referenced.
Table Examples of s given to the function isPalindrome and the return values
Answer group
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
Function hammingDistance receives two strings as argument and returns Hamming distance as the return value. Hamming distance is a metric used to measure the difference between two strings of equal length. It counts the number of positions at which the corresponding characters are different. For instance, the return value is 2 when the function hammingDistance( is called, because positions 2 and 5 have different characters.
It can also be extended to strings of different lengths by considering the unmatched characters as differences. For instance, the return value is 5 when the function hammingDistance( is called, because of two differences in first six characters and addition of three extra unmatched characters.
Answer group
From the answer group below, select the correct answer to be inserted into blank in the program.
A string of character(s) is called a palindrome if it reads the same forwards and backwards. Here the input string consists of only the uppercase Roman alphabet. As an example, the string "MADAM" is a palindrome as it remains the same when written backwards (right to left). The procedure isPalindrome receives a string str as a parameter and outputs whether the string str is a palindrome or not. The procedure should use the minimum number of iterations for any number of characters in the string. Note that division is performed for data type integer, that is, a ÷ b is the quotient of a divided by b.
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
N-grams are continuous sequences of words, symbols, or tokens in a document. N-grams of texts are extensively used in text mining and natural language processing tasks. An n-gram model is built by counting how often word sequences occur in corpus text and then estimating the probabilities. The figure shows an example of the unigrams, bigrams, and trigrams of the example sentence “ITPEC includes members from 6 countries.”
Figure Example of unigrams, bigrams, and trigrams
The procedure NGRAMS generates n-grams from text and outputs them. If the argument for n is 1, the procedure outputs the unigram result. If n is 2, the procedure outputs the bigram result and so on. The input text is a string of words separated by a space, so it is needed to split the string to generate n-grams. The table shows the description of the functions used in the program. In this question, the operator “+” is used for both arithmetic calculation of integer data type and concatenation of one or more strings into one string. In the program, areas outside of the array must not be referenced.
Table Functions
Answer group