1436: python列表(apple pie ver)
题目
题目描述
本题中你可以自由地调用任意库,并且允许查阅cppreference。
https://en.cppreference.com/w/
https://zh.cppreference.com/w/
本题中,你提交的代码将被作为名为"src.hpp"的头文件而被提交,评测主程序将会include该头文件并检验该头文件的正确性。
python解释器一直为大家所津津乐道,但stella发现里面却没有列表的实现要求。stella觉得没有列表的python怎么能用呢?于是趁这次机会ta决定让大家用c++实现一个python列表。
目的:阅读测试样例,完成一个极简的 python 中的列表,int 版本,类名为pylist。
这个列表类似一个vector,区别在于:
- 列表可以互相嵌套,嵌套深度无限制,如
[0, 1, [2, []]]
。 - 列表在赋值与拷贝构造的时候都是传引用。如,执行语句
ls2 = ls
后,ls2
与ls
实际指向了同一个列表,对两者中的任何一个的操作将同时反映在另一个列表当中。直到ls = ls3
以后,ls
与ls3
指向了同一个列表,而ls2
不变。
需要支持功能:
-
成员函数append:在列表末尾增加一个int或pylist,类似vector中的push_back。
-
成员函数pop:弹出列表末尾的int或pylist,并返回这个被弹出的值,类似的vector中的pop_back(带返回值)。
-
下标访问运算符[]:返回指定下标处的int或pylist,返回值也可当做左值被赋值为int或pylist。 (以这种方法赋值时,int 可被赋值为 pylist,pylist 也可被赋值为 int ) 。
-
重载输出流运算符<<:打印该列表,格式见样例。
保证样例中不会出现任何形式的自引用,即保证:无论何时用<<输出列表,都不会出现无限的输出。
细节说明:保证样例中对你的列表pop出或被[]访问而返回的int的使用不超过以下方式:用<<进行输出,或被用在二元运算符+ - * / % & | ^ << >>
,一元运算符+ -
,比较运算符 < > <= >= == !=
中,或重新被append入一个pylist。我们不会对其类型做判断,例如,我们不会用一个int*指针指向它,不会用decltype或typeid获取类型,也不会对其调用*=2
等操作。
代码模板
```
ifndef PYLIST_H
define PYLIST_H
include
class pylist {
private:
public:
pylist() {
}
void append(const pylist &x) {
}
pylist pop() {
return pylist(); //meaningless return to prevent compile error.
}
pylist &operator[](size_t i) {
return *this; //meaningless return to prevent compile error.
}
friend std::ostream &operator<<(std::ostream &os, const pylist &ls) {
return os;
}
};
endif //PYLIST_H
```
样例测试程序
```cpp // // Created by JiamingShan on 2021/8/24. //
include "src.hpp"
include
using namespace std;
int main() { puts("Same with sample except this string."); puts("First, have some simple operation just like std::vector."); pylist ls; ls.append(1); cout << ls[0] << endl; // 1 ls.append(2); cout << ls[1] << endl; // 2 ls.pop(); ls.append(3); cout << ls[1] << endl; // 3 (Now, ls == [1, 3]) puts("After warming up exercise, your assignment operator will be tested."); puts("Now we will test whether your assignment operator will pass by reference."); pylist ls2 = ls; ls.append(4); cout << ls2[2] << endl; // 4 (Now, ls == ls2 == [1, 3, 4]) (Because assignment operator pass by reference. We will not mention this afterward.) ls.pop(); ls2.append(5); cout << ls[2] << endl; // 5 (Now, ls == ls2 == [1, 3, 5]) puts("Now we will test whether your pylist enable nesting."); pylist ls3; ls3.append(6); ls[0] = ls3; cout << ls2[0][0] << endl;// 6 (Now, ls == ls2 == [[6], 3, 5]) ls2.append(ls3); ls2[3].append(7); ls[1] = pylist(); cout << ls[0][1] << endl; // 7 (Now, ls == ls2 == [[6, 7], [], 5, [6, 7]]) ls2[2] = ls[1]; ls2[1].append(8); cout << ls2[2][0] << endl; // 8 (Now, ls == ls2 == [[6, 7], [8], [8], [6, 7]], ls3 = [6, 7]) puts("Robust test now."); ls[0] = pylist(); ls2 = ls; ls3 = ls3; //(Now, ls == ls2 == [[], [8], [8], [6, 7]], ls3 = [6, 7]) ls2 = ls3; //(Now, ls == [[], [8], [8], [6, 7]], ls2 == ls3 = [6, 7]) ls2.append(3*3); //(Now, ls == [[], [8], [8], [6, 7, 9]], ls2 == ls3 = [6, 7, 9]) cout << ls << endl; //[[], [8], [8], [6, 7, 9]] cout << ls2 << endl; //[6, 7, 9] cout << ls3 << endl; //[6, 7, 9] ls[0].append(ls2); pylist ls4 = pylist(); ls4.append(ls2[2]); cout << ls4.pop() << endl; // 9 ls[2][0] = 1000; cout << 2 * ls[0][0][2] + (ls2[1] << 1 >> 1) - (1 ^ ls2[2] ^ 1) - (ls2[1] / 1) - (ls[1][0] % 8) + 1 << endl; // 10 } ```
输入格式
无
输出格式
无
样例输入
无
样例输出
``` Same with sample except this string. First, have some simple operation just like std::vector. 1 2 3 After warming up exercise, your assignment operator will be tested. Now we will test whether your assignment operator will pass by reference. 4 5 Now we will test whether your pylist enable nesting. 6 7 8 Robust test now. [[], [8], [8], [6, 7, 9]] [6, 7, 9] [6, 7, 9] 9 10
```
数据范围
要求append
,pop
,下标访问运算符[]
的时间复杂度均为O(1)。
测试点说明:
| 测试序号 | 范围说明 |
| ---- | ------------------------------------------------------------ |
| 1 2 | 以如同c++ vector的方式使用vector,需完成append
,pop
,下标访问运算符[]
。其中测试点2为压力测试 |
| 3 4 | 在1 2基础上增加了对无省略号的列表输出、赋值或拷贝构造传引用的考察 |
| 5 6 | 在1 2基础上增加了对无省略号的列表输出、列表嵌套的考察 |
| 7 8 | 综合测试点 |
Oops! 本题目还没有解答!
助教老师们编题的速度,已经超过了解题的速度!
OJ翻了一新,但本解答集还大多用的是2017-2019级,甚至更早的同学们贡献的答案。
如果你已经AC了,可以的话,请您参考添加页面,与大家一起分享你的题解!