-
Notifications
You must be signed in to change notification settings - Fork 2
/
Case-specific Sorting of Strings.py
43 lines (32 loc) · 1.3 KB
/
Case-specific Sorting of Strings.py
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
43
class Solution:
#Function to perform case-specific sorting of strings.
def caseSort(self,s,n):
upper=[]
lower=[]
#storing uppercase and lowercase characters in two separate lists.
for char in s:
if(self.Case(char)):
upper.append(char)
else:
lower.append(char)
#sorting both the lists.
upper.sort(reverse=True)
lower.sort(reverse=True)
sorted_string=""
#iterating over the given string.
for char in s:
#if current character is in uppercase then we pick character from
#the list containing uppercase characters and add it to result.
if(self.Case(char)):
sorted_string+=upper.pop()
#else we pick the character from the sorted list
#containing lowercase characters and add it to result.
else:
sorted_string+=lower.pop()
#returning the result.
return sorted_string
#Function to return 1 if character is in upper case and 0 for lower case.
def Case(self,c):
if(ord(c)<=ord('Z') and ord(c)>=ord('A')):
return 1
return 0