2020
2121
2222
23- def memorySort (lst : list , mode : str = 'i' , type = int ) -> list :
23+ def memorySort (lst : list , mode : str = '' ) -> list :
2424 """Sorts the inputted list in ascending order utilizing memory to speed up computation time
2525
2626 Args:
@@ -30,25 +30,27 @@ def memorySort(lst: list, mode: str = 'i',type = int) -> list:
3030 'i' or 'int' = (default) list containing integers
3131 'n' or 'None' = list containing integers with no repeats
3232 'c' or 'chr' = list containing characters
33-
34- type:
35- int = (default) list containing integers
33+ 's' or 'str' = list containing strings
3634
3735
3836
3937 Returns:
4038 list: sorted list
4139 """
42- if mode == 'i ' :
43- if isinstance (type , int ):
40+ if mode == '' :
41+ if isinstance (lst [ 0 ] , int ):
4442 return memorySortI (lst )
43+ if isinstance (lst [0 ], str ):
44+ return memorySortS (lst )
4545
46- if mode .lower () == 'i' or mode .lower () == 'int' :
46+ if mode .lower () == 'i' or mode .lower () == 'int' or mode . lower () == '' :
4747 return memorySortI (lst )
4848 if mode .lower () == 'n' or mode .lower () == 'none' :
4949 return memorySortN (lst )
5050 if mode .lower () == 'c' or mode .lower () == 'chr' :
5151 return memorySortC (lst )
52+ if mode .lower () == 's' or mode .lower () == 'str' :
53+ return memorySortS (lst )
5254 return None
5355
5456
@@ -111,6 +113,7 @@ def memorySortN(lst: list) -> list:
111113 return output
112114
113115
116+
114117def memorySortC (lst : list ) -> list :
115118 """Sorts the inputted list of character elements in ascending order utilizing memory to speed up computation time
116119
@@ -143,3 +146,57 @@ def memorySortC(lst: list) -> list:
143146 p : int = p + 1
144147
145148 return output
149+
150+
151+
152+ def memorySortS (lst : list ) -> list :
153+ """Sorts the inputted list of string elements in ascending order utilizing memory to speed up computation time
154+
155+ Args:
156+ lst (list): List of string elements to get sorted
157+
158+ Returns:
159+ list: sorted list of string elements
160+ """
161+
162+ high : int = 0
163+ for l in lst [0 ]:
164+ high = high * 128 + ord (l )
165+ low : int = high
166+
167+ for l in lst :
168+ temp : int = 0
169+ for i in l :
170+ temp = temp * 128 + ord (i )
171+
172+ if temp > high :
173+ high : int = temp
174+ elif temp < low :
175+ low : int = temp
176+
177+
178+ ran : int = high - low + 1
179+ amount : list = [0 ] * ran
180+
181+ for l in lst :
182+ temp : int = 0
183+ for i in l :
184+ temp = temp * 128 + ord (i )
185+ amount [temp - low ] += 1
186+
187+ output : list = ['' ] * len (lst )
188+ p : int = 0
189+ for o in range (len (amount )):
190+ if amount [o ] != 0 :
191+ i = o + low
192+ out = ''
193+ while i != 0 :
194+ letter = chr (i % 128 )
195+ i = i // 128
196+ out = letter + out
197+
198+ for _ in range (amount [o ]):
199+ output [p ] = out
200+ p : int = p + 1
201+
202+ return output
0 commit comments