The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
First Function
Write a function named 'swap' that is passed in a list and 2 index numbers (your function can assume these index numbers are between 0 and len() - 1, inclusive). Your function should swap the values at those index locations. Notice that the 'swap' function does not print anything itself, and it does not return a value. It's only job is to modify (change) the list passed in as a parameter.
For example:
stuff = [10, 20, 30, 40]
print(stuff)
swap(stuff, 0, 2)
print(stuff)
nums = [5, 8, 15, 12, 18]
print(nums)
swap(nums, 2, 3)
print(nums)
Would display:
[10, 20, 30, 40]
[30, 20, 10, 40]
[5, 8, 15, 12, 18]
[5, 8, 12, 15, 18]
Â
Â
After you write your function definition, write some test code to check that your function works properly.
Second Function
Write a function that is passed in a list of strings and returns a new list with all the strings of the original list with length 2 (the original list should not be changed). TIP: the built-in len() function can be used to determine the number of characters in a string.
For example:
stuff = ['this', 'is', 'a', 'list', 'of', 'strings', 'yo']
print(stuff)
others =sift_two(stuff)
print(others)
print(stuff)
Â
Would display:
['this', 'is', 'a', 'list', 'of', 'strings', 'yo']
['is', 'of', 'yo']
['this', 'is', 'a', 'list', 'of', 'strings', 'yo']
Â
Â
After you write your function definition, write some test code to check that your function works properly. When testing, include cases that confirm that the function can find a 2-character string at the beginning and end of the original list.
Submit your file with all the code in it (both function definitions and some test calls, all in one file).