Redian新闻
>
if ladyred be the next PM, I wouldn't visit this board anymore
avatar
if ladyred be the next PM, I wouldn't visit this board anymore# Immigration - 落地生根
w*s
1
input: 4个数字, a,b,c,d
output: 所有结果=24的等式,例如:24=(a+b)*(c*d)
avatar
C*y
2
rt
avatar
l*8
3
很繁琐,如果不是事先写过,很难在45分钟内写好。
avatar
T*y
4
So let's try to make it happen that she wouldn't be!

【在 C*********y 的大作中提到】
: rt
avatar
l*8
5
单单把二叉树输出为算术表达式(没有冗余括号》, 就可以作为面试coding的一道题
目了。
avatar
l*d
6
All right, I will do my best to compete to be the BM and I will do my best
to make your guys to stay here. Thank you.
avatar
s*5
7
如果只有4个数字的话,一共24个顺序X64种变化,brute force OK了吧
avatar
a*n
8
why???
avatar
s*5
9
如果只有4个数字的话,一共24个顺序X64种变化,brute force OK了吧
avatar
l*d
10
They do not like me that much now but they will change their mind as time
goes by.

【在 a***n 的大作中提到】
: why???
avatar
s*5
11
如果只有4个数字的话,一共24个顺序X64种变化,brute force OK了吧
avatar
T*y
12
I like it.
You are a fighter, so am I.

【在 l*****d 的大作中提到】
: All right, I will do my best to compete to be the BM and I will do my best
: to make your guys to stay here. Thank you.

avatar
p*2
13
对呀。好像就是brute force呀
avatar
g*y
15
准备面试的标准应该做到可以写,不是真写完,但应该把框架写出来。
写成接近可运行的程序,45分钟要求有点高,但也不算太离谱。那次谁贴的Amazon的计
算机多个加油站之间距离的,我觉得比这个难多了。大家在版上讨论了很久。碰上那种
题,绝对是人品问题了。

【在 w********s 的大作中提到】
: input: 4个数字, a,b,c,d
: output: 所有结果=24的等式,例如:24=(a+b)*(c*d)

avatar
l*d
16
Yes, we are alike. I like this contest too. I am realizing what are my
limitations day by day and trying to make up after losing some sheep. And
Crazydog is learning Term operations. So this contest is totally positive.

【在 T*******y 的大作中提到】
: I like it.
: You are a fighter, so am I.

avatar
m*n
17
a*(b+c)/d
这个还要考虑括号吧。

【在 s*********5 的大作中提到】
: 如果只有4个数字的话,一共24个顺序X64种变化,brute force OK了吧
avatar
c*g
18
谢谢夸奖,在下已经学会丝路了

【在 l*****d 的大作中提到】
: Yes, we are alike. I like this contest too. I am realizing what are my
: limitations day by day and trying to make up after losing some sheep. And
: Crazydog is learning Term operations. So this contest is totally positive.

avatar
l*8
19
In addition, the output should not contains duplications even if there are
duplicated numbers.
For example,
6 6 6 6
we don't want to output 6+6+6+6 for 24 times.

【在 m***n 的大作中提到】
: a*(b+c)/d
: 这个还要考虑括号吧。

avatar
T*y
20
Cheer for you!

【在 c******g 的大作中提到】
: 谢谢夸奖,在下已经学会丝路了
avatar
p*2
21

明白什么意思了。

【在 m***n 的大作中提到】
: a*(b+c)/d
: 这个还要考虑括号吧。

avatar
l*d
22
赞扬,继续学习~~主要看看b5 to b8 操作。我以前写过一些教程,我去找找。丝路
在web下也可以用,但不如Term 下用方便。

【在 c******g 的大作中提到】
: 谢谢夸奖,在下已经学会丝路了
avatar
m*n
23
这个好办,直接表达式压入set就行了。
其实a*(b+c)/d的情况也是可以解决的。不用考虑括号。
a*(b+c)/d 等同于
b+c*a/d 每次evaluate表达式直接从左往右evaluate.
那么只要穷举了所有的abcd排列, 然后3个符号位置组合就行了。

【在 l*********8 的大作中提到】
: In addition, the output should not contains duplications even if there are
: duplicated numbers.
: For example,
: 6 6 6 6
: we don't want to output 6+6+6+6 for 24 times.

avatar
z*y
24
这个有些过吧,大家要淡定,淡定
avatar
l*8
25
if depends on if the interviewer thinks
a*(b+c)/d and (b+c)*a/d are different or not.

【在 m***n 的大作中提到】
: 这个好办,直接表达式压入set就行了。
: 其实a*(b+c)/d的情况也是可以解决的。不用考虑括号。
: a*(b+c)/d 等同于
: b+c*a/d 每次evaluate表达式直接从左往右evaluate.
: 那么只要穷举了所有的abcd排列, 然后3个符号位置组合就行了。

avatar
c*g
26
不用,叔有葵花宝典

【在 l*****d 的大作中提到】
: 赞扬,继续学习~~主要看看b5 to b8 操作。我以前写过一些教程,我去找找。丝路
: 在web下也可以用,但不如Term 下用方便。

avatar
y*n
27
粗想了一下,我会把算24列成两个公式:
((a +-*/ b) +-*/ c) +-*/ d
或者 (a +- b) */ (c +- d)
这样循环穷举可在短时间搞定。
avatar
l*d
28
那个太多了,您急切之下学不过来的。

【在 c******g 的大作中提到】
: 不用,叔有葵花宝典
avatar
c*p
29
我以前尝试用MATLAB写过,不太成功
基本思路是递归,把四元变成三元,把三元变成两元。
但是这样的不可能找到形如(a+b) * (c+d)这样的解。
另外,因为浮点数精度的问题,对于3 3 7 7 可能找不到(3 + 3/7) * 7这样的解

【在 w********s 的大作中提到】
: input: 4个数字, a,b,c,d
: output: 所有结果=24的等式,例如:24=(a+b)*(c*d)

avatar
m*t
30
这个。。。有点过了吧。有人愿意出来做这个花费精力和时间的斑竹,就应该表扬。关
键是要做出一点儿事

【在 C*********y 的大作中提到】
: rt
avatar
l*8
31
how about a*/b +- c*/d

【在 y****n 的大作中提到】
: 粗想了一下,我会把算24列成两个公式:
: ((a +-*/ b) +-*/ c) +-*/ d
: 或者 (a +- b) */ (c +- d)
: 这样循环穷举可在短时间搞定。

avatar
m*t
32
agree :)

【在 z****y 的大作中提到】
: 这个有些过吧,大家要淡定,淡定
avatar
g*e
33
貌似挺难的 有标准解吗
avatar
T*y
34
It's a piece of cake for him, like how writing a poem is for you.

【在 l*****d 的大作中提到】
: 那个太多了,您急切之下学不过来的。
avatar
h*e
35
我觉得用C++/Java基本上写不出来,但是用Perl、Python、Ruby
之类的scripting languages可能可以。
avatar
m*t
36
感觉你也在意气用事,淡定淡定。即使现任斑竹在位,大家不也还在互相帮助嘛。我想
说,有人愿意出来做这个花费精力和时间的斑竹,就应该表扬。关键是要做出一点儿事。

【在 T*******y 的大作中提到】
: more details are here:
: http://www.mitbbs.com/article_t/Immigration/32092007.html

avatar
y*n
37
good catch! 我当时想的不够严谨,可能还有漏掉的情况。

【在 l*********8 的大作中提到】
: how about a*/b +- c*/d
avatar
T*y
38
You are right. The more ladyred works recently, the more the IDs are
benefited. It's good for everyone before she's gone from the BF position.

事。

【在 m****t 的大作中提到】
: 感觉你也在意气用事,淡定淡定。即使现任斑竹在位,大家不也还在互相帮助嘛。我想
: 说,有人愿意出来做这个花费精力和时间的斑竹,就应该表扬。关键是要做出一点儿事。

avatar
b*u
39
暴力解. Binary Tree.
3+5+7+9
(3+9)*(7-5)
5*9-3*7
9*(5-7/3)
2+4+8+10
(4*10+8)/2
(10-4)*8/2
2-10+4*8
4*8-10+2
2-(10-4*8)
2*10-(4-8)
(2+10)/(4/8)
(2+10)*8/4
(8/4+10)*2
2*10+8-4
(2+10)*8/4
4*10-2*8
(2*8-10)*4
4*(10-8/2)
8/2*(10-4)
(10-4)/2*8
8/(2/(10-4))
(2+10)/4*8
8/(4/(2+10))
(10-2)*4-8
(10-4)/(2/8)
(3/7+3)*7
5*(5-1/5)
avatar
T*y
40
Better yet, if she still helps others here after she's not a BF, that's even
better for everyone.

【在 T*******y 的大作中提到】
: You are right. The more ladyred works recently, the more the IDs are
: benefited. It's good for everyone before she's gone from the BF position.
:
: 事。

avatar
l*8
41
你这个答案看起来不错啊。
你的程序是怎么知道9*5-7*3只是5*9-3*7的trivial variation的?

【在 b******u 的大作中提到】
: 暴力解. Binary Tree.
: 3+5+7+9
: (3+9)*(7-5)
: 5*9-3*7
: 9*(5-7/3)
: 2+4+8+10
: (4*10+8)/2
: (10-4)*8/2
: 2-10+4*8
: 4*8-10+2

avatar
d*3
42
怪事啊, 赶都赶不走, 只能相信你们这儿占着坑是有$$$关系的

【在 l*****d 的大作中提到】
: All right, I will do my best to compete to be the BM and I will do my best
: to make your guys to stay here. Thank you.

avatar
l*8
43
为什么输出两次(2+10)*8/4 ?

【在 b******u 的大作中提到】
: 暴力解. Binary Tree.
: 3+5+7+9
: (3+9)*(7-5)
: 5*9-3*7
: 9*(5-7/3)
: 2+4+8+10
: (4*10+8)/2
: (10-4)*8/2
: 2-10+4*8
: 4*8-10+2

avatar
c*g
44
难道他也接case?

【在 d*****3 的大作中提到】
: 怪事啊, 赶都赶不走, 只能相信你们这儿占着坑是有$$$关系的
avatar
b*u
45
用 bool IsSameFor24 ( OperationNode node1, OperationNode node2 )
对 + *, 检查左右互换后是否相同。
也检查了 a + ( b + c ) = ( a + c ) + b 的情况。
有五种TREE
(a - b) - (c - d)
a - ( b - ( c -d ) )
a - ( (b - c) -d )
((a - b) - c) -d
(a - (b - c)) -d


【在 l*********8 的大作中提到】
: 你这个答案看起来不错啊。
: 你的程序是怎么知道9*5-7*3只是5*9-3*7的trivial variation的?

avatar
T*y
46
We really don't know the reasons behind. Anyway, seeing them go away is the
way to break this chain.

【在 c******g 的大作中提到】
: 难道他也接case?
avatar
l*8
47
你是把每个符合条件的答案存起来。生成新答案的时候,和已有答案逐个相比吧?
那为什么输出两次(2+10)*8/4 ?

【在 b******u 的大作中提到】
: 用 bool IsSameFor24 ( OperationNode node1, OperationNode node2 )
: 对 + *, 检查左右互换后是否相同。
: 也检查了 a + ( b + c ) = ( a + c ) + b 的情况。
: 有五种TREE
: (a - b) - (c - d)
: a - ( b - ( c -d ) )
: a - ( (b - c) -d )
: ((a - b) - c) -d
: (a - (b - c)) -d
:

avatar
b*u
48
是的。
我猜原因是 有两个TREE 31 和 22. TREE 本身不同,但结果相同。
( ( 2 + 10 ) * 8 ) /4
( ( 2 + 10 ) * ( 8 /4 )

【在 l*********8 的大作中提到】
: 你是把每个符合条件的答案存起来。生成新答案的时候,和已有答案逐个相比吧?
: 那为什么输出两次(2+10)*8/4 ?

avatar
l*8
49
知道了。然后你输出的时候去掉了不必要的括号(的确应该),就造成一模一样的两个
答案了。

【在 b******u 的大作中提到】
: 是的。
: 我猜原因是 有两个TREE 31 和 22. TREE 本身不同,但结果相同。
: ( ( 2 + 10 ) * 8 ) /4
: ( ( 2 + 10 ) * ( 8 /4 )

avatar
r*g
50
That's an interesting thought..
用scripting L的不同在哪里?

【在 h****e 的大作中提到】
: 我觉得用C++/Java基本上写不出来,但是用Perl、Python、Ruby
: 之类的scripting languages可能可以。

avatar
l*i
51
It is easier if you use postfix or prefix notation, then you have no ( ) to
deal with, 3 operators and 4 operands, 7! is small
just write eval_postfix() and in the function check whether it is a valid
postfix or not
avatar
h*e
52
Scripting languages的强项在于可以做真正的very-high level programming.
举Ruby为例:
- 生成所有的permutations:
a=[1,2,3]
a.permutation.to_a
- Set的操作:
require 'set'
s1 = Set.new [1, 2]
s1.merge([2, 6])
很多在C++/Java里面要一大段代码的,在这些scripting中一句话就解决了。
Google的Peter Norvig有一篇非常经典的文章,就是讲如何只用一小页的
Python代码来解决所有的Sudoku:
http://norvig.com/sudoku.html

【在 r*****g 的大作中提到】
: That's an interesting thought..
: 用scripting L的不同在哪里?

avatar
x*5
53
这个编程之美第一章16题是答案, python代码如下
from __future__ import division
def Point_Game(n,numbers,result,Value):
"Implement 24-point game"
if n==1:
if math.fabs(numbers[0]-Value)<0.0001:
print result[0]
return True
else: return False

for i in range(n):
for j in range(i+1,n):
a,b=numbers[i],numbers[j]
expa,expb=result[i],result[j]
numbers[j]=numbers[n-1]
result[j]=result[n-1]

result[i]='('+expa+'+'+expb+')'
numbers[i]=a+b
if Point_Game(n-1,numbers,result,Value):
return True

result[i]='('+expa+'-'+expb+')'
numbers[i]=a-b
if Point_Game(n-1,numbers,result,Value):
return True

result[i]='('+expb+'-'+expa+')'
numbers[i]=b-a
if Point_Game(n-1,numbers,result,Value):
return True

result[i]='('+expa+'*'+expb+')'
numbers[i]=a*b
if Point_Game(n-1,numbers,result,Value):
return True

if b:
result[i]='('+expa+'/'+expb+')'
numbers[i]=a/b
if Point_Game(n-1,numbers,result,Value):
return True

if a:
result[i]='('+expb+'/'+expa+')'
numbers[i]=b/a
if Point_Game(n-1,numbers,result,Value):
return True

numbers[i]=a
numbers[j]=b
result[i]=expa
result[j]=expb

return False
测试:
numbers=[6,6,6,6]
results=[]
for n in numbers:
results.append(str(n))
Recur.Point_Game(4, numbers, results, 24)
Output:
(((6+6)+6)+6)
测试: numbers=[1,3,4,6]--->(6/(1-(3/4)))
numbers=[3,3,8,8]-->(8/(3-(8/3)))
numbers=[3,3,7,7]-->(((3/7)+3)*7)
测试用例是google最难算的24点
基本idea是back-tracking,这个程序可以在45分钟内写出来吧,欢迎找bug
avatar
x*5
54
好像要所有结果啊,我再想想
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。