Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

part07-18_own_programming_language can't pass Running tests #73

Open
muyy7879 opened this issue Sep 19, 2024 · 0 comments
Open

part07-18_own_programming_language can't pass Running tests #73

muyy7879 opened this issue Sep 19, 2024 · 0 comments

Comments

@muyy7879
Copy link

muyy7879 commented Sep 19, 2024

I have complete part7-18 ,which is "own_programming_language"task, a very enthralling and challenging task, it takes me a few hours to accomplish it, and it seems running correctly cause all the answer it return are right( as same as that in the example),but when I run my code with TMC, it takes so much long time and can't return a result, just like:
截屏2024-09-19 22 48 57
just like this for a few hours, I can't solve it by my self and I have no idea about what's wrong, if you can help me with it, I'll be so much appreciate cause I will got all my task done 100%,all of them!
anyway, this is my code:

Write your solution here

import string
dic = {character: 0 for character in string.ascii_uppercase}

output=[]
newbegin=0

def run(order:list):
sign=1
while sign:
sign=itera(order,begin=newbegin)
return output

def itera(order:list,begin):
global newbegin
for i in range(begin,len(order)):
order_list=order[i].split(" ") #每一行命令的数组
act=order_list[0] #取出命令

    if act in["MOV" ,"ADD","SUB" ,"MUL"]: 
        var=order_list[1]
        val = int(dic[order_list[2]]) if order_list[2] in string.ascii_uppercase else int(order_list[2])
        
        if act=="MOV": #MOV [variable] [value]
            dic[var]=val
        elif act=="ADD": #ADD [variable] [value] 
            dic[var]+=val
        elif act=="SUB": #SUB [variable] [value]
            dic[var]-=val
        elif act=="MUL": #MUL [variable] [value]
            dic[var]*=val

    elif act=="IF": #IF [condition] JUMP [location]
        com_1=order_list[1] # 前者
        com1=int(dic[com_1]) # 前者值
        express=order_list[2] #比较符号
        com_2=order_list[3] #后者
        com2 = int(dic[com_2]) if com_2 in string.ascii_uppercase else int(com_2)

        if express=="==" and com1==com2:
            act="JUMP"
        elif express=="!=" and com1!=com2:
            act="JUMP"
        elif express=="<" and com1<com2:
            act="JUMP"
        elif express=="<=" and com1<=com2:
            act="JUMP"
        elif express==">" and com1>com2:
            act="JUMP"
        elif express==">=" and com1>=com2:
            act="JUMP"
        else: #条件不满足,下一条命令
            pass

    elif act=="PRINT": #PRINT [value]
        va=order_list[1]
        pva = dic[va] if va in string.ascii_uppercase else int(va)
        output.append(pva)

    elif act=="END":
        return 0

    else:
        pass

    if act=="JUMP": #JUMP [location]
        index=order_list.index("JUMP") #定位
        loca=order_list[index+1]+":" #取出跳的位置 
        for k in range(len(order)):
            if order[k] == loca:
                global newbegin
                newbegin=k+1 #定义重新循环的位置
                return 1
        break

    if i==len(order)-1: #不是END的最后一行,且没有跳出循环:
        return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant