AMS 209: HW 4: Problem 3ΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# coding: utf8

'''
Created on October 31st, 2017

@author: Geetanjali
'''

def is_sorted(arr_list):
	"""
	:param arr_list: the list to be checked if it is sorted
	:return flag: is set to True if arr_list is sorted, else is set to False
	"""

	#We compare pairwise all the adjacent elements in the list to check if the list is sorted. 
	for i in range(0, len(arr_list)-1):
		if(arr_list[i] <= arr_list[i+1]):
			flag = True
		else:
			flag = False
			break	
	return flag


if __name__ == '__main__':

	arr_list = []

	print("Enter a list: (just enter a non-alphanumeric character to stop)")
	flagDigit = False
	while(True):
		user_input = input()
		if(user_input.isalpha()):
			arr_list.append(user_input)
			print("You started with a list of letters. Make sure you only enter letters and not digits. (just enter a non-alphanumeric character to stop)")
		elif(user_input.isdigit()):
			arr_list.append(int(user_input))
			print("You started with a list of numbers. Make sure you only enter numbers and not letters. (just enter a non-alphanumeric character to stop)")
		else:
			break
	print("Is your list sorted? : "+str(is_sorted(arr_list)))