分析

写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. javascript:typeof与instanceof区别

    from:http://www.wxwdesign.cn/article/skills/javascript_typeof_instanceof.htm JavaScript中typeof和insta ...

  2. windows配置cider和clojure clj

    windows配置cider和clojure clj */--> pre.src {background-color: #292b2e; color: #b2b2b2;} windows配置ci ...

  3. jenkins 调用 k8s api

    delete_old_images() { #删除以前的镜像,节省本地空间 if docker images | grep $javaname ; then docker rmi -f `docker ...

  4. 【招聘123】Some good open positions

    Software Engineer III - Java, REST, Agile/Kanban https://jobs.cmegroup.com/jobs/3679794-software-eng ...

  5. 在不升级 mysql 的情况下直接使用 mysql utf8 存储 超过三个字节的 emoji 表情

    由于现在数据库的版本是5.5.2,但是看网上说要直接存储emoji表情,需要升级到5.5.3然后把字符集设置为utf8mb4,但是升级数据库感觉属于敏感操作. 考虑了多久之后直接考虑使用正则来替换,但 ...

  6. 【转】tomcat搭建本地服务器 实现apk更新下载

    转自:http://www.kankanews.com/ICkengine/archives/121748.shtml 做apk的更新下载功能,测试的时候需要个服务器. 所以就选用 Apache To ...

  7. 404 Note Found队Beta4

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组 ...

  8. Struts2 的ActionContext 详解

    转自:http://www.cnblogs.com/shaohz2014/p/3962779.html ActionContext是Action的上下文,Struts2自动在其中保存了一些在Actio ...

  9. hive--数据仓库

    1.1.1     hive是什么? Hive是基于 Hadoop 的一个数据仓库工具: 1.       hive本身不提供数据存储功能,使用HDFS做数据存储: 2.       hive也不分布 ...

  10. scala集合与java集合的转换应用

    今天在业务开发中遇到需要Scala集合转为Java集合的场景: 因为业务全部是由Scala开发,但是也避免不了调用Java方法的场景,所以将此记录下来加深记忆: import scala.collec ...