Write a bash script to calculate the frequency of each word in a text file words.txt.
For simplicity sake, you may assume:
words.txt contains only lowercase characters and space ' ' 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:
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for word frequency involves counting how many times each word appears in a given text. We meticulously go through each word and compare it against every other word in the text. This ensures we don't miss any occurrences.
Here's how the algorithm would work step-by-step:
def word_frequency_brute_force(text):
words = text.split()
word_counts = {}
for first_word_index in range(len(words)):
current_word = words[first_word_index]
word_count = 0
# Iterate over all words in the list
# to compare with the current word
for second_word_index in range(len(words)):
comparison_word = words[second_word_index]
if current_word == comparison_word:
word_count += 1
# Store the count for the current word.
word_counts[current_word] = word_count
return word_countsThe task is to count how often each word appears in a given text. We can efficiently solve this by using a structure that quickly tells us how many times we've seen each word.
Here's how the algorithm would work step-by-step:
def word_frequency(text):
text = text.lower()
for punctuation in ".,!?:;":
text = text.replace(punctuation, "")
words = text.split()
word_counts = {}
# Iterate through each word in the cleaned text
for word in words:
# If word is not present, add the word
if word not in word_counts:
word_counts[word] = 1
# If word is present, increment count
else:
word_counts[word] += 1
# Present each word along with its frequency
for word, count in word_counts.items():
print(f'{word}: {count}')
return word_counts| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty dictionary or an appropriate error message as no words exist to count. |
| String with only whitespace characters | Treat as an empty string and return an empty dictionary or error. |
| Very large input string exceeding memory capacity | Consider using techniques like streaming or processing the input in chunks to avoid memory overflow. |
| String with extremely long words exceeding reasonable limits | Implement a word length limit and truncate or ignore excessively long words to prevent resource exhaustion. |
| Input string contains special characters, punctuation, and numbers | Preprocess the string by removing or normalizing these characters based on the problem's requirements (e.g., lowercase, remove punctuation). |
| Case sensitivity: 'The' vs 'the' | Convert the input string to lowercase to ensure case-insensitive word counting. |
| Different word delimiters: spaces, tabs, newlines | Split the string based on any whitespace character, treating them as delimiters. |
| High frequency of certain words skewing results | Consider using stop word removal (e.g., 'the', 'a', 'is') to focus on more meaningful words. |