In: Computer Science
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
Code:
#asking for number of inputs
numInput=int(input("How many numbers you want to input: "))
sortedAsc=True #setting sorted by Ascending order as true
sortedDesc=True #setting sorted by descending order as true
#looping to take input
for i in range(numInput):
#taking next number
numInput=int(input("Input the number: "))
if i==0:#if number is first number then setting last number to
input
lastNum=numInput
#checking if input is greater than last number(condition for
Ascending order)
if lastNum<=numInput:
sortedAsc=sortedAsc and True #if condition is met then anding with
True
else:
sortedAsc=sortedAsc and False #if condition does not met then
anding with False, this makes final sortedAsc as false
if lastNum>=numInput:
sortedDesc=sortedDesc and True #if condition is met then anding
with True
else:
sortedDesc=sortedDesc and False #if condition does not met then
anding with False, this makes final sortedDesc as false
lastNum=numInput
#checking if sorted by either Ascending or descending
#and print True or False based on that
if sortedAsc or sortedDesc:
print("True")
else:
print("False")
Code Screenshot:
Output: