In: Computer Science
The elements of the list are going to be strings, and will be in order of strictly increasing or decreasing length. Some examples of lists in strictly increasing length order are [“pan”, “banana”,”xylophone”], and [“kayn”, “swain”, “morgana”].
Function Name: fix_string_list |
Parameter: data - A list with 3 elements (that are all lowercase strings) that is either in order of strictly increasing or decreasing length. |
Return: A list with the same 3 elements as the parameter that is in order of strictly increasing length. |
Description: Given a list of 3 strings that may be in order of either strictly increasing or decreasing length, return a list of 3 strings that are in order of strictly increasing length. |
Examples: Parameter: [“aloe”,”zebra”,”magnesium”] Returns: [“aloe”,”zebra”,”magnesium”] Parameter: [“Hydrogen”, “Carbon”, “Iron”] Returns: [“Iron”,”Carbon”,”Hydrogen”] |
PROGRAMMING LANGUAGE - PYTHON3
CODE -
def fix_string_list(data):
# Returning the reverse of the list if the strings are in order of
strictly decreasing length
if len(data[0]) > len(data[1]):
return data[::-1]
# Returning the original list if the strings are in order of
strictly increasing length
else:
return data
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.