Question

In: Computer Science

Problem 2 Write a function (call it by your first name e.g. abc_trim) that takes a...

Problem 2

Write a function (call it by your first name e.g. abc_trim) that takes a list as argument , modifies it by removing the first and last two elements, and returns the new modified list e.g. if we call the function as katline_trim([1,4,6,8,11, 15]), we should get [4,6,8]. (Hint: Look at how we refer to list elements by their index and also how we use a negative number as index. You can also use a loop with the len() method

NB: Include python code, please. use spider

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:
Note: If the below code is missing indentation please refer code Images

Typed Code:

# Creating a function named abc_trim
# with input parameter a List
def abc_trim(List):
# removing the first element
# using index 0 points to the first element
List.pop(0)
# removing the second last element
# using negative index -2 points to the second last element
List.pop(-2)
# removing the last element
# using negative index -1 points to the last element
List.pop(-1)
# returning the trimmed List
return List

# A List of integers
List = [1,4,6,8,11, 15]
# calling function and printing the returned List
print("Trimmed List:",abc_trim(List))
#code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Put your name on the first line in camelCase. Write a JavaScript function to calculate the...
Put your name on the first line in camelCase. Write a JavaScript function to calculate the revenue for blocks sales. Input the number of wooden blocks, the number of plastic blocks and the number of rubber blocks. Wooden blocks are $5. Plastic blocks are $2 and rubber blocks are $4. Calculate the cost for each type of block. Calculate the total cost. Display the four outputs on the page. Label the inputs and the output on your page. Change the...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
Write a function name randintlist which takes three parameters: 1.minimum 2.maximum 3.length The randintlist function should...
Write a function name randintlist which takes three parameters: 1.minimum 2.maximum 3.length The randintlist function should return a list of random int values (using random.randint) where the minimum and maximum values of those random numbers are the argument values for the first two parameters, and the length of the list(count of random integers ) is given by the argument value of the third parameter For the list of random int values returned by randintlist no two values should be equal...
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 function that takes a person’s first and last names as input and (a) uses...
Write a function that takes a person’s first and last names as input and (a) uses lists to return a list of the common letters in the first and last names (the intersection). (b) uses sets to return a set that is the intersection of the characters in the first and last names. (c) uses sets to return the set that is the symmetric difference between the first and last names. please write in python program
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT