Question

In: Computer Science

For these of string functions, write the code for it in C++ or Python (without using...

For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type.

1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str

Solutions

Expert Solution

PYTHON CODE:

#
#   Python program to find positions of a substring in the given string
#
def searchA(in_str, sub):
    # Empty list containing positions of substrings (if exists)
    pos = []
    # Search in_str
    for i in range(len(in_str)):
        matched = True
        # If in_str's character matches with first character of substring
        if in_str[i] == sub[0]:
            # Check if the rest of the characters matches or not
            for j in range(i + 1, len(sub) + i):
                if j >= len(in_str) or in_str[j] != sub[j - i]:
                    matched = False
                    break
            # If all characters matched then append the starting index of sub in in_str
            if matched:
                pos.append(i)
    # If substring was not found then append -1 to pos list
    if len(pos) == 0:
        pos.append(-1)
    return pos
in_str = "aaabbbabbababaab"
sub1 = "ab"
sub2 = "abbc"
print(searchA(in_str, sub1))
print(searchA(in_str, sub2))

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x...
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x + 4.
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s...
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s and a length 1 string c that is contained at least twice in s and returns the index of the second location of c. second_instance: Str Str -> Nat Requires: len(c) == 1 c occurs at least twice in s    Examples: second_instance("banana", "a") => 3 second_instance("bb", "b") => 1 - Write the body of a function make_list(n) which consumes a natural number n...
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. The string given by the user can be either 1 word or 1000 words. Must accept all ASCII characters. Please do not copy from the internet. This is my 3rd time posting the same question and I have not received a correct answer.
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT