192. Word Frequency
Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt
.
For simplicity's sake, you may assume:
words.txt
contains only lowercase characters and space' '
characters.- Each word must consist of lowercase characters only.
- Words are separated by one or more whitespace characters.
Example:
Assume that words.txt
has the following content:
the day is sunny the the
the sunny is is
Your script should output the following, sorted by descending frequency:
the 4
is 3
sunny 2
day 1
Note:
- Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
- Could you write it in one line using Unix pipes?
Solution
tr ' ' '\n' < words.txt | grep -E '^[a-z]+$' | sort | uniq -c | sort -nr | sed -E 's/ +(.*) (.*)/\2 \1/'
Idea:
tr => (translate) from to here ' ' to '\n' (space to new line)
< words.txt => for input
grep -E => Grep by pattern (grep char / string)
sort => Sort
uniq -c => count uniq char
sort -nr => again sort based on count
sed -E => it can perform lots of functions on file like searching, find and replace, insertion or deletion
That’s all folks! In this post, we solved LeetCode problem #192. Word Frequency
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to solve leetcode questions & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment