-
Notifications
You must be signed in to change notification settings - Fork 0
/
contiguous.py
81 lines (61 loc) · 2.24 KB
/
contiguous.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
## Last version: 13 June 2019
## A class implementing contiguous memory
class Contiguous:
"""
Fields: _items is a list of items
_size is number of items that can be stored
"""
## Contiguous(s) produces contiguous memory of size s
## and initializes all entries to None.
## __init__: Int -> Contiguous
## Requires: s is positive
def __init__(self, s):
self._items = []
self._size = s
for index in range(self._size):
self._items.append(None)
## repr(self) produces a string with the sequence of values.
## __repr__: Contiguous -> Str
def __repr__(self):
to_return = "("
for index in range(self._size - 1):
if self.access(index) == None:
to_print = "None"
else:
to_print = self.access(index)
to_return = to_return + str(to_print) + ","
if self.access(self._size - 1) == None:
to_print = "None"
else:
to_print = self.access(self._size - 1)
return to_return + str(to_print) +")"
## self.size() produces the size of self.
## size: Contiguous -> Int
def size(self):
return self._size
## self == other produces True if self and other have the same
## size and store the same values at each index.
## __eq__: Contiguous Contiguous -> Bool
def __eq__(self, other):
if self.size() != other.size():
return False
else:
for pos in range(self.size()):
if self.access(pos) != other.access(pos):
return False
return True
## self.access(index) produces the value at the given index.
## access: Contiguous Int -> Any
## Requires: 0 <= index < self._size
def access(self, index):
return self._items[index]
## self.store(index, value) stores value at the given index.
## Effects: Mutates self by storing value at the given index.
## store: Contiguous Int Any -> None
## Requires: 0 <= index < self._size
def store(self, index, value):
self._items[index] = value