网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
09月22日漏签0天
python吧 关注:477,452贴子:1,975,671
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 1回复贴,共1页
<<返回python吧
>0< 加载中...

小白作业题目 请了解的评估下题目难度

  • 只看楼主
  • 收藏

  • 回复
  • str0ngman
  • 童生
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个是More Python features - Python Programming MOOC 2024
就是python导引课程的作业题的最后一道 自己花了6个小时才调好
这个题是属于什么难度的题 有了解的吗?
NB: Some exercises have multiple parts, and you can receive points for the different parts separately. You can submit a partially completed exercise by choosing 'Submit Solution' from the menu next to the button for executing tests .
In this exercise you will write your own programming language executor. You can use any techniques and skills you have learnt on this course thus far.
The programs will consist of rows, and each row will be in one of the following formats:
PRINT [value]: prints the value
MOV [variable] [value]: assigns the value to the variable
ADD [variable] [value]: adds the value to the variable
SUB [variable] [value]: subtracts the value from the variable
MUL [variable] [value]: multiplies the variable by the value
[location]:: names a line of code, so it can be jumped to from elsewhere
JUMP [location]: jumps to the location specified
IF [condition] JUMP [location]: if the condition is true, jump to the location specified
END: finish execution
The square brackets above are just a notation to signify operands; see below for usage examples.
The program is executed line by line from the first line onwards. The execution ends when the executor comes across the command END, or when there are no more lines to execute.
Each program has 26 pre-defined variables, named A to Z. Each variable has the value 0 when the program begins. The notation [variable] refers to one of these 26 variables.
All the values processed by the program are integer numbers. The notation [value] refers either to a value stored in a variable, or an integer number typed in directly.
The notation [location] refers to any name of a location which consists of lowercase letters a to z and/or numbers 0 to 9. Two different locations may not have the same name.
The notation [condition] refers to any expression in the format [value] [comparison] [value], where [comparison] is one of the following operators: ==, !=, <, <=, > and >=.
Please write a function named run(program), which takes a list containing the program commands as its argument. Each item on the list is a line of code in the program. The function should return a new list, which contains the results of the PRINT commands executed during the program's run.
You may assume the function will only be given programs which are entirely in the correct format. You do not have to implement any input validation or error handling.
This exercise is worth two points. You will receive one point if the commands PRINT, MOV, ADD, SUB, MUL and END are working correctly. You will receice another point if the rest of the commands, which are used to implement loops, also work.
Below are some examples, which you may also use for testing. Example 1:
program1 = []program1.append("MOV A 1")program1.append("MOV B 2")program1.append("PRINT A")program1.append("PRINT B")program1.append("ADD A B")program1.append("PRINT A")program1.append("END")result = run(program1)print(result)Sample output
[1, 2, 3]
Example 2:
program2 = []program2.append("MOV A 1")program2.append("MOV B 10")program2.append("begin:")program2.append("IF A >= B JUMP quit")program2.append("PRINT A")program2.append("PRINT B")program2.append("ADD A 1")program2.append("SUB B 1")program2.append("JUMP begin")program2.append("quit:")program2.append("END")result = run(program2)print(result)Sample output
[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]
Example 3 (factorial):
program3 = []program3.append("MOV A 1")program3.append("MOV B 1")program3.append("begin:")program3.append("PRINT A")program3.append("ADD B 1")program3.append("MUL A B")program3.append("IF B <= 10 JUMP begin")program3.append("END")result = run(program3)print(result)Sample output
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
Example 4 (prime numbers):
program4 = []program4.append("MOV N 50")program4.append("PRINT 2")program4.append("MOV A 3")program4.append("begin:")program4.append("MOV B 2")program4.append("MOV Z 0")program4.append("test:")program4.append("MOV C B")program4.append("new:")program4.append("IF C == A JUMP error")program4.append("IF C > A JUMP over")program4.append("ADD C B")program4.append("JUMP new")program4.append("error:")program4.append("MOV Z 1")program4.append("JUMP over2")program4.append("over:")program4.append("ADD B 1")program4.append("IF B < A JUMP test")program4.append("over2:")program4.append("IF Z == 1 JUMP over3")program4.append("PRINT A")program4.append("over3:")program4.append("ADD A 1")program4.append("IF A <= N JUMP begin")result = run(program4)print(result)Sample output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
这个是我的中文翻译
# 个人的程序执行器
这个程序是由多个行构成, 每一个行为下列中的一种格式:
. print[value]: print(v)
. MOV[variable] [value]:var = v
. ADD[variable] [value]:value +=v
. SUB[var][v] : var-v
. MUL[var][v] : var*v
. [location]: #命名一个位置 就算是个标签位置
. JUMP[location] :跳转到指定位置
. IF [condition] JUMP[location]:如果条件成立, 则跳转到指定位置
. END: 结束执行
说明1:
[] 方括号表示为操作数
程序执行顺序为逐行向下执行, 遇到END结束
每一个程序有26个预设置的变量 A-Z, 程序开始的时候均设为0
符号[variable] 指26个变量中的一个
所有的值由程序处理都是整数值
[value]要么是已保存的值 要么是一个打字输入的整型
符号[location]: 指任何位置名 包含小写a-z 以及或者0-9, 不同位置名字不会相同
符号[condition] 指任意一个表达式以格式 [v][comp][v], 然后[comp]为==,!=,<,<=,>,>=之一
说明2:
run(program) 参数为一个list 包含所有命令
返回值为一个新的list 包含所有的字符串结果
说明3:
假设所有的输入的格式都符合要求
说明4:
2分评价:
1分: 检测PRINT, MOV, ADD, SUB, MUL 和END的正确识别
1分: 其余的为这些功能使用的循环命令是否使用正确


  • 乱码lby
  • 榜眼
    13
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
自己写编译器?这肯定不是刚学的人做的


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 1回复贴,共1页
<<返回python吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示