ginortS in python - HackerRank Solution
Problem :
S contains alphanumeric characters only.
Your task is to sort the string S 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.
Input Format :
A single line of input contains the string S.
Constraints :
- 0 < len(S) < 1000
Output Format :
Output the sorted string S.
Sample Input :
Sorting1234
Sample Output :
ginortS1324
Solution :
1 2 3 4 5 6 7 8 | # ginortS in python - Hacker Rank Solution # Python 3 # Enter your code here. Read input from STDIN. Print output to STDOUT # ginortS in python - Hacker Rank Solution START print(*sorted(input(), key=lambda c: (c.isdigit() - c.islower(), c in '02468', c)), sep='') # ginortS in python - Hacker Rank Solution END |
Note : I have not confidant on following solutions is correct or wrong. this solutions are given from some on else site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # ginortS in python - Hacker Rank Solution # Python 2 # ginortS in python - Hacker Rank Solution START from __future__ import print_function def func(x): if x.isalpha(): if x.isupper(): return (ord(x)-ord('A')) else: return (ord(x)-ord('a'))-30 else: if int(x) % 2 == 0: return 60+int(x) else: return 30+int(x) s = raw_input() map(lambda x: print(x,end=''),(sorted(s,key = func))) # ginortS in python - Hacker Rank Solution END |
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 | # ginortS in python - Hacker Rank Solution # Python 2 # # ginortS in python - Hacker Rank Solution START from __future__ import print_function upper = [] lower = [] even = [] odd = [] def separator(a): if a.isalpha(): if a.isupper(): upper.append(a) else: lower.append(a) else: if int(a)%2 == 0: even.append(a) else: odd.append(a) return map(separator,raw_input()) upper.sort() lower.sort() even.sort() odd.sort() t = lower+upper+odd+even map(lambda x: print(x,end=''),t) # ginortS in python - Hacker Rank Solution END |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
import re
S= input()
series="[a-z]","[A-Z]","[13579]","[02468]"
print("".join(map(lambda x: "".join(sorted(re.findall(x, S))),series)))
Note: Sorry for my bad england