LeetCode Shell Problems
195. Tenth Line -- 第十行
How would you print just the 10th line of a file?
Solution:
awk 'NR==10' file.txt
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:
name age
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
Solution:
awk '{
max_nf = NF
max_nr = NR
for (x = 1; x <= NF; x++) {
vector[x, NR] = $x
}
}
END {
for (x = 1; x <= max_nf; x++) {
for (y = 1; y <= max_nr; y++) {
printf("%s", vector[x, y])
if (y < max_nr)
printf(" ")
}
if (x < max_nf)
printf("\n")
}
}' file.txt
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 the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
You may also assume each line in the text file must not contain leading or trailing white spaces.
For example, assume that file.txt has the following content:
987-123-4567
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567
(123) 456-7890
Solution:
awk '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/ || /^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/' file.txt
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.
Solution:
awk '{for(i=1;i<=NF;i++) a[$i]+=1} END{for(i in a) print i,a[i] | "sort -r -n -k2"}' words.txt
LeetCode Shell Problems的更多相关文章
- leetcode shell
leetcode 195. 第十行 # | | 第一种是先取出前10行,然后取出最后一行.(但是不足10行,也可以取出最后一行) 正解: tail -n +K :从第K行取出所有 然后取出第一行 le ...
- https://oj.leetcode.com/problems/majority-element/
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...
- leetcode shttps://oj.leetcode.com/problems/surrounded-regions/
1.从外围搜索O,深度搜索出现了 Line 35: java.lang.StackOverflowError Last executed input: ["OOOOOOOOOOOOOOOOO ...
- https://leetcode.com/problems/palindromic-substrings/description/
https://www.cnblogs.com/grandyang/p/7404777.html 博客中写的<=2,实际上<=1也是可以的 相当于判断一个大指针内所有子字符串是否可能为回文 ...
- [leetcode shell]194. Transpose File
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
- [leetcode shell]192. Word Frequency
统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...
- leetcode 新题型----SQL,shell,system design
leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...
- [LeetCode] Transpose File 转置文件
Given a text file file.txt, transpose its content. You may assume that each row has the same number ...
随机推荐
- 软件工程 speedsnail 冲刺3
2015-5-7 完成任务:环境崩溃,重新配置环境,学习了黑马android教学视频前6集: 遇到问题: 问题1 Error: Error parsing C:\Users\sam\.android\ ...
- AddToDate
AddToDate is a PeopleCode built-in function for manipulating a date in PeopleCode. You can use it to ...
- 【Zend Studio】10.6.0版本设置默认编码为UTF-8
1.打开Windows->Prefefences 2.找到Workspace->Text file encoding,修改为UTF-8,OK保存.
- 为你的Windows7设置动态壁纸
From:http://www.cnblogs.com/killerlegend/p/3644014.html By KillerLegend DreamScene是Vista上的一个功能,可以让你设 ...
- Android SDK中国在线更新镜像服务器 解决GOOGLE更新无法下载 更新失败的问题
Android Tools Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/I ...
- devexpress 控制面板汉化方式 参考信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# 乘法口诀表的实现方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 乘法运算 ...
- Oracle 11gR2 RAC修改SCAN IP
一.查看当前环境: # grid用户 检查scan-ip地址的配置 [grid@node1 ~]$ srvctl config scan SCAN name: scan-cluster.com, Ne ...
- 一些关于python的小感想
python是一门优秀的语言,但随之而来的是大量的知识,各种模块,相信一个人的大脑是很难记住如此多的内容.这时后的我们就应该想办法避免去记忆这么多的内容. 1.查看官方文档(英语很重要,啥也不说了) ...
- 6.css文本样式
文本样式,只要针对的是文本的效果和文本的方位,即文本样式和文本控制总结起来有一表中的属性可用: 属性名 说明 CSS 版本 text-decoration 装饰文本出现各种划线 1 text-tran ...