-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs.py
59 lines (52 loc) · 1.87 KB
/
gs.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
"""Lazy finite geometric sequences"""
class FiniteGeometricSequence:
"""Finite geometric sequence that computes
terms only as needed (is "lazy")
"""
def __init__(self,start,ratio,length):
"""Initialize geometric sequence with first term
`start`, ratio of successive terms `ratio` and
total number of terms `length`.
"""
self.start = start
self.ratio = ratio
self.length = length
def __getitem__(self,idx):
"""Compute a term in the geometric sequence"""
# TODO: Decide whether negative indices should
# be an error (as now) or if they should count
# from the end of the sequence (as with lists)
if idx < 0 or idx >= self.length:
raise IndexError("index {} not valid for geometric sequence of length {}".format(
idx,
self.length
))
return self.start * self.ratio**idx
# Reminder: PEMDAS means this evaluates as
# self.start * (self.ratio**idx)
def __str__(self):
"""Human-readable string representation"""
return "{}(start={},ratio={},length={})".format(
self.__class__.__name__,
self.start,
self.ratio,
self.length
)
def __setitem__(self,idx,val):
"""Modify either the start or ratio using
item assignment
"""
if idx==0:
# change start
self.start = val
else:
# change the ratio so that
# the term at index idx becomes
# val
self.ratio = (val/self.start)**(1/idx)
def __repr__(self):
"""Unambiguous string representation"""
return str(self)
def __len__(self):
"""Number of terms in the sequence"""
return self.length