String Processing
12 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
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 simRatio compares the two character-type arrays s1 and s2, each with one or more elements, that are given as arguments, and if their numbers of elements are equal, returns (the number of pairs of elements with the same index whose characters match ÷ the number of elements in s1) as a real number, as an index of how similar the sequences of the arrays are. For example, the return value is 1 when all elements of the arrays match, and 0 when none of the elements match.
If the numbers of elements of the two arrays are not equal, the function returns −1.
The table shows examples of s1 and s2 given to the function simRatio and the return values. In the program, areas outside the arrays must not be referenced.
Table Examples of s1 and s2 given to the function simRatio and the return values
Answer group
From the answer group below, select the correct answer to be inserted into blank in the program.
This is a program that, for any two different characters c1 and c2, calculates the ratio at which c2 appears immediately after c1 in the English words contained in a group of English words. English words consist of lowercase English letters only. When the last character of an English word is c1, that position is not included in the calculation of the ratio. For example, in the group of English words consisting of the four English words “importance,” “inflation,” “information,” and “innovation” shown in the figure, let c1 be “n” and c2 be “f”. Other than the last characters of the English words, there are five “n”s, and of these, two have “f” as the next character. Therefore, the ratio to be calculated is 2 ÷ 5 = 0.4. When the sequence of c1 and c2 never appears, the ratio is defined as 0 regardless of the number of occurrences of c1.
Figure Example of a group of English words consisting of four words
In the program, the group of English words is stored in the Words-type global variable words. The table shows an explanation of the methods of the class Words. In this question, the operator “+” for strings represents the concatenation of strings. Also, the operator “÷” for integers performs the calculation as real numbers.
Table Explanation of the methods of the class Words
Answer group
From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array index starts at 1.
The function search receives two character-type arrays as the arguments data and key respectively, searches data for all the sequences that are the same as the sequence of the elements of key, and returns an array that stores all the indexes of the first elements of those sequences. If none is found, it returns an array with 0 elements.
When the function search is called as search(, the number of times the conditional expression on the line /*** β ***/ is true is blank.
Answer group