From 7dea02ffaaa7e0f964e307d7ad17b3030a06d798 Mon Sep 17 00:00:00 2001 From: openset Date: Mon, 18 Feb 2019 12:25:07 +0800 Subject: [PATCH] Add: Word Frequency --- problems/word-frequency/README.md | 4 ++++ problems/word-frequency/word_frequency.bash | 5 +++++ problems/word-frequency/words.txt | 2 ++ 3 files changed, 11 insertions(+) create mode 100755 problems/word-frequency/word_frequency.bash create mode 100644 problems/word-frequency/words.txt diff --git a/problems/word-frequency/README.md b/problems/word-frequency/README.md index cb096378b..97be1df34 100644 --- a/problems/word-frequency/README.md +++ b/problems/word-frequency/README.md @@ -5,6 +5,10 @@ +[< Previous](https://github.com/openset/leetcode/tree/master/problems/number-of-1-bits "Number of 1 Bits") +                 +[Next >](https://github.com/openset/leetcode/tree/master/problems/valid-phone-numbers "Valid Phone Numbers") + ## 192. Word Frequency (Medium)

Write a bash script to calculate the frequency of each word in a text file words.txt.

diff --git a/problems/word-frequency/word_frequency.bash b/problems/word-frequency/word_frequency.bash new file mode 100755 index 000000000..fe817eebd --- /dev/null +++ b/problems/word-frequency/word_frequency.bash @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +# Read from the file words.txt and output the word frequency list to stdout. + +awk '{for(i=1;i<=NF;i++) a[$i]++} END {for(k in a) print k,a[k]}' words.txt | sort -k2 -nr diff --git a/problems/word-frequency/words.txt b/problems/word-frequency/words.txt new file mode 100644 index 000000000..18d52475f --- /dev/null +++ b/problems/word-frequency/words.txt @@ -0,0 +1,2 @@ +the day is sunny the the +the sunny is is