Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0192 - Word Frequency

LeetCode 0192 - Word Frequency

作者头像
Reck Zhang
发布于 2021-08-11 06:54:20
发布于 2021-08-11 06:54:20
30500
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Word Frequency

Desicription

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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
the day is sunny the the
the sunny is is

Your script should output the following, sorted by descending frequency:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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?

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2, $1}'
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 0126 - Word Ladder II
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
Reck Zhang
2021/08/11
1780
LeetCode 0212 - Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Reck Zhang
2021/08/11
2000
​LeetCode刷题实战192:统计词频
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/03/04
7290
Leetcode No.192 统计词频
写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。 为了简单起见,你可以假设: words.txt只包括小写字母和 ' ' 。 每个单词只由小写字母组成。 单词间由一个或多个空格字符分隔。
week
2021/05/06
1.1K0
LeetCode 0127 - Word Ladder
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Reck Zhang
2021/08/11
1820
LeetCode-192. 统计词频
写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。
悠扬前奏
2019/05/29
7040
hdu-----(1113)Word Amalgamation(字符串排序)
Word Amalgamation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2586    Accepted Submission(s): 1246 Problem Description In millions of newspapers across the United States there is a word game
Gxjun
2018/03/22
6410
HDOJ/HDU 2163 Palindromes(判断回文串~)
Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a sequence of characters that is identical to the string when the characters are placed in reverse order. For example, the following strings are palindromes: “ABCCBA”, “A”, and “AMA”. The following strings are not palindromes: “HELLO”, “ABAB” and “PPA”.
谙忆
2021/01/21
2690
leetcode-819-Most Common Word(词频统计)
题目描述: Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique. Words in the list of banned words are
chenjx85
2018/05/21
1.1K0
HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)
Problem Description In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
谙忆
2021/01/21
2940
字符串基础题
总结:所有题目都已做,有些Easy没有做第二遍,有两道没有accept,请戳 link-en, link-cn
王脸小
2019/11/02
9790
Google Shell 风格规范
可执行文件必须以 #!/bin/bash 开头和最少数量的标志。 使用 set 设置 shell 选项,以便将脚本调用为 bash 脚本名称不会破坏其功能。
acc8226
2022/05/17
1.3K0
在线matlab代码学习神器Octave Online
Octave与MATLAB完全兼容,免安装使用方便。注册和非注册功能会有不同,如下:
zhangrelay
2019/01/23
6.8K0
第八章:从 shell 眼中看世界
In this chapter we are going to look at some of the “magic” that occurs on the commandline when you press the enter key. While we will examine several interesting andcomplex features of the shell, we will do it with just one new command:
砖业洋__
2023/05/06
2560
LeetCode 付费题目(一)
【题目】Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
四火
2022/07/19
2.6K0
网课CodeCademy
In the example above, an image of a prickly pear has been turned into a link by wrapping the outside of the <img> element with an <a> element.
ZHaos
2019/02/27
7390
第二十二章 : 格式化输出
In this chapter, we continue our look at text related tools, focusing on programs that areused to format text output, rather than changing the text itself. These tools are often usedto prepare text for eventual printing, a subject that we will cover in the next chapter. Theprograms that we will cover in this chapter include:
砖业洋__
2023/05/06
3020
第二十六章 : 启动一个项目
Starting with this chapter, we will begin to build a program. The purpose of this projectis to see how various shell features are used to create programs and, more importantly,create good programs.
砖业洋__
2023/05/06
2260
LeetCode 题目解答——Medium 部分(上)
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
6370
LeetCode 题目解答——Medium 部分(上)
LeetCode 题目解答——155~226 题
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
7090
LeetCode 题目解答——155~226 题
相关推荐
LeetCode 0126 - Word Ladder II
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验