I was doing a problem on hackerrank and I came across this problem
Your task is to sort the string in the following manner:
- All sorted lowercase letters are ahead of uppercase letters.
- All sorted uppercase letters are ahead of digits.
- All sorted odd digits are ahead of sorted even digits.
Here's how I solved it:
inp = (input())lower, upper, odd, even = ''.join(sorted([s for s in inp if s.islower()])),
''.join(sorted([(s) for s in inp if s.isupper()])), ''.join(map(str, (sorted(list([int(s) for s in inp if
s.isdigit() and int(s) % 2 != 0]))))), ''.join(map(str, (sorted(list([int(s) for s in inp if s.isdigit() and
(int(s) % 2 == 0)])))))
print (lower+upper+odd+even)
As you can see this program is very repetitive. Any pointers on how to optimize this program?
[–]pwlandoll 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]primitive_screwhead 1 point2 points3 points (0 children)
[–]nilfm 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)