我想检查在它们之间放置基本算术符号时7位数是否可以达到100。
def is_hundred(n1,n2,n3,n4,n5,n6,n7):
p = [+,-,*,/]
for p1 in p:
for p2 in p:
for p3 in p:
for p4 in p:
for p5 in p:
for p6 in p:
if n1 p1 n2 p2 n3 p3 n4 p4 n5 p5 n6 p6 n7 == 100:
return "success"
如何用列表中的算术符号替换变量?
答案 0 :(得分:1)
如果您没有听到它:使用eval
是邪恶的。因此,这绝对是不代码,我将其付诸实践。也就是说,这项练习也是我可能永远不需要部署的内容......
这个想法:使用参数和"暴力"在它们之间添加操作的所有可能性。将这些选项构造为字符串,并为每个选项构建完成后,评估总数(使用eval
),如果总数为100,则将其打印到stdout:
def is_hundred(args, res=''):
if res == '':
res = args[0]
args = args[1:]
if not args:
if eval(res) == 100:
print(res)
else:
first = args[0]
for op in ['+','-','*','/']:
is_hundred(args[1:], "{}{}{}".format(res, op, first))
# you can pass any number of arguments in a list: 7, 8, 15...
is_hundred([2,3,4,8,10,11]) # 2+3+4+8*10+11
答案 1 :(得分:1)
以下是搜索一对运算符的示例代码。 List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);
default List<User> findTop20ByUserIdOrderBySNumber(String userId) {
return findByUserIdOrderBySNumber(userId, new PageRequest(0,20));
}
中的i
表示就地iadd
运算符。您只需切换到add
即可。 add
会创建运算符的重复排列。
product