首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >试图为一个孩子的谜题编写一个解决方案--有些计算似乎不合适。

试图为一个孩子的谜题编写一个解决方案--有些计算似乎不合适。
EN

Stack Overflow用户
提问于 2022-04-06 18:20:49
回答 1查看 25关注 0票数 0

在过去的一个月里,我一直试图通过一些在线课程来学习python,而之前对编码一无所知。在完成了一些基本知识之后,我想做一些项目来扩展我的知识和技能。所以,我遇到了一个儿童的数学难题游戏,它的想法是把木制的立方体旋转,这样的方式,每一个脸的拼图都在同一时间被解决。每个立方体是1,2,3和4的序列,而操作(多维数据集2和4)是+-* /。第六个立方体总是"=“符号。这个魔鬼的事情比它看上去难得多!因此,作为一项练习,我认为尝试用代码编写我的解决方案会很有趣。在过去的几天里,我一直在尽我最大的努力,但是现在我想我是因为我的代码所面临的一些令人费解的(在我看来)问题而撞到了砖墙。

我的想法是“平整立方体”,并尝试所有可能的组合,看看是否有任何设法‘解决’的难题在所有方面在同一时间。以下是我到目前为止编码的内容:

代码语言:javascript
运行
复制
import numpy as np
import pandas as pd
import operator
import random

# defining the sequences of each cube - l1 to l3 the first 3 numeric cubes, s1 the last cube, f1 and f2 the function cubes
l1 = [3, 2, 1, 4]
l2 = [2, 1, 3, 4]
l3 = [2, 4, 1, 3]
s1 = [3, 1, 4, 2]
f1 = ['*', '-', '+', '/']
f2 = ['-', '/', '+', '*']

# Getting all possible and put them in a dataframe
shuf1 = pd.DataFrame([l1[len(l1)-i:] + l1[0:len(l1)-i] for i in range(len(l1))])
shuf_f1 = pd.DataFrame([f1[len(f1)-i:] + f1[0:len(f1)-i] for i in range(len(f1))])
shuf2 = pd.DataFrame([l2[len(l2)-i:] + l2[0:len(l2)-i] for i in range(len(l2))])
shuf_f2 = pd.DataFrame([f2[len(f2)-i:] + f2[0:len(f2)-i] for i in range(len(f2))])
shuf3 = pd.DataFrame([l3[len(l3)-i:] + l3[0:len(l3)-i] for i in range(len(l3))])
shuf4 = pd.DataFrame([s1[len(s1)-i:] + l1[0:len(s1)-i] for i in range(len(s1))])

#define operators you want to use
allowed_operators={
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv}

# these lists are for iteration purposes later on
v1 = [0,1,2,3]
v2 = [0,1,2,3]
v3 = [0,1,2,3]
v4 = [0,1,2,3]
v5 = [0,1,2,3]
v6 = [0,1,2,3]
v7 = [0,1,2,3]

# opening some empty "boxes" for later use
test_results = []
temp_list = np.array([0, 0])

for v in v6:
    for w in v5:
        for z in v4:
            for u in v3:
                for y in v2:
                    for x in v1:
                        for r in v7:
                            for i in range(len(l1)):
                                
                                # Setting the face of each cube
                                a=shuf1.iloc[i,r]
                                b=shuf2.iloc[i,x]
                                c=shuf3.iloc[i,y]
                                d=shuf4.iloc[i,u]
                                string_operator=shuf_f1.iloc[i,z]
                                string_operator2=shuf_f2.iloc[i,w]
                                
                                # Doing the calculations
                                result=allowed_operators[string_operator](a,b)
                                result2=allowed_operators[string_operator2](result,c)
                                logic_test = result2 == shuf4.iloc[i,v]
                                test_results.append(result2 == logic_test)
                                
                                # feed results to list
                                newrow = []
                                second_box = str(a) + str(string_operator) + str(b) + str(string_operator2) + str(c) + "=" + str(d)
                                newrow.append(second_box)
                                newrow.append(logic_test)
                                temp_list = np.vstack([temp_list, newrow])
# Append to dataframe
solution_list = pd.DataFrame(temp_list, columns = ['Operation','Result'])

# Print out outcomes
print(solution_list.head(10))
print(solution_list.info())
preview = solution_list[solution_list['Result'] == 'True']
print(preview.head(20))

# Locate the index of the sequence of 4 consequtive positive solutions
quadruple_sequence = np.where((solution_list['Result'] == 'True') & (solution_list['Result'].shift() == 'True') & (solution_list['Result'].shift(periods=2) == 'True') & (solution_list['Result'].shift(periods=3) == 'True'))[0]
print(quadruple_sequence.size)
print(quadruple_sequence)

# Print out the 4 sequence
solution_list.iloc[quadruple_sequence]

现在我的问题是,我收到了一个奇怪的输出。在这里,我正在粘贴一些输出,当我为那些对谜题有积极解决方案的人过滤可能的组合的数据时,我收到了这些输出(结果= True)。在开始时,可以看到操作结果和“结果”之间存在着真正的关系:例如,1+2+1确实等于4,因此" true“语句确实是真的。但是看看dataframe的其他行,python似乎认识到一些非常不真实的组合是"True“。我完全不知道为什么会发生这种事。有什么想法吗?

在dataframe头中的输出示例:

代码语言:javascript
运行
复制
  Operation Result
0         0      0
1   3*2-2=3  False
2   4/4*3=2  False
3   1+3+1=4  False
4   2-1/4=1  False

True的序列来自相同的dataframe:

代码语言:javascript
运行
复制
    Operation   Result
2164    2/2/1=1 True
2420    2/2/1=4 True
2676    2/2/1=2 True
2932    2/2/1=3 True

我想知道这怎么可能,我哪里出了问题。我相信你可以想出更好的方法来构造这个问题,任何想法都是受欢迎的!只要记住,我是真正的初学者水平,我的理解和努力学习!谢谢!=) 1:https://i.stack.imgur.com/MToqG.jpg

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-06 19:52:15

中的最后一个多维数据集上迭代两次。

代码语言:javascript
运行
复制
d=shuf4.iloc[i,u]

代码语言:javascript
运行
复制
logic_test = result2 == shuf4.iloc[i,v]

将最后一行改为

代码语言:javascript
运行
复制
logic_test = result2 == d

并且删除v上的循环将得到所需的结果。顺便说一句。熊猫也许不是这个练习的最佳框架,你应该去看看列表操作员。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71771856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档