LeetCode 192. Word Frequency
分析
写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.txtcontains 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的更多相关文章
- LeetCode(192. Word Frequency)
192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- [leetcode shell]192. Word Frequency
统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...
- 192 Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- Word Frequency
https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...
- [Bash]LeetCode192. 统计词频 | Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- 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 ...
- Individual Project - Word frequency program-11061171-MaoYu
BUAA Advanced Software Engineering Project: Individual Project - Word frequency program Ryan Mao (毛 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [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 ...
随机推荐
- javascript:typeof与instanceof区别
from:http://www.wxwdesign.cn/article/skills/javascript_typeof_instanceof.htm JavaScript中typeof和insta ...
- windows配置cider和clojure clj
windows配置cider和clojure clj */--> pre.src {background-color: #292b2e; color: #b2b2b2;} windows配置ci ...
- jenkins 调用 k8s api
delete_old_images() { #删除以前的镜像,节省本地空间 if docker images | grep $javaname ; then docker rmi -f `docker ...
- 【招聘123】Some good open positions
Software Engineer III - Java, REST, Agile/Kanban https://jobs.cmegroup.com/jobs/3679794-software-eng ...
- 在不升级 mysql 的情况下直接使用 mysql utf8 存储 超过三个字节的 emoji 表情
由于现在数据库的版本是5.5.2,但是看网上说要直接存储emoji表情,需要升级到5.5.3然后把字符集设置为utf8mb4,但是升级数据库感觉属于敏感操作. 考虑了多久之后直接考虑使用正则来替换,但 ...
- 【转】tomcat搭建本地服务器 实现apk更新下载
转自:http://www.kankanews.com/ICkengine/archives/121748.shtml 做apk的更新下载功能,测试的时候需要个服务器. 所以就选用 Apache To ...
- 404 Note Found队Beta4
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组 ...
- Struts2 的ActionContext 详解
转自:http://www.cnblogs.com/shaohz2014/p/3962779.html ActionContext是Action的上下文,Struts2自动在其中保存了一些在Actio ...
- hive--数据仓库
1.1.1 hive是什么? Hive是基于 Hadoop 的一个数据仓库工具: 1. hive本身不提供数据存储功能,使用HDFS做数据存储: 2. hive也不分布 ...
- scala集合与java集合的转换应用
今天在业务开发中遇到需要Scala集合转为Java集合的场景: 因为业务全部是由Scala开发,但是也避免不了调用Java方法的场景,所以将此记录下来加深记忆: import scala.collec ...