ginortS HackerRank Solution

This Python program takes a string as input and sorts it based on a custom order. The order is defined as follows: all sorted lowercase letters are ahead of uppercase letters, all uppercase letters are ahead of digits, and all odd digits are ahead of even digits.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the ginortS HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by using Python's built-in 'sorted' function with a custom sorting key. The key is a tuple where each element represents a condition for sorting. The conditions are checked in order, and the first one that differs will determine the order. The conditions are: whether the character is an even digit, whether it's a digit, whether it's an uppercase letter, whether it's a lowercase letter, and finally the character itself. This ensures the desired custom order.


s = input()
print(
    "".join(
        sorted(
            s,
            key=lambda x: (
                x.isdigit() and int(x) % 2 == 0,
                x.isdigit(),
                x.isupper(),
                x.islower(),
                x,
            ),
        )
    )
)

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform