由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Quant版 - 又一个码题
相关主题
码题[合集] Interview Question: C++Algorithm
[合集] 今天的几个interview问题[合集] problem: expected distances
[合集] interview question 4An interview question for grep in linux (转载)
[合集] how do you sovle this kind of problem?问个简单的问题
[合集] a probability question问一道题
bond price needed?有没有被面过LINUX问题的?
问两道probability的题一个面试题
Given the range of stock price and the moving average, how to determine the investment point?Goldman Sachs的面试(front developer)
相关话题的讨论汇总
话题: sed话题: cat话题: mouse话题: dog话题: 码题
进入Quant版参与讨论
1 (共1页)
j******n
发帖数: 271
1
Given a text file, replace all "cat" with "dog" on all lines where there
is no "mouse". Use only traditional unix tools that come with system
installation, including sed, awk, but excluding perl, ruby, python.
c****w
发帖数: 79
2
nobody cares?
here is an ugly one,
assume the text file is a, then
$ nl -w10 -n rz a | grep mouse > a0
$ nl -w10 -n rz a | grep -v mouse > a1
$ cat a1 | sed 's/cat/dog/g' > a11
$ cat a11 a0 | sort | sed 's/[0-9]*\t//' > b
then file b has the result

【在 j******n 的大作中提到】
: Given a text file, replace all "cat" with "dog" on all lines where there
: is no "mouse". Use only traditional unix tools that come with system
: installation, including sed, awk, but excluding perl, ruby, python.

c****w
发帖数: 79
3
there were errors, so here's a better ugly one, hehe
$ nl -n rz a | grep mouse > a0
$ nl -n rz a | grep -v mouse | sed 's/cat/dog/g'> a1
$ cat a1 a0 | sort | sed 's/^[0-9]*\t//' > b

【在 c****w 的大作中提到】
: nobody cares?
: here is an ugly one,
: assume the text file is a, then
: $ nl -w10 -n rz a | grep mouse > a0
: $ nl -w10 -n rz a | grep -v mouse > a1
: $ cat a1 | sed 's/cat/dog/g' > a11
: $ cat a11 a0 | sort | sed 's/[0-9]*\t//' > b
: then file b has the result

k*******d
发帖数: 1340
4
原来现在街上编程的要求这么高了啊,要求这么高的Unix,看来只有CS的能行了
j******n
发帖数: 271
5
That is a good one. Actually there is a way without using temporary files,
even without pipes.

【在 c****w 的大作中提到】
: there were errors, so here's a better ugly one, hehe
: $ nl -n rz a | grep mouse > a0
: $ nl -n rz a | grep -v mouse | sed 's/cat/dog/g'> a1
: $ cat a1 a0 | sort | sed 's/^[0-9]*\t//' > b

c****w
发帖数: 79
6
Thanks to the little pressure
sed '/mouse/ !{ s/cat/dog/g }' a

【在 j******n 的大作中提到】
: That is a good one. Actually there is a way without using temporary files,
: even without pipes.

j******n
发帖数: 271
7
Wow cool. More elegant than the one I cooked up. Thanks!
J*****n
发帖数: 4859
8

能解释一下其中的语法么?

【在 c****w 的大作中提到】
: Thanks to the little pressure
: sed '/mouse/ !{ s/cat/dog/g }' a

1 (共1页)
进入Quant版参与讨论
相关主题
Goldman Sachs的面试(front developer)[合集] a probability question
问个martingale的题bond price needed?
请教一道option的题问两道probability的题
回报版面:这两个月碰到的面试题Given the range of stock price and the moving average, how to determine the investment point?
码题[合集] Interview Question: C++Algorithm
[合集] 今天的几个interview问题[合集] problem: expected distances
[合集] interview question 4An interview question for grep in linux (转载)
[合集] how do you sovle this kind of problem?问个简单的问题
相关话题的讨论汇总
话题: sed话题: cat话题: mouse话题: dog话题: 码题