B.Sc Computer Science with Data Analytics
Python Programming - Lab
Core Lab 5
Bharathiar University
Program 10- Write a python program to sort a given sequence: String, List and Tuple.
SOURCE CODE:
# Function to sort a sequence (string, list, or tuple)
def sort_sequence(seq):
if isinstance(seq, str):
# If input is a string, convert it to a list of characters
s = ''.join(sorted(seq))
elif isinstance(seq, (list, tuple)):
# If input is a list or tuple, sort it directly
s = sorted(seq)
else:
return "Invalid input. Please provide a string, list, or tuple."
return s
# read input from user
input_sequence = input("Enter a string, list, or tuple to sort: ")
sorted_sequence = sort_sequence(input_sequence)
print("Sorted sequence:", sorted_sequence)
OUTPUT:
SORTING STRING
Enter a string, list, or tuple to sort: hello
Sorted sequence: ehllo
SORTING LIST
Enter a string, list, or tuple to sort: [3,1,4,2,5]
Sorted sequence: [1,2,3,4,5]
SORTING TUPLE
Enter a string, list, or tuple to sort: (9,2,7,1,5)
Sorted sequence: (1,2,5,7,9)
No comments:
Post a Comment