annotate 2-Io/day2-self-study.io @ 40:82627dd71c75

Implement 2D list/array
author IBBoard <dev@ibboard.co.uk>
date Thu, 14 Sep 2017 20:24:25 +0100
parents c48e6e454991
children f4ea1f637f22
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 # 1) Fibanacci by recursion (fibr) and loop (fibl)
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 fibr := method(num,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 if (num < 1,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 return 0,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 if (num == 1,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 return 1,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 prev := fibr(num - 1) ;
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 prev_prev := fibr(num - 2) ;
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 return prev + prev_prev
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 "fibr(9) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 fibr(9) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 "fibr(4) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 fibr(4) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 "fibr(1) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 fibr(1) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 fibl := method(num,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 if (num < 1, return 0)
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 if (num == 1, return 1)
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 prev := 1
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 prev_prev := 0
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 val := 0
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 for(i, 2, num,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 val = prev + prev_prev
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 prev_prev = prev
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 prev = val
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 return val
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 "fibl(9) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 fibl(9) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 "fibl(4) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 fibl(4) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 "fibl(1) = " print
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 fibl(1) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 # 2) Changing "/" operator to return 0 for a zero denominator
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 # Operators are just messages on objects, so "/" is the message to override
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 # and it must be on the object type Number
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 Number defaultDivide := Number getSlot("/")
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 Number / := method(denom,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 if (denom == 0,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 return 0,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 return self defaultDivide(denom))
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 (4 / 2) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 (4 / 0) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 # 3) Add up numbers in a 2D array
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 arr := list(
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 list(1, 2, 3),
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 list(4, 5, 6),
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 list(7, 8, 9)
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 add2D := method(arr,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 sum := 0
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 # A "reduce()" method would be nice, but KISS for now and foreach all the things
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 arr foreach(dimension, dimension foreach(val, sum = sum + val))
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 return sum
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 add2D(arr) println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 # 4) Add myAverage to average numbers in a list
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 List myAverage := method(
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 sum := 0
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 self foreach(val, if (val proto == Number,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 sum = sum + val,
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 Exception raise("Non-object found in list"))
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 return sum / self size
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 )
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 list(1, 2, 3) myAverage println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 # This will exception - run at the end!
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 # list(1, 2, 3, "foo") myAverage
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 # With default behaviour then this could divide by zero (unless we'd been defensive coding)
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 # But we're in the same file as our custom "/", so we should get zero
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 list() myAverage println
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91
40
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
92 #5) 2D list
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
93 TwoDList := Object clone
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
94 # It'd be nice to do something to set defaults when first cloning
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
95 # but I can't find how to do that, so force the developer to
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
96 # always dim() after clone
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
97 TwoDList dim := method(x, y,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
98 self arr := list()
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
99 for (i, 1, x,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
100 sub_list := list()
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
101 for (j, 1, y,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
102 sub_list append(nil)
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
103 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
104 arr append(sub_list)
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
105 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
106 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
107 TwoDList set := method(x, y, value,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
108 self arr at(x) atPut(y, value)
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
109 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
110
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
111 TwoDList get := method(x, y,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
112 outer := self arr at(x)
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
113 if (outer != nil,
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
114 return outer at(y),
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
115 return nil
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
116 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
117 )
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
118
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
119 my_arr := TwoDList clone
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
120 my_arr dim(2, 3)
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
121 my_arr println
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
122 my_arr set(0, 1, "0,1")
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
123 my_arr set(1, 2, "Bottom-right")
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
124 my_arr get(1, 2) println
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
125 my_arr get(0, 1) println
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
126 my_arr get(0, 0) println
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
127 my_arr get(5, 5) println
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
128
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
129
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
130
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
131 # 4) final piece
39
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 list(1, 2, 3, "foo") myAverage