From b40bc1e855517ce72d595e33b58d1c3be2f53547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=B8=E7=94=B1?= Date: Fri, 16 Jul 2021 11:04:37 +0800 Subject: [PATCH] =?UTF-8?q?shell=E4=BB=A5=E5=8F=8Asql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/q0192.sh | 48 +++++++++++++++++++++++++++++++++++++++++ shell/q0193.sh | 35 ++++++++++++++++++++++++++++++ shell/q0194.sh | 34 +++++++++++++++++++++++++++++ shell/q0195.sh | 38 +++++++++++++++++++++++++++++++++ sql/q0175.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ sql/q0176.md | 49 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 shell/q0192.sh create mode 100644 shell/q0193.sh create mode 100644 shell/q0194.sh create mode 100644 shell/q0195.sh create mode 100644 sql/q0175.md create mode 100644 sql/q0176.md diff --git a/shell/q0192.sh b/shell/q0192.sh new file mode 100644 index 0000000..ecfad8f --- /dev/null +++ b/shell/q0192.sh @@ -0,0 +1,48 @@ +# 192.统计词频 +# +# [原题链接](https://leetcode-cn.com/problems/word-frequency/) +# +# 2021-07-16 09:23:44 +# +# 写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。 +# +# 为了简单起见,你可以假设: +# +# +# words.txt只包括小写字母和 ' ' 。 +# 每个单词只由小写字母组成。 +# 单词间由一个或多个空格字符分隔。 +# +# +# 示例: +# +# 假设 words.txt 内容如下: +# +# the day is sunny the the +# the sunny is is +# +# +# 你的脚本应当输出(以词频降序排列): +# +# the 4 +# is 3 +# sunny 2 +# day 1 +# +# +# 说明: +# +# +# 不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。 +# 你可以使用一行 Unix pipes 实现吗? +# +# Related Topics Shell +# +# 👍 149 👎 0 + +# 0ms +cat words.txt | awk -F '[ ]' '{for(i=1;i<=NF;i++){if($i!=""){a[$i]++}}}END{for(i in a){print i,a[i]| "sort -nr -k2"}}' + +# 4ms +tr -s ' ' '\n'