Question

In: Computer Science

This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...

This is an intro to python question.

#Write a function called search_for_string() that takes two
#parameters, a list of strings, and a string. This function
#should return a list of all the indices at which the
#string is found within the list.
#
#You may assume that you do not need to search inside the
#items in the list; for examples:
#
# search_for_string(["bob", "burgers", "tina", "bob"], "bob")
# -> [0,3]
# search_for_string(["bob", "burgers", "tina", "bob"], "bae")
# -> []
# search_for_string(["bob", "bobby", "bob"])
# -> [0, 2]
#
#Use a linear search algorithm to achieve this. Do not
#use the list method index.
#
#Recall also that one benefit of Python's general leniency
#with types is that algorithms written for integers easily
#work for strings. In writing search_for_string(), make sure
#it will work on integers as well -- we'll test it on
#both.


#Write your code 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: [1, 4, 5]
sample_list = ["artichoke", "turnip", "tomato", "potato", "turnip", "turnip", "artichoke"]
print(search_for_string(sample_list, "turnip"))

Solutions

Expert Solution

# Write a function called search_for_string() that takes two
# parameters, a list of strings, and a string. This function
# should return a list of all the indices at which the
# string is found within the list.
#
# You may assume that you do not need to search inside the
# items in the list; for examples:
#
# search_for_string(["bob", "burgers", "tina", "bob"], "bob")
# -> [0,3]
# search_for_string(["bob", "burgers", "tina", "bob"], "bae")
# -> []
# search_for_string(["bob", "bobby", "bob"])
# -> [0, 2]
#
# Use a linear search algorithm to achieve this. Do not
# use the list method index.
#
# Recall also that one benefit of Python's general leniency
# with types is that algorithms written for integers easily
# work for strings. In writing search_for_string(), make sure
# it will work on integers as well -- we'll test it on
# both.


def search_for_string(lst, target):
    result = []
    for i in range(len(lst)):
        if lst[i] == target:
            result.append(i)
    return result


# 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: [1, 4, 5]
sample_list = ["artichoke", "turnip", "tomato", "potato", "turnip", "turnip", "artichoke"]
print(search_for_string(sample_list, "turnip"))


Related Solutions

In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Python question Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash...
Python question Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash and an integer called modulus. The function returns an integer that is the hash value of string_to_hash. This hash function processes the string in blocks of 4 characters (the final block may be shorter depending on the length of string_to_hash) and multiplies each character’s ASCII value by a weight. The weight depends on the position of the character in the block of 4 characters:...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Python: Implement a function called monthlyAmortization() that takes the following parameters: the principal amount of the...
Python: Implement a function called monthlyAmortization() that takes the following parameters: the principal amount of the loan (p) the interest rate, or annual rate (r) the term of the loan in months (t) the payment amount each period (monthlyPayment) The function prints the amortization table to a file called amoritizedLoan.txt. At the end of this file, the fucntion prints the total amount of interest paid for the loan. The output needs to be nicely formatted as shown below. The example...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
In Python, write a function called user_name that takes two arguments, first_name and last_name and creates...
In Python, write a function called user_name that takes two arguments, first_name and last_name and creates a “user name” consisting of the first three letters of the first name concatenated with the first three letters of the last name.   You can assume ach name is at least three letters long. Return the new user name.    your results should yield something like these test cases. >>> print(user_name("bob", "taft")) bobtaf >>> >>> print(user_name("Ralph", "Waldo")) RalWal >>>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT