Skip to content

Instantly share code, notes, and snippets.

@huzefamehidpurwala
Created December 4, 2022 02:55
Show Gist options
  • Save huzefamehidpurwala/0918e7f7f3c0c1d09de5427d50eaa314 to your computer and use it in GitHub Desktop.
Save huzefamehidpurwala/0918e7f7f3c0c1d09de5427d50eaa314 to your computer and use it in GitHub Desktop.
This Program sorts a string with lower-case letters before upper-case letter before numerical digits before other special characters
# github -> https://github.com/huzefamehidpurwala/Sorting-String-Python.git
s = input() # the alphanumeric string to be sorted
# defining empty lists to bifercate every index of the string respectively
strl_lst = []
stru_lst = []
num_lst = []
extra_lst = []
# iterate the string to every index
for i in s:
if i.isalpha(): # check the character is alphabatic
if i.islower(): # checks the alphabet is of lowercase
strl_lst.append(i)
else: # if the alphabet is not lowercase then definitely it is uppercase
stru_lst.append(i)
elif i.isnumeric(): # checks if the char is numeric
num_lst.append(int(i))
else: # if the char is neither alpha or numeric then definitely it is special char
extra_lst.append(i)
# sorts every numeric value then maps the sorted list to str to convert in string then converts the mapped object to list
num = list(map(str, sorted(num_lst, key=lambda x: [not x % 2, x])))
# final sorted concatenated list
final_lst = sorted(strl_lst) + sorted(stru_lst) + num + sorted(extra_lst)
# join each element of the concatenated list and print it
print("".join(final_lst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment