1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/problem/word-frequency.html

35 lines
1.0 KiB
HTML
Raw Normal View History

2022-03-29 15:21:05 +08:00
<p>Write a bash script to calculate the frequency of each word in a text file <code>words.txt</code>.</p>
<p>For simplicity sake, you may assume:</p>
<ul>
<li><code>words.txt</code> contains only lowercase characters and space <code>&#39; &#39;</code> characters.</li>
<li>Each word must consist of lowercase characters only.</li>
<li>Words are separated by one or more whitespace characters.</li>
</ul>
<p><strong>Example:</strong></p>
<p>Assume that <code>words.txt</code> has the following content:</p>
<pre>
the day is sunny the the
the sunny is is
</pre>
<p>Your script should output the following, sorted by descending frequency:</p>
<pre>
the 4
is 3
sunny 2
day 1
</pre>
<p><b>Note:</b></p>
<ul>
<li>Don&#39;t worry about handling ties, it is guaranteed that each word&#39;s frequency count is unique.</li>
<li>Could you write it in one-line using <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html">Unix pipes</a>?</li>
</ul>