diff 2-Io/day2-self-study.io @ 39:c48e6e454991

Add first four self-study answers
author IBBoard <dev@ibboard.co.uk>
date Wed, 13 Sep 2017 20:55:07 +0100
parents
children 82627dd71c75
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/2-Io/day2-self-study.io	Wed Sep 13 20:55:07 2017 +0100
@@ -0,0 +1,92 @@
+# 1) Fibanacci by recursion (fibr) and loop (fibl)
+fibr := method(num,
+    if (num < 1,
+        return 0,
+        if (num == 1,
+            return 1,
+            prev := fibr(num - 1) ;
+                prev_prev := fibr(num - 2) ;
+                return prev + prev_prev
+        )
+    )
+)
+
+"fibr(9) = " print
+fibr(9) println
+"fibr(4) = " print
+fibr(4) println
+"fibr(1) = " print
+fibr(1) println
+
+
+fibl := method(num,
+    if (num < 1, return 0)
+    if (num == 1, return 1)
+    prev := 1
+    prev_prev := 0
+    val := 0    
+    for(i, 2, num,
+        val = prev + prev_prev
+        prev_prev = prev
+        prev = val
+    )
+    return val
+)
+
+"fibl(9) = " print
+fibl(9) println
+"fibl(4) = " print
+fibl(4) println
+"fibl(1) = " print
+fibl(1) println
+
+
+# 2) Changing "/" operator to return 0 for a zero denominator
+# Operators are just messages on objects, so "/" is the message to override
+# and it must be on the object type Number
+Number defaultDivide := Number getSlot("/")
+Number / := method(denom,
+    if (denom == 0,
+        return 0,
+        return self defaultDivide(denom))
+)
+(4 / 2) println
+(4 / 0) println
+
+
+# 3) Add up numbers in a 2D array
+arr := list(
+    list(1, 2, 3),
+    list(4, 5, 6),
+    list(7, 8, 9)
+)
+
+add2D := method(arr,
+    sum := 0
+    # A "reduce()" method would be nice, but KISS for now and foreach all the things
+    arr foreach(dimension, dimension foreach(val, sum = sum + val))
+    return sum
+)
+
+add2D(arr) println
+
+
+# 4) Add myAverage to average numbers in a list
+List myAverage := method(
+    sum := 0
+    self foreach(val, if (val proto == Number,
+                          sum = sum + val,
+                          Exception raise("Non-object found in list"))
+                )
+    return sum / self size
+)
+
+list(1, 2, 3) myAverage println
+# This will exception - run at the end!
+# list(1, 2, 3, "foo") myAverage
+# With default behaviour then this could divide by zero (unless we'd been defensive coding)
+# But we're in the same file as our custom "/", so we should get zero
+list() myAverage println
+
+
+list(1, 2, 3, "foo") myAverage
\ No newline at end of file