Question

Python Implement function swapFL() that takes a list as input and swaps the first and last...

Python

Implement function swapFL() that takes a list as input and swaps the first and last ele- ments of the list. You may assume the list will be nonempty. The function should return the new list.

>>> ingredients = ['flour', 'sugar', 'butter', 'apples']

>>> swapFL(ingredients)

>>> ingredients

['apples', 'sugar', 'butter', 'flour']

Homework Answers

Answer #1


def swapFL(ingredients):
#get length of list
i=len(ingredients)
  
#swap fiirst and last value
temp=ingredients[0]
ingredients[0]=ingredients[i-1]
ingredients[i-1]=temp
ingredients = ['flour', 'sugar', 'butter', 'apples']

print("Before swap ",ingredients)

#function call
swapFL(ingredients)
print("After swap ",ingredients)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Implement function swap that takes a 2D list (a list of list of integers) and modifies...
Implement function swap that takes a 2D list (a list of list of integers) and modifies it in-place by swapping the first element in each row with the last element in that row. The return value is None. You may not use slicing. You maynot use any list method like append, insert, etc. Modification must be in-place, you may not move the list itself or any of its sublists to a new memory location. Examples: reverse([[1,2,3],[4,5,6]])        ->    [[3,2,1],[6,5,4]] reverse([[1,2,3,4],[5,6,7,8]])    ->    [[4,2,3,1],[8,6,7,5]]...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses the list. You should mutate the original list, without creating any new lists. Do NOT return anything. Do not use any built-in list functions such as reverse(). def reverse(lst):    """Reverses lst in place (i.e. doesn't create new lists).     >>> L = [1, 2, 3, 4]     >>> reverse(L)     >>> L     [4, 3, 2, 1]     """
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.
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 strings and the second...
Write a Python function that takes two parameters: the first a list 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.
In Python, implement a function that takes in two sorted lists and merges them into one...
In Python, implement a function that takes in two sorted lists and merges them into one list, the new list must be sorted. The function MUST run in O(m+n), where m is the length of list 1 and n the length of list 2. In other words, the function cannot do more than one pass on list 1 and list 2.
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT