-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubles.py
More file actions
74 lines (53 loc) · 1.84 KB
/
Copy pathdoubles.py
File metadata and controls
74 lines (53 loc) · 1.84 KB
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
#!/usr/bin/env python3
from collections import Iterable
import sys
__all__ = ['doublePreceding', 'doubleSucceeding', 'testDoubles']
testList = [9, 4, 3]
def doublePreceding(vals):
failure = None
try:
prev = 0
for val in vals:
yield prev*2
prev = val
except TypeError as e:
excReason = str(sys.exc_info()[1])
if 'subscriptable' in excReason or 'iterable' in excReason:
valsType = vals.__class__.__name__
failure = TypeError("cannot iterate over {0}".format(valsType) )
elif 'for *' in excReason:
valType = val .__class__.__name__
prevType = prev.__class__.__name__
failure = TypeError("cannot multiply {0} by {1}".format(valType, prevType) )
else:
failure = TypeError("unknown type error")
if failure:
raise failure
def doubleSucceeding(vals):
failure = None
try:
next = vals[1]
for i, val in enumerate(vals):
yield next*2
try:
next = vals[i+2]
except IndexError:
yield 0
break
except TypeError as e:
excReason = str(sys.exc_info()[1])
if 'subscriptable' in excReason or 'iterable' in excReason:
valsType = vals.__class__.__name__
failure = TypeError("cannot iterate over {0}".format(valsType) )
elif 'for *' in excReason:
valType = val .__class__.__name__
nextType = next.__class__.__name__
failure = TypeError("cannot multiply {0} by {1}".format(valType, nextType) )
else:
failure = TypeError("unknown error")
if failure:
raise failure
def testDoubles():
print(testList)
print(list(doublePreceding(testList) ) )
print(list(doubleSucceeding(testList) ) )