Pipes实现LeetCode(192.单词频率)

[LeetCode] 192.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.

For 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.

[show hint]

Hint:
Could you write it in one-line using Unix pipes?

这道题给了我们一个文本文件,让我们统计里面单词出现的个数,提示中让我们用管道Pipes来做,在之前那道Tenth Line中,我们使用过管道命令。提示中让我们用管道连接各种命令,然后一行搞定,那么我们先来看第一种解法,乍一看啥都不明白,咋办?没关系,容我慢慢来讲解。首先用的关键字是grep命令,该命令一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。后面紧跟的-oE '[a-z]+'参数表示原文本内容变成一个单词一行的存储方式,于是此时文本的内容就变成了:

the
day
is
sunny
the
the
the
sunny
is

下面的sort命令就是用来排序的。排完序的结果为:

day
is
is
is
sunny
sunny
the
the
the
the

后面的uniq命令是表示去除重复行命令,后面的参数-c表示在每行前加上表示相应行目出现次数的前缀编号,得到结果如下:

   1 day
3 is
2 sunny
4 the

然后我们再sort一下,后面的参数-nr表示按数值进行降序排列,得到结果:

   4 the
3 is
2 sunny
1 day 

而最后的awk命令就是将结果输出,两列颠倒位置即可:

解法一:

grep -oE '[a-z]+' words.txt | sort | uniq -c | sort -nr | awk '{print $2" "$1}' 

下面这种方法用的关键字是tr命令,该命令主要用来进行字符替换或者大小写替换。后面紧跟的-s参数表示如果发现连续的字符,就把它们缩减为1个,而后面的‘ '和‘\n'就是空格和回车,意思是把所有的空格都换成回车,那么第一段命令tr -s ' ' '\n' < words.txt 就好理解了,将单词之间的空格都换成回车,跟上面的第一段实现的作用相同,后面就完全一样了,参见上面的讲解:

解法二:

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

下面这种方法就没有用管道命令进行一行写法了,而是使用了强大的文本分析工具awk进行写类C的代码来实现,这种写法在之前的那道Transpose File已经讲解过了,这里就不多说了,最后要注意的是sort命令的参数-nr -k 2表示按第二列的降序数值排列:

解法三:

awk '{
    for (i = 1; i <= NF; ++i) ++s[$i];
} END {
    for (i in s) print i, s[i];
}' words.txt | sort -nr -k 2

参考资料:

https://leetcode.com/discuss/33353/my-accepted-answer-using-tr-sort-uniq-and-awk

https://leetcode.com/discuss/46976/my-ac-solution-one-pipe-command-but-cost-20ms

https://leetcode.com/discuss/29001/solution-using-awk-and-pipes-with-explaination

到此这篇关于Pipes实现LeetCode(192.单词频率)的文章就介绍到这了,更多相关Pipes实现单词频率内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++实现LeetCode(188.买卖股票的最佳时间之四)

    [LeetCode] 188.Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You m

  • C++实现LeetCode(309.买股票的最佳时间含冷冻期)

    [LeetCode] 309.Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as

  • C++实现LeetCode(191.位1的个数)

    [LeetCode] 191.Number of 1 Bits 位1的个数 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000

  • Pipes实现LeetCode(193.验证电话号码)

    [LeetCode] 193.Valid Phone Numbers 验证电话号码 Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers. You may assume that a valid phone number must appear in one of th

  • C++实现LeetCode(190.颠倒二进制位)

    [LeetCode] 190. Reverse Bits 颠倒二进制位 Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 re

  • Pipes实现LeetCode(194.转置文件)

    [LeetCode] 194.Transpose File 转置文件 Given a text file file.txt, transpose its content. You may assume that each row has the same number of columns and each field is separated by the ' ' character. For example, if file.txt has the following content: na

  • Pipes实现LeetCode(195.第十行)

    [LeetCode] 195.Tenth Line 第十行 How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth li

  • C++实现LeetCode(557.翻转字符串中的单词之三)

    [LeetCode] 557.Reverse Words in a String III 翻转字符串中的单词之三 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode conte

  • Pipes实现LeetCode(192.单词频率)

    [LeetCode] 192.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 consis

  • Python开发的单词频率统计工具wordsworth使用方法

    使用方法: python wordsworth --filename textfile.txt python wordsworth -f textfile.txt 分析结果: 附上github项目地址:https://github.com/autonomoid/wordsworth

  • c语言实现词频统计的简单实例

    需求: 1.设计一个词频统计软件,统计给定英文文章的单词频率. 2.文章中包含的标点不计入统计. 3.将统计结果以从大到小的排序方式输出. 设计: 1.因为是跨专业0.0···并不会c++和java,只能用仅学过的C语言进行编写,还是挺费劲的. 2.定义一个包含单词和频率两个成员的结构体来统计词频(进行了动态分配内存,可以处理较大文本). 3.使用fopen函数读取指定的文档. 4.使用fgetc函数获取字符,再根据取得的字符是否是字母进行不同的处理. 5.采用快速排序法对统计结果进行排序. 5

  • python字典操作实例详解

    本文实例为大家分享了python字典操作实例的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import turtle ##全局变量## #词频排列显示个数 count = 10 #单词频率数组-作为y轴数据 data = [] #单词数组-作为x轴数据 words = [] #y轴显示放大倍数-可以根据词频数量进行调节 yScale = 6 #x轴显示放大倍数-可以根据count数量进行调节 xScale =

  • PHP编程计算文件或数组中单词出现频率的方法

    本文实例讲述了PHP编程计算文件或数组中单词出现频率的方法.分享给大家供大家参考,具体如下: 如果是小文件,可以一次性读入到数组中,使用方便的数组计数函数进行词频统计(假设文件中内容都是空格隔开的单词): <?php $str = file_get_contents("/path/to/file.txt"); //get string from file preg_match_all("/\b(\w+[-]\w+)|(\w+)\b/",$str,$r); //

  • python统计文本字符串里单词出现频率的方法

    本文实例讲述了python统计文本字符串里单词出现频率的方法.分享给大家供大家参考.具体实现方法如下: # word frequency in a text # tested with Python24 vegaseat 25aug2005 # Chinese wisdom ... str1 = """Man who run in front of car, get tired. Man who run behind car, get exhausted."&quo

  • python实现统计文本中单词出现的频率详解

    本文实例为大家分享了python统计文本中单词出现频率的具体代码,供大家参考,具体内容如下 #coding=utf-8 import os from collections import Counter sumsdata=[] for fname in os.listdir(os.getcwd()): if os.path.isfile(fname) and fname.endswith('.txt'): with open(fname,'r') as fp: data=fp.readlines

随机推荐