This is one full question. Please write in Python.
2. (6 points) Within ciphers.py, write a function swap(x, i, j) that returns a version of the string x with the characters at indices i and j swapped. If either index is invalid (i.e., something besides 0, 1, 2, ... , len(x) - 1), the function should return None. (To keep things simple here, we'll disregard the fact that Python technically allows negative indices.) The function should work regardless of the order in which i and j are supplied. Do not call any built-in Python functions except len() and range() in your solution. String slicing will be very useful... Here are some example arguments for this function and their expected return values: ។ Arguments ('sloth', 0, 1) ('sloth', 1, 0) ('sloth', 1, 4) ('sloth’, 4, 1) ('sloth’, 4, 4) ('sloth', 4, 5) Return Value 'Isoth' 'lsoth' 'shotl' 'shotl' 'sloth None
This is one full question. Please write in Python.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am