Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 6, 2022 09:09 am GMT

Reorder Data in Log Files

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.

There are two types of logs:

Letter-logs: All words (except the identifier) consist of lowercase English letters.
Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:

The letter-logs come before all digit-logs.
The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
The digit-logs maintain their relative ordering.
Return the final order of the logs.

class Solution:    def reorderLogFiles(self, logs: List[str]) -> List[str]:        def get_key(log):            _id, rest = log.split(" ", maxsplit=1)            return (0, rest, _id) if rest[0].isalpha() else (1, )        return sorted(logs, key=get_key)

Original Link: https://dev.to/salahelhossiny/reorder-data-in-log-files-17k8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To