Write a program to check if the elements in the first half of a tuple are sorted in
descending order or not.
Answers
Answer:
==>> Algorithm:
- Store the first k elements in an array and sort that in ascending order.
- Store the remaining n-k elements in an array and sort that in decending order.
- Merge the two arrays by adding the elements from second array in reverse order.
Answer: - We need to write a program to check if the elements in the first half of a tuple are sorted in descending order or not. The program is written below.
The program: -
if firsthalf_tuple == tuple(sorted(firthalf_tuple, reverse=True)):
# Order is Descending
elif firsthalf_tuple == tuple(sorted(firsthalf_tuple)):
# Order is not in descending order
else:
# No order
OR you can use this program: -
# function to check if the tuple is sorted in descending order or not
def is_tuple_sorted(t):
for i in range(1, len(t)):
# return FALSE if the element is greater than the previous element
if t[i] > t[i-1]:
return FALSE
return TRUE
# Create a tuple
t = (6, 5, 4, 3, 2, 1)
# check if the tuple is sorted
print(is_tuple_sorted(t))
The output of this program will be TRUE.
- The for loop will be executed and then if loop will be executed after that if the loop is true then it will print as true and if the loop is false then it will print false.
To know more about the topic, visit the below links: -
https://brainly.in/question/35863260
https://brainly.in/question/24913885
#SPJ3