Question

In: Computer Science

Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...

Using Python

#Write a function called after_second that accepts two
#arguments: a target string to search, and string to search
#for. The function should return everything in the first
#string *after* the *second* occurrence of the search term.
#You can assume there will always be at least two
#occurrences of the search term in the first string.
#
#For example:
# after_second("11223344554321", "3") -> 44554321
#
#The search term "3" appears at indices 4 and 5. So, this
#returns everything from the index 6 to the end.
#
# after_second("heyyoheyhi!", "hey") -> hi!
#
#The search term "hey" appears at indices 0 and 5. The
#search term itself is three characters. So, this returns
#everything from the index 8 to the end.
#
#Hint: This may be more complicated than it looks! You'll
#have to look at the length of the search string and
#either modify the target string or take advantage of the
#extra arguments you can pass to find().


#Write your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print 44554321 and hi!, each on their own line.
print(after_second("11223344554321", "3"))
print(after_second("heyyoheyhi!", "hey"))

Solutions

Expert Solution


def after_second(s,target):

    # finding first occurrence of target string
    first_ind = s.find(target)

    # finding second occurrence of target string
    second_ind = s.find(target, first_ind+1)

    # if second_ind is -1 then there is no
    # second occurrence of target it returns empty
    if second_ind == -1:
        return ""

    # Example:
    #       s = heyyoheyhi! , target = hey
    #       first_index     = 0
    #       second_index    = 5
    #       we have to remove the target string
    #       so second index + length of target string
    #       gives the remaining text
    return s[second_ind+len(target):]


print(after_second("11223344554321", "3"))
print(after_second("heyyoheyhi!", "hey"))



Output:


Related Solutions

C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string....
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string. The function should return a greeting message based on the hour given in the time object as follow: If the time is before noon: 'Good Morning.', if it is in the afternoon: 'Good Day.' Please use the following template for your python script to define the greetings() function and name it as mt_q3.py. Replace the place holder [seneca_id] with your Seneca email user name....
Write a function called format_name that accepts a string in the format of first name followed...
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. For example, format_name("Jared Smith") should return "Smith, Jared" Hint: The following String Methods will be useful: # Returns the lowest index in the string where substring is # found. Returns -1 if...
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index...
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index (n). The function will: 1.Remove the n-th characterof str1 (the initial character is the 0-th character) 2.Swap the order of the substring in front of the removed character and the substring afterwards, and concatenate them as a new string 3.Return the converted (str2) back to the function caller For example, myfunc(“abc1xyz”, 3) returns “xyzabc”. Additional Features [6 pts] Expand the function by adding more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT