Question

Modify the find() function to locate a string within another string def find (astring, achar): Find and return the index of a

0 0
Add a comment Improve this question Transcribed image text
Answer #0
def find(astring, achar):
    # ix represents index of astring
    ix = 0
    # iy represents index of achar
    iy = 0

    # Iterate loop till ix+iy is < less than length of astring
    while ix + iy < len(astring):
        # If character does not match, increment ix by 1
        # Re-set iy to 0
        if astring[ix + iy] != achar[iy]:
            ix += 1
            iy = 0
            continue

        # Otherwise, increment iy by 1 to check for next character in achar
        iy += 1

        # If iy equals length of achar means all character matched, return ix
        if iy == len(achar):
            return ix
    # Return -1
    return -1


print(find("abracadabra", "ra"))
print(find("abracadabra", "a"))
print(find("abracadabra", "ara"))
print(find("Hello", "lo"))

SCREENSHOT

FHindstring.py def find (astring, achar): # ix represents index of astring 1x = 0 # y represents index of achar ly = 0 2 3 #

OUTPUT

FindString E: \Practice\Pyth 2 3 Process finished

Add a comment
Know the answer?
Add Answer to:
Modify the find() function to locate a string within another string def find (astring, achar): Find...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or...

    def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or not "B" or not "C" or not "D" or not "E" or not "F" or not "G" or not "H" or not "I" or not "J"\ or not "K" or not "L" or not "M" or not "N" or not "O" or not "P" or not "Q" or not "R" or not "S" or not "T"\ or not "U" or not "V" or not...

  • Write two (2) overloaded functions that find a string in another string (first function) and find...

    Write two (2) overloaded functions that find a string in another string (first function) and find an integer in an integer array (second function). These should be value returning functions that return: For the string find, the string value if found, “not found” if the string is not found. For the integer file, the value if found or -1 if not found. The string function will pass in a string to find and the containing string, e.g. the request could...

  • The function takes three parameters: string array, int arraySize, and string word The function will loop...

    The function takes three parameters: string array, int arraySize, and string word The function will loop through the string array. If word is found in the array, return the matched element’s position (index) in the array. Otherwise, return -1. When a match is found in the array, the loop should be terminated at the time --- no need to continue the loop. If no match is found, the function should display all elements in the array, as well as the...

  • ##8. A program contains the following function definition: ##def cube(num): ##return num * num * num...

    ##8. A program contains the following function definition: ##def cube(num): ##return num * num * num ##Write a statement that passes the value 4 to this function and assigns its return value ##to the variable result. ##9. Write a function named times_ten that accepts a number as an argument. When the ##function is called, it should return the value of its argument multiplied times 10. ##10. Write a function named is_valid_length that accepts a string and an integer as ##arguments....

  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this function is to calculate the similarity measure between one data segment and the pattern. The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num =...

    LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num = word.find(letter) word - word[:num] + word[num+1:] i=0 i=i+1 return word 10 print remove_all_from_string("hello", "1") 5 points Status: Not Submitted Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. This time, the second string may be any length, including 0. Test your function on the strings "bananas" and "na"....

  • Problem 1 Write a find subroutine that can locate a specified 8-bit quantity in a null-terminated...

    Problem 1 Write a find subroutine that can locate a specified 8-bit quantity in a null-terminated string 8-bit quantities. (Remember that null terminated means "it ends with zero"). The subroutine is passed two parameters. The pointer to the array is passed in array X, the value to be found is passed in ACCA. The function should return the index of the first occurrence of the value in ACCB, or -1 (0xFF) if the value is not found. a) Write the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT