In: Computer Science
using python
1.
#Write a function called multiply_by_index.
multiply_by_index
#should have one parameter, a list; you may assume every item
#in the list will be an integer. multiply_by_index should
#return a list where each number in the original list is
#multipled by the index at which it appeared.
#
#For example:
#
#multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20]
#
#In the example above, the numbers 1, 2, 3, 4, and 5 appear
#at indices 0, 1, 2, 3, and 4. 1*0 = 0, 2 * 1 = 2, 3 * 2 = 6,
#and so on.
#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:
#[0, 2, 6, 12, 20]
#[0, 7, 14, 21, 28, 35, 42]
#[0, 7, 74, 195, 36, 0, 330]
print(multiply_by_index([1, 2, 3, 4, 5]))
print(multiply_by_index([7, 7, 7, 7, 7, 7, 7]))
print(multiply_by_index([14, 7, 37, 65, 9, 0, 55]))
2.
#Write a function called inside_search. inside_search
should
#have two parameters: a list of strings and a string to
search
#for. inside_search should return a list of all the indices
at
#which the string in the list contains the search string.
Note
#that the string at that index does not need to BE the search
#string, but rather must just contain it.
#
#For example:
#
#a_list = ["cat", "cats", "dog", "dogs", "catsup"]
#search_term = "cat"
#inside_search(a_list, search_term) -> [0, 1, 4]
#
#Note that the strings "cat", "cats", and "catsup" all
contain
#the search string "cat", and thus the result is their
indices:
#[0, 1, 4].
#
#Make sure the list you return is sorted from lowest index to
#highest.
#Add 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:
#[0, 1, 4]
#[2, 3]
#[1, 4]
#[4]
#[]
cats_and_dogs_list = ["cat", "cats", "dog", "dogs", "catsup"]
print(inside_search(cats_and_dogs_list, "cat"))
print(inside_search(cats_and_dogs_list, "dog"))
print(inside_search(cats_and_dogs_list, "cats"))
print(inside_search(cats_and_dogs_list, "sup"))
print(inside_search(cats_and_dogs_list, "aardvark"))
# Write a function called multiply_by_index. multiply_by_index # should have one parameter, a list; you may assume every item # in the list will be an integer. multiply_by_index should # return a list where each number in the original list is # multipled by the index at which it appeared. # # For example: # # multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20] # # In the example above, the numbers 1, 2, 3, 4, and 5 appear # at indices 0, 1, 2, 3, and 4. 1*0 = 0, 2 * 1 = 2, 3 * 2 = 6, # and so on. def multiply_by_index(lst): result = [] for i in range(len(lst)): result.append(i * lst[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: # [0, 2, 6, 12, 20] # [0, 7, 14, 21, 28, 35, 42] # [0, 7, 74, 195, 36, 0, 330] print(multiply_by_index([1, 2, 3, 4, 5])) print(multiply_by_index([7, 7, 7, 7, 7, 7, 7])) print(multiply_by_index([14, 7, 37, 65, 9, 0, 55]))
# Write a function called inside_search. inside_search should # have two parameters: a list of strings and a string to search # for. inside_search should return a list of all the indices at # which the string in the list contains the search string. Note # that the string at that index does not need to BE the search # string, but rather must just contain it. # # For example: # # a_list = ["cat", "cats", "dog", "dogs", "catsup"] # search_term = "cat" # inside_search(a_list, search_term) -> [0, 1, 4] # # Note that the strings "cat", "cats", and "catsup" all contain # the search string "cat", and thus the result is their indices: # [0, 1, 4]. # # Make sure the list you return is sorted from lowest index to # highest. def inside_search(lst, search_term): result = [] for i in range(len(lst)): if search_term in lst[i]: 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: # [0, 1, 4] # [2, 3] # [1, 4] # [4] # [] cats_and_dogs_list = ["cat", "cats", "dog", "dogs", "catsup"] print(inside_search(cats_and_dogs_list, "cat")) print(inside_search(cats_and_dogs_list, "dog")) print(inside_search(cats_and_dogs_list, "cats")) print(inside_search(cats_and_dogs_list, "sup")) print(inside_search(cats_and_dogs_list, "aardvark"))