October 13, 2009 – 3:05 AM
今天无聊,在百度知道上看到这个问题:
Python里怎么样用二进制来做乘法
2个数字,先转换成二进制的2个list,然后用这两个list来做乘法,最后转换回数字
于是无比无聊的解答了一下,下面是代码:
N = 32 #the number of bits for an integer
def int2b(n, bit=N):
return [(n >> i) & 1 for i in range(bit)[::-1]]
def b_add(l1, l2, bit=N):
result = [0]*N
carry = 0
for i in range(N)[::-1]:
half_sum = l1[i] ^ l2[i]
b_sum = (half_sum ^ carry)
half_carry = (l1[i] & l2[i])
carry = (carry & half_sum) | half_carry
result[i] = b_sum
return result
def b_multiply(l1, l2, bit=N):
result = [0]*N
for i in range(N):
if l2[i]:
result = b_add(result[:],l1[N-i-1:]+[0]*(N-i-1))
return result
def b2int(l, bit=N):
result = 0
for i in range(bit):
if l[i]:
result += (l[i]<<(N-i-1))
return result
def main(x, y):
print b2int(b_multiply(int2b(x), int2b(y)))
if __name__ == '__main__':
main(5,7)
此代码只用于无符号整数,且没有考虑溢出问题,如果大家有更好的方法,可以到原帖回答一下,有赏金80块。。原帖已关闭=.=! 大家留言好了
October 2, 2009 – 4:41 PM
欢迎大家来围观我们学校新建的生物信息学问答社区:
BioStar is “A place to ask questions about bioinformatics and life science related data analysis.”
刚建了两天,所以上面的问题很少,希望大家多问问题啊,会有我们学校的牛人在上面回答的,包括我最爱的
Istvan牛!
去过
StackOverflow的同学会不会觉得界面很眼熟啊?Biostar是建在
StackExchange平台上的问答系统,StackExchange就是”The Stack Overflow Knowledge Exchange Platform”啦,所以长得很像啦。
一直很喜欢StackOverflow的回答评价机制,百度知道那是根本不能比的。原来也可以自己建个一样的!虽然不完全一样啦,可以自己改logo和主题颜色(实话说我觉得Biostar的配色挺难看的现在改得稍微好看点了)。不过这个服务价格不菲,最便宜的也要每月129美元。
总之,大家有问题的没问题的都去逛逛吧~
September 25, 2009 – 12:39 PM
在Top Languange上看到:
同事儿子的作业,大概意思是这样:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
4*4,把其中6个1替换成0,使得横竖1的个数都是偶数,一共有几种,并打印出来.
偶用的穷举法,答案是96。 以下是Python代码:
N = 4
def int2b(n, bit=N*N):
return [(n >> i) & 1 for i in range(bit)[::-1]]
def check(L):
for i in range(N):
if sum(L[i::N]) % 2 or sum(L[i*N:i*N+N]) % 2:
return 0
return 1
count = 0
for i in xrange(1<<N*N):
L = int2b(i)
if sum(L) == 10 and check(L):
print '\n'.join([' '.join(map(str,L)[i::N]) for i in range (N)])
print '-------'
count += 1
print count
看到有人写的Python代码只要300多字符,我写的为啥要400+,伤心了。。
September 25, 2009 – 3:41 AM
via Richard Wiseman’s blog
Can you take the clock face below, and cut it into four pieces such that the numerals on each part add up to the same number?

So, for example, this solution would not work because the numbers on the four pieces add up to different amounts….

I like this puzzle! You may think it is easy for several times, only to find yourself such an idiot soon.
You can vote on the original post if you need a hint.
Happy Friday!
By the way, can anyone tell me how I can activate the ibus input method on Ubuntu 9.10??
September 14, 2009 – 4:50 PM
来源在此
Supplies
A foreman was assigned three new workers, two big strong local men, and a little guy from Japan.
Because of their size, the foreman gave the two locals the digging work, and told the Japanese man “You’ll be in charge of supplies.”
After an hour or so, the foreman came back to check on their progress only to find the two locals sitting down doing nothing.
“What happened? Why aren’t you at work?”
The men replied that their tools were broken and that the Japanese man in charge of supplies, had disappeared.
Worried, the foreman ordered the two men outside the mine to help look for the little guy.
Just when they were about to give up the search, the Japanese guy jumps up from behind a rock and yells “Supplies!!”
一个有点变化的中文版本(ms too old)
一个德国人、法国人、及一个日本人要到矿场工作。
老板是美国人,他对德国人说:你体格不错,你负责苦力。
对法国人说:你说你是工程师,你负责采矿的计划。
而对日本人他说:你很瘦小。你负责supplies( 补给) 。
然后隔周,他们开始上工。
几天后德国人及法国人发现日本人不见了,找了很久后他们决定还是先回头工作。”
德国人开始工作的时候,日本人突然跳了出来,大声叫到: “SUPPLIES!!”

(还没人喜欢)

Loading ...
|
237次阅读
|
|
分类 fun, zz
|
标签 joke, zz
|