Question
NOTE: I can't use the map / zip or lambda functions, last time someone answered this they used them. Define a function named get_sorted_in_parallel(tuples1, tuples2)
NOTE: I can't use the map / zip or lambda functions, last time someone answered this they used them.
Define a function named get_sorted_in_parallel(tuples1, tuples2) which takes two tuples of integers as parameters. The tuples have the same length and their elements are related, i.e. the element at each index of the second parameter tuple corresponds to the element at the same index in the first parameter tuple, e.g. the element at index 1 in the first tuple corresponds to the element at index 1 in the second tuple. The function sorts the first parameter tuple, keeping the elements of the second parameter tuple in the order corresponding to the sorted first parameter tuple. The function returns a tuple made up of the sorted first parameter tuple and the new corresponding second tuple. For example, the following code fragment:
tuple1 = (4, 1, 3) tuple2 = (9, 8, 6) tuple3, tuple4 = get_sorted_in_parallel(tuple1, tuple2) print(tuple1, "-", tuple2) print(tuple3, "-", tuple4)
prints:
(4, 1, 3) - (9, 8, 6) (1, 3, 4) - (8, 6, 9)
4 and 9 are connected, 1 and 8 are connected, 3 and 6 are connected. The function sorts the first tuple, so 1 is the first element in the first tuple, so 8 will be the first element in the second tuple. 3 is the second element in the first tuple, so 6 will be the second element in the second tuple. 4 is the last element in the first tuple, so 9 will be the last element in the second tuple.
Note: you can assume that the tuple1 parameter contains unique elements (i.e. no duplicates).
For example:
Test | Result |
---|---|
tuple1 = (5, 6, 2, 1, 3) tuple2 = (3, 8, 9, 6, 7) tuple3, tuple4 = get_sorted_in_parallel(tuple1, tuple2) print(tuple1, "-", tuple2) print(tuple3, "-", tuple4) | (5, 6, 2, 1, 3) - (3, 8, 9, 6, 7) (1, 2, 3, 5, 6) - (6, 9, 7, 3, 8) |
tuple1 = (3, 8, 4, 9, 1, 2, 7) tuple2 = (43, 24, 76, 12, 9, 23, 14) tuple3, tuple4 = get_sorted_in_parallel(tuple1, tuple2) print(tuple1, "-", tuple2) print(tuple3, "-", tuple4) | (3, 8, 4, 9, 1, 2, 7) - (43, 24, 76, 12, 9, 23, 14) (1, 2, 3, 4, 7, 8, 9) - (9, 23, 43, 76, 14, 24, 12) |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started