- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reorder Data in Log Files in Python
Suppose we have an array of logs. In that array each entry is a space delimited string of words. The first word in each log is an alphanumeric identifier. Then, there are different types of strings like below −
- Each word after the id will consist only of lowercase letters;
- Each word after the id will consist only of digits.
We will call these two types of logs as letter-logs and digit-logs respectively. And ti is guaranteed that each log has at least one word after its id.
We have to reorder the logs so that all of the letter-logs stays before any digit-log. And the letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. Finally, the digit-logs should be put in their original order. So we have to return the final order of the logs.
So, if the input is like logs = ["dig1 9 2 5 2","let1 art can","dig2 4 8","let2 own kit dig","let3 art zero"], then the output will be ["let1 art can","let3 art zero","let2 own kit dig","dig1 9 2 5 2","dig2 4 8"]
To solve this, we will follow these steps −
- words := a new list
- nums := a new list
- for each log in logs, do
- s := a list of words of log
- if second word is a digit, then
- insert log at the end of nums
- join each element of s by separating with spaces and insert into words array at the end
- words = Then sort the words array lexicographically
- words := join each element of words array by separating with spaces and make a list of strings
- merge two lists words and nums, then return
Let us see the following implementation to get better understanding −
class Solution: def reorderLogFiles(self, logs): words =  nums =  for log in logs: s = log.split() if s.isdigit(): nums.append(log) else: words.append((s, ' '.join(s[1:]))) words = sorted(words, key=lambda x: (x,x)) words = [' '.join(w) for w in words] return words + nums ob = Solution() print(ob.reorderLogFiles(["dig1 9 2 5 2","let1 art can","dig2 4 8","let2 own kit dig","let3 art zero"]))
["dig1 9 2 5 2","let1 art can","dig2 4 8","let2 own kit dig","let3 art zero"]
['let1 art can', 'let3 art zero', 'let2 own kit dig', 'dig1 9 2 5 2', 'dig24 8']
- Log functions in Python
- Python Pandas CategoricalIndex - Reorder categories
- Log functions in Python Program
- How to reorder the columns in an R data frame?
- Reorder List in C++
- Python – Reorder for consecutive elements
- How to reorder the row indices in an R data frame?
- Design Log Storage System in Python
- How to process excel files data in chunks with Python?
- Path for log based data backups in SAP HANA
- Python - Write multiple files data to master file
- How to share common data among multiple Python files?
- How to make a log histogram in Python?
- Checking data disk, log disk and Trace disk in SAP HANA