File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments