分析

写bash,不太会啊……

难度 中

来源

https://leetcode.com/problems/word-frequency/

题目

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.
  • 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?

解答

https://leetcode.com/problems/word-frequency/discuss/55443/My-simple-solution-(one-line-with-pipe)

 cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'

tr -s: truncate the string with target string, but only remaining one instance (e.g. multiple whitespaces)

sort: To make the same string successive so that uniq could count the same string fully and correctly.

uniq -c: uniq is used to filter out the repeated lines which are successive, -c means counting

sort -r: -r means sorting in descending order

awk '{ print $2, $1 }': To format the output, see here.

LeetCode 192. Word Frequency的更多相关文章

  1. LeetCode(192. Word Frequency)

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

  2. [leetcode shell]192. Word Frequency

    统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...

  3. 192 Word Frequency

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

  4. Word Frequency

    https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...

  5. [Bash]LeetCode192. 统计词频 | Word Frequency

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

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Individual Project - Word frequency program-11061171-MaoYu

    BUAA Advanced Software Engineering Project:  Individual Project - Word frequency program Ryan Mao (毛 ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. Python的网络编程 Socket编程

    Socket是进程间通信的一种方式,与其他进程间通信的一个主要不同是:能实现不同主机间的进程间通信,网络上各种各样的服务大多都是基于Socket来完成通信的,要解决网络上两台主机间的通信问题,首先要唯 ...

  2. 1050. [HAOI2006]旅行【并查集+枚举】

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...

  3. Day10 API

    String类 String是不可变类:值一旦确定了,就不会更改. public static void main(String[] args) { String s1 = "hello&q ...

  4. Selenium启动最新的火狐浏览器异常排查

    报错如下: WebDriverException: Message: 'Can\'t load the profile 打开谷歌浏览器和IE浏览器均正常 网上查阅资料,疑似与selenium版本相关联 ...

  5. C语言程序设计I—第十一周教学

    第十一周教学总结(12/11-17/11) 教学内容 第4章 循环结构-break continue嵌套循环 4.3 判断素数,4.4求1! + 2! + -. + 100! 课前准备 在蓝墨云班课发 ...

  6. mysql sqlite3 postgresql 简明操作

    安装 mysql $ sudo apt-get install mysql-server sqlite3 $ sudo apt-get install sqlite3 postgresql $ sud ...

  7. oracle快速学习

  8. CANVAS实现调色板 之 我的第一个随笔

    主题代码 <canvas id="color"></canvas> <script> var color=document.getElement ...

  9. Nivo Slider用法

    示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  10. 20155327 实验三 敏捷开发与XP实践

    20155327 实验三 敏捷开发与XP实践 实验内容 任务一 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装al ...