Skip to content

Commit 95dbca4

Browse files
committed
unit4 pro1 version 1 solved
1 parent 726186b commit 95dbca4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

unit4/pro1.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Write a visitor to solve each of the following problems:
3+
1. Find the smallest element of a list.
4+
2. Find the largest element of a list.
5+
3. Compute the sum of all the elements of a list.
6+
4. Compute the product of all the elements of a list.
7+
'''
8+
9+
from opus7.orderedListAsArray import OrderedListAsArray
10+
from opus7.visitor import Visitor
11+
12+
def pro1():
13+
order_list = OrderedListAsArray(10)
14+
m_visitor = MinimumVisitor()
15+
order_list.insert(42)
16+
order_list.insert(21)
17+
order_list.insert(41)
18+
order_list.insert(13)
19+
order_list.insert(94)
20+
order_list.insert(34)
21+
order_list.accept(m_visitor)
22+
m_visitor.current()
23+
24+
class MinimumVisitor(Visitor):
25+
26+
def __init__(self):
27+
self.minimum = 9999999999
28+
29+
def visit(self, obj):
30+
if obj < self.minimum:
31+
self.minimum = obj
32+
33+
def current(self):
34+
print "current minimum value is:", self.minimum
35+
36+
37+
pro1()

0 commit comments

Comments
 (0)