Question

In: Computer Science

using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you...

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"))

Solutions

Expert Solution

# 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"))

Related Solutions

This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT