由买买提看人间百态

topics

全部话题 - 话题: stdin
1 2 3 4 下页 末页 (共4页)
j*****y
发帖数: 1071
1
来自主题: JobHunting版 - file 输入和 stdin 输入
有个程序 a.out,从 stdin 读入
比如
cin >> n
cin >> s1
cin >> s2
想一次性从 file里面读入,但是不改变程序
比如有个 file data.txt 有以下三行
3
hello
world
这个怎么搞阿? cat data.txt | ./a.out ?
好像不work 阿
p*****p
发帖数: 379
2
来自主题: JobHunting版 - file 输入和 stdin 输入
freopen("data.txt", "r", stdin)
p****u
发帖数: 2422
3
来自主题: Linux版 - 请教个简单命令问题
from Advanced Bash-Scripting Guide:
http://www.tldp.org/LDP/abs/html/io-redirection.html
Chapter 20. I/O Redirection
Table of Contents
20.1. Using exec
20.2. Redirecting Code Blocks
20.3. Applications
There are always three default files [1] open, stdin (the keyboard), stdout
(the screen), and stderr (error messages output to the screen). These, and a
ny other open files, can be redirected. Redirection simply means capturing o
utput from a file, command, program, script, or even code block withi... 阅读全帖
s*****b
发帖数: 8
4
来自主题: JobHunting版 - 请问除了刷题还能怎样提高编程
我来贴一个。
Rocket fule (Software Engineer - Machine Learning Scientist ) 技术电面后code
test. code通过了所有test cases. 人家看过code 后就拒了。问题在哪里呢?请各位
牛人不吝赐教。题目本版以前贴过
You are standing in a rectangular room and are about to fire a laser toward
the east wall. Inside the room a certain number of prisms have been placed.
They will alter the direction of the laser beam if it hits them. There
are north-facing, east-facing, west-facing, and south-facing prisms. If the
laser beam strikes an east-facing prism, its cours... 阅读全帖
d*****u
发帖数: 17243
5
来自主题: Programming版 - Python有什么好的方法建two-way pipe?
我找到一个相对简单的办法,贴一下。
自己定义一个class,
read和write代替默认的读写方式
class Pipe(subprocess.Popen):
def __init__(self, argv, timeout = 0):
self.timeout = timeout
subprocess.Popen.__init__(self, argv, stdin = subprocess.PIPE,
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
def write(self, data):
poll = select.poll()
poll.register(self.stdin.fileno(), select.POLLOUT)
fd = poll.poll(self.timeout)
if len(fd):
f = fd[0]
if f[1] > 0:
... 阅读全帖
k********r
发帖数: 18
6
来自主题: Programming版 - 问个socket编程中select()的问题。
1。请问用select()的话是不是就block在select()这个函数?
2. 按我的理解, 一般可以把select()放在一个while(1)loop里这样就可以一直监听了。
即:每有一个事件就执行一下然后返回到select()停在那里。 但请看这段程序:
。。。
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
while(1){
printf("before select\n");
select(STDIN+1, &readfds, NULL, NULL, NULL);
printf("after select\n");
if (FD_ISSET(STDIN, &readfds))
printf("A key was pressed!\n");
}
printf("Timed out.\n");
......
我一按键,程序就不停地执行:
after select
A key was pressed!
before select
a
n**a
发帖数: 104
7
You can do that. All you need to do is to REDIRECT the input/output to
stdout/stdin.
You can use WIN32 API
BOOL WINAPI AllocConsole(void)
to allocate a console for the calling process (/DLL).
Further you can wrap in a class
CConsole:: CConsole()
{
AllocConsole();
freopen("CONOUT$","w+t",stdout);
freopen("CONIN$","r+t",stdin);
}
CConsole:: ~CConsole()
{
fclose(stdout);
fclose(stdin);
FreeConsole();
}
Then when you initialize your DLL, you create an instance of CConsole
y**a
发帖数: 39
8
来自主题: Unix版 - [转载] 请教
【 以下文字转载自 Programming 讨论区 】
【 原文由 yoga 所发表 】
在perl中如何把需要从STDIN得到的parameter不从STDIN来pass给它.
For example:
call a script:
`htpasswd passwordfile userid`;
which will add userid into passwordfile and generate the encrypted password
from the STDIN.
it will run like the following:
passwd:
re-type passwd:
怎样把一个default的passwd传过去在不改变这个script的情况下.
谢谢
i******s
发帖数: 301
9
来自主题: JobHunting版 - triplets 题怎么做比较高效?
这不就是抄interviewstreet上的么.
下面是我的python code,idea一样,处理重复有一点技巧,你可以自己想想。Time
complexity 应该是O(n^2),主要花在build smaller, greater array上,不过应该能
做到O(nlogn) on average case。我没仔细想过。
import sys
import bisect
arr_size = int(sys.stdin.readline())
arr = map(int, sys.stdin.readline().split())
smaller_than = [0 for i in range(arr_size)]
greater_than = [0 for i in range(arr_size)]
index_map = {}
for i in range(arr_size):
if arr[i] in index_map:
index_map[arr[i]].append(i)
else:
index_map[arr[... 阅读全帖
M**A
发帖数: 78
10
来自主题: JobHunting版 - 请教github上1道编程题的题意
请教各位朋友, 下面github上1道编程题的题意。
Write a program in C/C++ that translates a pattern specification
provided as ancommand line argument into a regular expression, processes
lines of input text receivedfrom stdin using that regular expression to
qualify matches, and finally writes eachmatching input line to stdout.
Each line of input is terminated by the newline character'n', and the
program should terminate when it receives EOF.The program should be
executable as follows:$ cat input.txt | ... 阅读全帖
g********t
发帖数: 39
11
来自主题: JobHunting版 - 贡献Rocket Fuel 4 hour online test
贡献刚做的online test,职位是Machine Learning related。
Question 1 / 2 (LaserMaze)
You are standing in a rectangular room and are about to fire a laser toward
the east wall. Inside the room a certain number of prisms have been placed.
They will alter the direction of the laser beam if it hits them. There
are north-facing, east-facing, west-facing, and south-facing prisms. If the
laser beam strikes an east-facing prism, its course will be altered to be
East, regardless of what direction it had been goi... 阅读全帖
M****g
发帖数: 162
12
来自主题: JobHunting版 - 急求rocket fuel 3小时的online test!!!
Question 1 / 2 (LaserMaze)
You are standing in a rectangular room and are about to fire a laser toward
the east wall. Inside the room a certain number of prisms have been placed.
They will alter the direction of the laser beam if it hits them. There
are north-facing, east-facing, west-facing, and south-facing prisms. If the
laser beam strikes an east-facing prism, its course will be altered to be
East, regardless of what direction it had been going in before. If it hits
a south-facing prism... 阅读全帖
d***r
发帖数: 2032
13
来自主题: JobHunting版 - two sigma 的online code test 的问题
收到邮件要做这个test,做之前有个sample test,我做了一下发现这个系统下,读入
文件总是出错或者读不进去数据。
比如数据在 STDIN 里:
4
1 2 3 4
我的试验程序如下
#include
#include
#include
#include
using namespace std;
int main() {
ifstream infile("STDIN.txt");
string line;
while (getline(infile, line))
{
stringstream iss(line);
cout< }
}
总是无法输出,但是同样程序在VS2011就没问题。 请问大牛,这种情况应该如何做才
能读入数据?如果这个问题解决不了,我估计做题肯定通不过。
谢谢
x****u
发帖数: 81
14
我做过Yahoo发的一个,比较简单3道题,总共是45分钟还是一个小时来着。基本都是几
分钟可以搞定一个,不过要看清楚条件,会有复杂度要求。
另外就是提前看下读stdin的语法,很多题的接口是stdin和stdout,不是让你完成函数
c*******4
发帖数: 51
15
来自主题: JobHunting版 - Simple Database设计问题,附code
详细信息在下面网址:
https://www.thumbtack.com/challenges/simple-database
面试一道题目,题目是设计一个Simple Database,我提交的解法说有问题然后被拒了。
只想各位大神帮我看看code有哪些毛病,feedback说算法不是最优的,function大小写
没有注意。
目前我觉得自己问题在NUMEQUALTO复杂度n不是1,方程都大写了,想请教下还有什么问
题。
下面是我python的代码。再次感谢大家的帮助,我只想知道代码哪里还出了问题。
import sys
class Database(object):
def __init__(self):
self._history = [] #list to store transaction history
self._database = {} #dictionary store key value pair
def BEGIN(self):
self._history.append({})
def S... 阅读全帖
c*******4
发帖数: 51
16
来自主题: JobHunting版 - Simple Database设计问题,附code
详细信息在下面网址:
https://www.thumbtack.com/challenges/simple-database
面试一道题目,题目是设计一个Simple Database,我提交的解法说有问题然后被拒了。
只想各位大神帮我看看code有哪些毛病,feedback说算法不是最优的,function大小写
没有注意。
目前我觉得自己问题在NUMEQUALTO复杂度n不是1,方程都大写了,想请教下还有什么问
题。
下面是我python的代码。再次感谢大家的帮助,我只想知道代码哪里还出了问题。
import sys
class Database(object):
def __init__(self):
self._history = [] #list to store transaction history
self._database = {} #dictionary store key value pair
def BEGIN(self):
self._history.append({})
def S... 阅读全帖
f*******r
发帖数: 976
17
来自主题: JobHunting版 - Simple Database设计问题,附code
这种题目都出来了,难度不小啊

详细信息在下面网址:
https://www.thumbtack.com/challenges/simple-database
面试一道题目,题目是设计一个Simple Database,我提交的解法说有问题然后被拒了。
只想各位大神帮我看看code有哪些毛病,feedback说算法不是最优的,function大小写
没有注意。
目前我觉得自己问题在NUMEQUALTO复杂度n不是1,方程都大写了,想请教下还有什么问
题。
下面是我python的代码。再次感谢大家的帮助,我只想知道代码哪里还出了问题。
import sys
class Database(object):
def __init__(self):
self._history = [] #list to store transaction history
self._database = {} #dictionary store key value pair
def BEGIN(self):
self._history.appe... 阅读全帖
s***g
发帖数: 495
18
来自主题: Hardware版 - pogoplug 装 ArchLinux 一点心得
arcNumber = architecture number?
It is set in uboot so should be used by uboot to pass to the kernel.
Use a newer uboot and kernel should make it boot on sata.
I have tried on pogo v4 and it boot from all three source: usb, mmc, and
sata.
My way is different from debian forum becase I think it is easier and
confirmed by your experience too.
Here is my uboot env for pogo v4. I shared it here for the same reason.
arcNumber=3960
baudrate=115200
bootcmd=setenv bootargs console=ttyS0,$baudrate root=L... 阅读全帖
c******n
发帖数: 4965
19
我做如下的东西
Runtime.getRumtime().exec("ssh -t localhost sudo blahblah");
我想要这个sudo 的password prompt 自然转到terminal 上,然后人可以敲密码,
但现在它干脆就不读输入的密码---ssh 说stdin is not a terminal, 所以-t 不行。
谁有个办法?
另外注意我必须access terminal, not stdin, because many (most) applications
directly read the terminal when asking for passwd.
l*******G
发帖数: 1191
20
来自主题: Linux版 - perl 高手看过来
想要把程序的patch 转成html 格式,这样code 的前后变化一目了然,便于交流,
却找不到合适的工具,
找到了一个patch2html.pl
http://david.decotigny.free.fr/snippets/patch2html.pl
下载后,运行却出错,程序很短,这里全贴出来,高手们试一试,看能否解决。(我想
很可能是程序经http下载后个别特殊字符出了问题。
错误信息如下:
Name "main::FH" used only once: possible typo at
patch2html.pl line 312.
Use of uninitialized value $string in substitution (s///)patch2html.pl line 71, line 2.
Use of uninitialized value $string in substitution (s///) patch2html.pl line 76, line 2.
---原程序 -------------------------
h*******c
发帖数: 248
21
来自主题: Linux版 - 我的home server
是的。音乐,照片都是server转的。关于音乐:
http://huyouncic.wordpress.com/2010/02/18/ape-cue-to-flac-tracks/
http://huyouncic.wordpress.com/2010/12/06/convert-flacs-to-mp3s/
关于照片:
http://huyouncic.wordpress.com/2010/12/13/jpg-resize-and-re-orientation/
watch dog的下载部分:可能需要re-format
#!/usr/bin/perl
use POSIX qw(setsid);
$SEED="/mnt/nas/hdraid1/mldonkey/seeds";
$INCOMING="/mnt/nas/hdraid1/mldonkey/incoming";
$DESTINATION="/mnt/nas/hdraid1/share";
chdir '/' or die "cannot change to /:$!";
open STDIN,'/dev/null' or di... 阅读全帖
p***o
发帖数: 177
22
来自主题: Programming版 - perl question
$_=;
until (/My name is (\[a-z]+)/){
$_=;
}
print "$1\n";
怎么不work? 郁闷
I**********s
发帖数: 441
23
来自主题: Programming版 - One more "keng" about Perl
This is a bug of Perl's regular expression.
What will "(a)|(b)\1|(b)\2" match in "bb"?
The perl script below shows that "(a)|(b)\1|(b)\2" matches the empty string
"" before "bb", which can't be correct.
do {
print "Enter a regular expression: ";
$re = ;
chop($re);
do {
print "Enter a string: ";
$_ = ;
chop($_);
if (m/$re/) {
print "$re matches \"$&\" in $_\n"; # the matched part.
print "\$` = $`\n"; # the part of the string before the match.
print "\$'
l******t
发帖数: 12659
24
来自主题: Programming版 - [请教]命令行的重定向
非常感谢两位的回复!!!
我也不知道为什么那个题目重定向stdin到文件夹
不过, wc的命令似乎也可以将stdin重定向到文件夹
link: http://blog.chinaunix.net/u1/56343/showart_442411.html
t*********o
发帖数: 143
25
来自主题: Programming版 - python比java慢这么多呀
谢谢大家讨论。楼上贴的benchmark results非常有用,跟我的数据很吻合,很好 :)
我的python核心代码如下。很简单,一行一行的读,把某一列的数据重新label一下,
然后写到一个新的文件。
我已经决定就用java了。从benchmark比较结果来看,java速度已经位列最快的一波了
,居然比c#还快。
pos = 10; // 11th column of the data
valueMap = {}
cnt = 0
for ln in sys.stdin:
vals = ln.split('\t')
val = vals[pos]
if val == null or len(val) == 0: continue
else:
if (not valueMap.has_key(val)):
... 阅读全帖
v*s
发帖数: 946
26
来自主题: Programming版 - python比java慢这么多呀
我这段代码貌似快了不少。 你的用了10秒钟处理1M lines, 我这个3.9秒。 你试试看。
测试的时候都是重新定向到文件。 直接输出到屏幕那个太慢。
我把你的文件中间“xxx lines processed” 也去掉了。
import sys
pos = 10
valueMap = {}
cnt = 0
for line in sys.stdin:
vals = line.split('\t')
val = vals[pos]
if not val:
continue
else:
if not val in valueMap:
valueMap[val] = str(len(valueMap) + 1)
vals[pos] = valueMap[val]
sys.stdout.write('\t'.join(vals))
sys.stdin.close()
sys.stdout.close()
t*********o
发帖数: 143
27
来自主题: Programming版 - python比java慢这么多呀
谢谢大家讨论。楼上贴的benchmark results非常有用,跟我的数据很吻合,很好 :)
我的python核心代码如下。很简单,一行一行的读,把某一列的数据重新label一下,
然后写到一个新的文件。
我已经决定就用java了。从benchmark比较结果来看,java速度已经位列最快的一波了
,居然比c#还快。
pos = 10; // 11th column of the data
valueMap = {}
cnt = 0
for ln in sys.stdin:
vals = ln.split('\t')
val = vals[pos]
if val == null or len(val) == 0: continue
else:
if (not valueMap.has_key(val)):
... 阅读全帖
v*s
发帖数: 946
28
来自主题: Programming版 - python比java慢这么多呀
我这段代码貌似快了不少。 你的用了10秒钟处理1M lines, 我这个3.9秒。 你试试看。
测试的时候都是重新定向到文件。 直接输出到屏幕那个太慢。
我把你的文件中间“xxx lines processed” 也去掉了。
import sys
pos = 10
valueMap = {}
cnt = 0
for line in sys.stdin:
vals = line.split('\t')
val = vals[pos]
if not val:
continue
else:
if not val in valueMap:
valueMap[val] = str(len(valueMap) + 1)
vals[pos] = valueMap[val]
sys.stdout.write('\t'.join(vals))
sys.stdin.close()
sys.stdout.close()
y*******g
发帖数: 6599
29
来自主题: Programming版 - python 问题
我试了没这个问题
只是
Traceback (most recent call last):
File "", line 1, in
File "", line 6, in coef
TypeError: unsupported operand type(s) for ^: 'float' and 'int'
n******7
发帖数: 12463
30
想了一个土办法
一开始run的时候从stdin读入密码
存到一个变量里面
然后
方案1: 每次需要输入密码的时候模拟stdin输入这个密码
方案2: 把密码bake到某个函数里面 这个函数再通过pipe把密码传给rsync
最终是保证密码不要明文出现在shell命令行,被ps或者log 文件捕获
这样跟手输密码差不多安全吧
c*****m
发帖数: 1160
31
from subprocess import Popen, PIPE, STDOUT
p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input='data_to_write')[0]
往 stdin里写
p**k
发帖数: 120
32
I am working on a system recoverey tool that needs
to capture all keystrokes of the machine, are there
anyway to do this under linux? I know there are some
commercial tools for Windows. But is there anyway to
do this under linux?
I can have access to root and everything. Is it possible
to pipe stdin to some places and record it before feed
back to (a faked) stdin? This needs to work under X also.
Thanks a lot.
D*******a
发帖数: 207
33
Use python; it is fast and easy to understand.
Save the following program as "abc.py", than run it as:
python abc.py < oldfinename > outputfilename
import sys
header = sys.stdin.readline()
print header,
pre_id = ""
pre_value = ""
for line in sys.stdin:
lineSplit = line.rstrip("\s").split()
if lineSplit[1] == "NA":
if lineSplit[0] == pre_id:
lineSplit[1] = pre_value;
else:
lineSplit[1] = "0"
pre_id = lineSplit[0]
pre_value = lineSplit[1]
p*********a
发帖数: 21
34
来自主题: JobHunting版 - 请教一道算法题
Here is the code to find the largest square matrix, for the rectangular, it's a bit more difficult. I will share the code if possible.
#include
#include
const int N = 100;
int d[N][N];
int m, n;
inline int minF(int a, int b, int c) {
return a }
int main()
{
freopen("in.txt", "r", stdin);
int i, j;
while(scanf("%d%d", &m, &n)!=EOF) {
memset(d, 0, sizeof(d));
for(i=1; i<=m; i++) {
for(j=1; j<=n; j++) {
f*********r
发帖数: 68
35
来自主题: JobHunting版 - Google面试问题
写了一个O(log5(N))的算法, 不过好像还有更快的!
int n;
int tbl[] = {1, 1, 2, 6, 4, 4, 4, 8, 4, 6};
int tbl2[][4] = {{}, {2, 6, 8, 4}, {4, 2, 6, 8}, {6, 8, 4, 2}, {8, 4, 2, 6}};
int fun(int n) {
if(n<5) return tbl[n];
int k = (fun(n/5)*tbl[n%10]*6)%10;
return tbl2[k/2][(n/5)%4];
}
void solve() {
int res;
res = fun(n);
cout< }
int main(){
freopen("in.txt", "r", stdin);
while(cin>>n) {
solve();
}
}
b****j
发帖数: 78
36
来自主题: JobHunting版 - this question is nice
can be done in less than 20 lines of python:
import sys
last = []
for line in sys.stdin:
node, data = map(str.strip, line.split('='))
nodes = map(str.strip, node.split('.'))
for i, (s1, s2) in enumerate(map(None, last, nodes)):
if s1 != s2:
if s1:
sys.stdout.write('' % '> if s2:
sys.stdout.write('<%s>' % '><'.join(nodes[i:]))
break
sys.stdout.write(data)
last = last[:i] + nodes[i:]
if last:
print '' % '>
i**********e
发帖数: 1145
37
来自主题: JobHunting版 - FaceBook面经--第三(最后)部分
我以前贴过,但不知道为什么找不回那个帖子了。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in, indentLevel+TAB_SPACE);
c = in.get();
assert(c == '... 阅读全帖
g*******s
发帖数: 490
38
感想,居然第一题就考到了goto。。我靠。。
有两三道题程序实在是太长,一堆指针+loop,看不清楚,不过这些程序基本是找错。。只能看答案猜了
最好两台电脑,一台要打程序,查资料操作不过来,如果有帮手就更好了
1. variable definition 和 declaration的 区别
2. 怎么是最portable的方法操作integer最高位的那个byte,比如设1
3. vfprintf考到了
4. char *x;
x="abcd"
请问这个操作的意义,合法不合法?空间是怎么allocate
5. freebsd某个头文件的一个macro
#if defined xxxx
#define _p(xxx) (xxx)
#else
#define _p(xxx) ()
#end
what does this macro for
6. memcpy 在某个iso xxx标准中的标准declaration是
7. memmve
8.哪个方法是一个c program必须的方法,main?
9. c提供了哪个方法获取 any file的 size
10. file * fp ... 阅读全帖
i**********e
发帖数: 1145
39
来自主题: JobHunting版 - Facebook Interview Questions
你这方法我想了想,应该是不行的。
试想想,假设有重复的数字怎么处理呢?
[3, 5, 2], 5
hash[5] = pointer that point to node with value 5.
sliding window move to the right 1 step.
3, [5, 2, 5]
Now, you have two 5's. If you choose hash[5] to point to the first 5, which
will be deleted next, so now hash[5] points to nothing!
这问题最优解好像是 O(N),大家再努力想想吧。
(以下的解法是利用 hash table 来数每一个数字出现的次数,不是最优解,复杂度为
O(N lg W),W = sliding window 的长度
#include
#include
#include
#include
#include
using namespace s... 阅读全帖
i**********e
发帖数: 1145
40
来自主题: JobHunting版 - 问道 facebook 面试题
不是大侠,贴一贴我的代码,代码如果不好看 请多多包涵
恐怕只能 handle 以上的 sample test case。
基本思路就是递归,如果有更复杂的情况可以再慢慢改进。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in... 阅读全帖
l*********y
发帖数: 142
41
来自主题: JobHunting版 - 这个facebook puzzle样题怎么做?
顺手写了一个,46min整。也没用状态压缩,待会看一下gloomyturkey的code。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 9;
const int maxk = 6;
int N, K;
struct State {
State() {
for (in... 阅读全帖
l*********y
发帖数: 142
42
来自主题: JobHunting版 - Palantir面经2
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Segment {
int from, to;
};
struct cmp {
bool operator() (const Segment& s1, const Segment& s2) {
return s1.from < s2.from;
}
};
bool ... 阅读全帖
d********w
发帖数: 363
43
来自主题: JobHunting版 - 拼写检查的变种
拼写检查做法有个计算编辑距离,http://en.wikipedia.org/wiki/Levenshtein_distance
google人工智能老大曾经写过一个非常简短但很高效的拼写检查器,http://norvig.com/spell-correct.html.
下面这个题目也有些类似
Write a program that reads a large list of English words (e.g. from /usr/
share/dict/words on a unix system) into memory, and then reads words from
stdin, and prints either the best spelling suggestion, or "NO SUGGESTION" if
no suggestion can be found. The program should print ">" as a prompt before
reading each word, and should loop until killed.
Yo... 阅读全帖
n*******w
发帖数: 687
44
来自主题: JobHunting版 - 问个关于排序的面试题
当然可以。
其实这题难度不在算法。对效率也没要求。但是要在规定时间内code然后编译运行出正
确的结果。输入是stdin。时间会很紧张。很难有时间写高效的算法。
还一个题在这里。
http://www.mitbbs.com/article_t/JobHunting/31993307.html
n*******w
发帖数: 687
45
来自主题: JobHunting版 - 一个面试题 -- restore database
一个distributed database,各个host上都只保留了所有数据的一个subset。
现在要在每个host上restore所有数据。也就是对于每个host,把missing的数据从其它
host copy过来。至于选哪一个host,没有要求。
assume 所有host上数据的union就是整个database。
input是
host 1: 1 3 5
host 2: 2 3 4
host 3: 1 2 4 5 6
一种可能的output是
copy 2 from host 2 to 1
copy 4 from host 2 to 1
copy 6 from host 3 to 1
copy 1 from host 1 to 2
copy 5 from host 3 to 2
copy 6 from host 3 to 2
copy 3 from host 1 to 3
所有input要从stdin输入。
跟上一题同样的,最难的不在算法,而是短时间内写出code编译运行出正确结果。这题
可能还需要点数据结构。
e***n
发帖数: 42
46
来自主题: JobHunting版 - FB coding challenge sample question
既然是样题,希望能公开讨论一下:这道题像careercup书上Hanoi Tower的一个变形:
There are K pegs. Each peg can hold discs in decreasing order of radius when
looked from bottom to top of the peg. There are N discs which have radius 1
to N; Given the initial configuration of the pegs and the final
configuration of the pegs, output the moves required to transform from the
initial to final configuration. You are required to do the transformations
in minimal number of moves.
A move consists of picking the topmost disc of any o... 阅读全帖
n*s
发帖数: 752
47
来自主题: JobHunting版 - Epic Written Interview
4. telephone keyboard problem
import sys
map = {'0':'0','1':'1','2':'abc2','3':'def3','4':'ghi4','5':'jkl5','6':'mno6
','7':'pqrs7','8':'tuv8','9':'wxyz9','*':' ','#':'#'}
def translate():
print 'Please input from telephone keyboard:'
input = sys.stdin.readline().strip()
output = []
count = -1
prev = input[0]
for x in input:
if not (x in map.keys()):
return 'invalid input!'
if x == prev: # repeated input
if x == '*' or x == '0' or ... 阅读全帖
n*s
发帖数: 752
48
来自主题: JobHunting版 - Epic Written Interview
4. string transpose
import sys
def transpose(input,i):
mystr = list(input)
mystr[i],mystr[i+1] = mystr[i+1],mystr[i]
return ''.join(mystr)
def str_transpose():
print 'input two strings, separated by blank:'
a, b = sys.stdin.readline().split()
size = len(a)
if size != len(b) or sorted(a) != sorted(b):
return 'no way!'
next = [b]
parent = {b:None}
idx = size -1
notFound = True
while notFound:
newstr = []
for x in next:
... 阅读全帖
n*s
发帖数: 752
49
来自主题: JobHunting版 - Epic Written Interview
4. telephone keyboard problem
import sys
map = {'0':'0','1':'1','2':'abc2','3':'def3','4':'ghi4','5':'jkl5','6':'mno6
','7':'pqrs7','8':'tuv8','9':'wxyz9','*':' ','#':'#'}
def translate():
print 'Please input from telephone keyboard:'
input = sys.stdin.readline().strip()
output = []
count = -1
prev = input[0]
for x in input:
if not (x in map.keys()):
return 'invalid input!'
if x == prev: # repeated input
if x == '*' or x == '0' or ... 阅读全帖
n*s
发帖数: 752
50
来自主题: JobHunting版 - Epic Written Interview
4. string transpose
import sys
def transpose(input,i):
mystr = list(input)
mystr[i],mystr[i+1] = mystr[i+1],mystr[i]
return ''.join(mystr)
def str_transpose():
print 'input two strings, separated by blank:'
a, b = sys.stdin.readline().split()
size = len(a)
if size != len(b) or sorted(a) != sorted(b):
return 'no way!'
next = [b]
parent = {b:None}
idx = size -1
notFound = True
while notFound:
newstr = []
for x in next:
... 阅读全帖
1 2 3 4 下页 末页 (共4页)