没有合适的资源?快使用搜索试试~ 我知道了~
20151910042-刘鹏-DSA实验05-数组序列实验1
需积分: 0 0 下载量 132 浏览量
2022-08-08
20:16:17
上传
评论
收藏 9.57MB DOCX 举报
温馨提示
试读
51页
云南大学数学与统计学院上机实践报告课程名称:数据结构与算法实验年级:2015级上机实践成绩:指导教师:陆正福姓名:刘鹏上机实践名称:数组序列实验学号:20151
资源详情
资源评论
资源推荐
云南大学数学与统计学院数学系信息与计算科学专业
第 1 页 共 51 页
云南大学数学与统计学院
上机实践报告
课程名称:数据结构与算法实验
年级:2015 级
上机实践成绩:
指导教师:陆正福
姓名:刘鹏
上机实践名称:数组序列实验
学号:20151910042
上机实践日期:2017-06-08
上机实践编号:No.05
组号:
上机实践时间:上午 3、4 节
一、实验目的
1. 熟悉与 Python 序列类型有关的数据结构与算法;
2. 熟悉主讲教材 Chapter 5 的代码片段。
二、实验内容
1. 数组序列有关的数据结构设计与算法设计
2. 调试主讲教材 Chapter 5 的 5.5.3 节的 Simple Cryptography 程序
三、实验平台
Windows 10 1703 Enterprise 中文版;
Python 3.6.0;
Wing IDE Professional 6.0.5-1 集成开发环境;
MATLAB R2017a;
Microsoft Office Excel 2016.
四、实验记录与实验结果分析
1 题
动态数组与均摊方法
程序代码:
1
2
3
4
5
6
7
8
9
# 5.3.0 Dynamic Arrays and Amortization
import sys # provides getsizeof function
data = []
for k in range(20): # NOTE: must fix choice of n
a = len(data) # number of elements
b = sys.getsizeof(data) # actual size in bytes
print('Length:{0:3d}; Size in bytes:{1:4d}'.format(a,b))
data.append(None) # increase by one
程序代码 1
云南大学数学与统计学院数学系信息与计算科学专业
第 2 页 共 51 页
实验结果:
运行结果 1
代码分析:
这是个分析数组大小与内存占用量的函数,通过与系统交互,得到实际数据。很显然,理论数据与内存占用不相同,
正式因为 Python 是动态语言。这里的实验仅仅是一个尝试,课本后面有这后面有着详细的解释。这是一种“等比性质”的
动态数组,有着很多优越性。
云南大学数学与统计学院数学系信息与计算科学专业
第 3 页 共 51 页
2 题
动态数组的实现。
程序代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 5.3.1 Implementing a Dynamic Array
import ctypes # provides low-level arrays
class DynamicArray:
"""A dynamic array class akin to a simplified Python list."""
def __init__(self):
"""Create an empty array."""
self._n = 0 # count actual elements
self._capacity = 1 # default array capacity
self._A = self._make_array(self._capacity) # low-level array
def __len__(self):
"""Return number of elements stored in the array."""
return self._n
def __getitem__(self,k):
"""Return element at index k."""
if not 0 <= k < self._n:
raise IndexError('invalid index')
return self._A[k] # retrieve from array
def append(self,obj):
"""Add object to end of the array."""
if self._n == self._capacity: # not enough room
self._resize(2 * self._capacity) #so double capacity
self._A[self._n] = obj
self._n += 1
def _resize(self,c): # nonpublic utitity
"""Resize internal array to capacity c."""
B = self._make_array(c) # new (bigger) array
for k in range(self._n): # for each existing value
B[k] = self._A[k]
self._A = B # use the bigger array
self._capacity = c
def _make_array(self,c): # nonpublic utitity
"""Return new array with capacity c."""
return(c * ctypes.py_object)() # see ctypes documentation
#----------------------------- my main function -----------------------------
a = DynamicArray()
云南大学数学与统计学院数学系信息与计算科学专业
第 4 页 共 51 页
46
47
48
49
50
51
52
53
print('1: The size of a is:',a._capacity)
a.append(1)
a.append(2)
a.append(3)
print('2: The size of a is:',a._capacity)
a._resize(40)
print('3: The size of a is:',a._capacity)
print('4: The number of elements in a is:',a._n)
程序代码 2
运行结果:
运行结果 2
代码分析:
这个代码仅仅作为一个参考,但是实际并不能用,因为 DynamicArray 类的封装做得很差,函数是用单下划线开头的
保护类型,这里为了安全起见,应该将之设计为 private 类型,即双下划线开头。这样一来,类的使用者就看不到 capacity
和 n,更无法修改这两个数值,这就安全了很多。
值得指出的就是_make_array 函数里的 ctypes.py_object 函数了,这里用 help(ctypes.py_object)获取了帮助文档。
云南大学数学与统计学院数学系信息与计算科学专业
第 5 页 共 51 页
关于 ctypes 的一些方法,doc 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Help on class py_object in module ctypes:
class py_object(_ctypes._SimpleCData)
| XXX to be provided
|
| Method resolution order:
| py_object
| _ctypes._SimpleCData
| _ctypes._CData
| builtins.object
|
| Methods defined here:
|
| __repr__(self)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Methods inherited from _ctypes._SimpleCData:
|
| __bool__(self, /)
| self != 0
|
| __ctypes_from_outparam__(...)
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __new__(*args, **kwargs) from _ctypes.PyCSimpleType
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from _ctypes._SimpleCData:
|
| value
| current value
|
| ----------------------------------------------------------------------
| Methods inherited from _ctypes._CData:
|
| __hash__(self, /)
| Return hash(self).
|
| __reduce__(...)
| helper for pickle
|
| __setstate__(...)
剩余50页未读,继续阅读
莫少儒
- 粉丝: 26
- 资源: 311
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0