测试3:实现加法。
本帖最后由 KeyError 于 2023-1-9 12:29 编辑请实现函数add(int a,int b) -> int,该函数返回a+b。
你不能使用+,-,*,/,//,%,if,for,while。 def add(a, b):
return a.__add__(b) 不能用 '+' 这个运算符,那就自己做一个加法器
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8
'''
def and_gate(a, b):
if a == 0: return 0
if b == 0: return 0
return 1
def or_gate(a, b):
if a == 1: return 1
if b == 1: return 1
return 0
def not_gate(a):
if a == 0: return 1
return 0
def xor_gate(a, b):
if a == b: return 0
return 1
def xor_gate(a, b):
w0 = not_gate(a)
w1 = not_gate(b)
w2 = and_gate(a, w1)
w3 = and_gate(b, w0)
return or_gate(w2, w3)
'''
def and_gate(a, b): return a & b
def or_gate(a, b): return a | b
def not_gate(a): return int(not a)
def xor_gate(a, b): return a ^ b
def half_adder_1(a, b):
s = xor_gate(a, b)
c = and_gate(a, b)
return (c, s)
def full_adder_1(a, b, c):
w0, w1 = half_adder_1(a, b)
w2, w3 = half_adder_1(w1, c)
w4 = or_gate(w0, w2)
return (w4, w3)
def full_adder_2(a, b, c):
w0, w1 = full_adder_1(a & 0x0001, b & 0x0001, c)
w2, w3 = full_adder_1(a >> 1, b >> 1, w0)
result = (w3 << 1) | w1
return (w2, result)
def full_adder_4(a, b, c):
w0, w1 = full_adder_2(a & 0x0003, b & 0x0003, c)
w2, w3 = full_adder_2(a >> 2, b >> 2, w0)
result = (w3 << 2) | w1
return (w2, result)
def full_adder_8(a, b, c):
w0, w1 = full_adder_4(a & 0x000f, b & 0x000f, c)
w2, w3 = full_adder_4(a >> 4, b >> 4, w0)
result = (w3 << 4) | w1
return (w2, result)
def full_adder_16(a, b, c):
w0, w1 = full_adder_8(a & 0x00ff, b & 0x00ff, c)
w2, w3 = full_adder_8(a >> 8, b >> 8, w0)
result = (w3 << 8) | w1
return (w2, result)
def full_adder_32(a, b, c):
w0, w1 = full_adder_16(a & 0xffff, b & 0xffff, c)
w2, w3 = full_adder_16(a >> 16, b >> 16, w0)
result = (w3 << 16) | w1
return (w2, result)
'''
print(full_adder_32(12345, 7366896, 0), 12345 + 7366896)
print(full_adder_32(12745, 7471895, 0), 12745 + 7471895)
print(full_adder_32(82345, 7586894, 0), 82345 + 7586894)
'''
def add(a, b): return full_adder_32(a, b, 0)
a = 13748389521
b = 1912349134
print(add(a, b), a + b)
a = 13741234521
b = 1919087134
print(add(a, b), a + b)
a = 93741
b = 291987634
print(add(a, b), a + b)
a = 13748389521
b = 1912349134
print(add(a, b) == a + b)
a = 13741234521
b = 1919087134
print(add(a, b) == a + b)
a = 93741
b = 291987634
print(add(a, b) == a + b)
sh-5.1$ ./main.py
15660738655 15660738655
15660321655 15660321655
292081375 292081375
True
True
True
sh-5.1$
2楼厉害,网上的算法还是不能符合要求:
def add(a,b):
p1=a^b
p2=(a&b)<<1
while p2!=0:
temp=p1^p2
p2=(p1&p2)<<1
p1=temp
return p1
print(add(3,69)) def add(a,b):
c=sum()
return c
print(add(3.5,69)) 如果你对这些原理感兴趣的话,那来学底层吧
在这里什么都自己写
自己写编译器,自己写操作系统,自己写计算机!
^_^
人造人 发表于 2023-1-9 01:26
不能用 '+' 这个运算符,那就自己做一个加法器
“专 业 对 口” Mike_python小 发表于 2023-1-9 11:15
“专 业 对 口”
{:10_298:} tommyyu 发表于 2023-1-8 22:43
__add__方法已经算+了。 人造人 发表于 2023-1-9 01:26
不能用 '+' 这个运算符,那就自己做一个加法器
这有if。 KeyError 发表于 2023-1-9 12:17
__add__方法已经算+了。
但是如果想写一个适用所有对象的add函数,就必须使用__add__魔法方法。否则只能指定某一类对象。 chinajz 发表于 2023-1-9 08:26
不能用任何与+,-,*,/,//,%运算符有关的方法。 KeyError 发表于 2023-1-9 12:18
这有if。
这个代码你看了吗?
看懂了吗?
有if吗?
KeyError 发表于 2023-1-9 12:40
答案与解析:
首先,我们的工具很少,
只有and,or,not,&,|,~,^,递归......(无法使用for,while迭代)
不至于吧?
贴代码之前要自己先运行一遍,没问题才贴的
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8
def add(int a, int b) -> int:
x = a + 0
y = b + 0
def f():
global x, y
x = ~ (- x)
y = - (~ y)
return bool(y) and f()
f()
return x
print(add(1, 1))
sh-5.1$ ./main.py
File "/tmp/./main.py", line 4
def add(int a, int b) -> int:
^
SyntaxError: invalid syntax
sh-5.1$ vim main.py
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8
#def add(int a, int b) -> int:
def add(a: int, b: int) -> int:
x = a + 0
y = b + 0
def f():
global x, y
x = ~ (- x)
y = - (~ y)
return bool(y) and f()
f()
return x
print(add(1, 1))
sh-5.1$ ./main.py
Traceback (most recent call last):
File "/tmp/./main.py", line 16, in <module>
print(add(1, 1))
File "/tmp/./main.py", line 13, in add
f()
File "/tmp/./main.py", line 10, in f
x = ~ (- x)
NameError: name 'x' is not defined
sh-5.1$ vim main.py
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8
#def add(int a, int b) -> int:
def add(a: int, b: int) -> int:
x = a + 0
y = b + 0
def f():
#global x, y
nonlocal x, y
x = ~ (- x)
y = - (~ y)
return bool(y) and f()
f()
return x
print(add(1, 1))
sh-5.1$ ./main.py
Traceback (most recent call last):
File "/tmp/./main.py", line 17, in <module>
print(add(1, 1))
File "/tmp/./main.py", line 14, in add
f()
File "/tmp/./main.py", line 13, in f
return bool(y) and f()
File "/tmp/./main.py", line 13, in f
return bool(y) and f()
File "/tmp/./main.py", line 13, in f
return bool(y) and f()
RecursionError: maximum recursion depth exceeded
sh-5.1$
好像要用__add__魔法方法 这个简单{:10_256:}
def add(a: int, b: int) -> int:
return int(input(f'{a}+{b}=?'))
def add(a, b):
return exec(f'{str(a)}{chr(43)}{str(b)}')
Doge 歌者文明清理员 发表于 2023-3-19 08:39
这个简单
什么我搞笑一下都能最佳答案????
页:
[1]