annotate 2-Io/day2-self-study.io @ 47:409249712590

Add final experimentation and note about DSL
author IBBoard <dev@ibboard.co.uk>
date Fri, 22 Sep 2017 19:48:13 +0100
parents f4ea1f637f22
children
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
41
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
130 #6) Bonus "transpose" method (ugly and inefficient method)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
131 TwoDList transpose := method(
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
132 new_list := TwoDList clone
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
133 x := self arr size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
134 y := self arr at(0) size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
135 new_list dim(y, x)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
136 for (i, 0, x - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
137 for (j, 0, y - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
138 new_list set(j, i, self get(i, j))
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
139 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
140 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
141 return new_list
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
142 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
143
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
144 transposed := my_arr transpose
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
145 transposed get(1, 2) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
146 transposed get(0, 1) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
147 transposed get(2, 1) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
148 transposed get(1, 0) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
149
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
150 #7) Write a matrix to a file and read it back
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
151 # Might as well use the TwoDList as a matrix
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
152 # Note: This isn't white-space safe, but nvm
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
153 TwoDList as_string := method(
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
154 x := self arr size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
155 y := self arr at(0) size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
156 #str := "#{self arr size}×#{self arr at(0) size}\n" interpolate
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
157 str := ""
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
158 for (i, 0, x - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
159 for (j, 0, y - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
160 str = "#{str}#{self get(i, j)} " interpolate
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
161 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
162 str = "#{str asMutable strip}\n" interpolate
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
163 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
164 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
165
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
166 TwoDList load := method(matrix_string,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
167 # strip to make sure that we don't get trailing new lines
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
168 rows := matrix_string strip split("\n")
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
169 row_count := rows size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
170 # Ugly way to get column count
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
171 col_count := rows at(0) split(" ") size
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
172 self dim(row_count, col_count)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
173 for (i, 0, row_count - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
174 row := rows at(i) split(" ")
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
175 for (j, 0, col_count - 1,
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
176 self set(i, j, row at(j))
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
177 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
178 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
179 )
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
180 matrix_file := "matrix.txt"
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
181 f := File with(matrix_file)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
182 f remove
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
183 f openForUpdating
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
184 f write(my_arr as_string)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
185 f flush
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
186 f close
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
187
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
188 f2 := File with(matrix_file)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
189 f2 openForReading
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
190 new_list := TwoDList clone
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
191 new_list load(f2 contents)
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
192 f2 close
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
193 new_list get(1, 2) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
194 new_list get(0, 1) println
f4ea1f637f22 Add answers for 6 and 7 (transpose and write/read matrix)
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
195
40
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
196
82627dd71c75 Implement 2D list/array
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
197 # 4) final piece
39
c48e6e454991 Add first four self-study answers
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
198 list(1, 2, 3, "foo") myAverage