Advertisement
ice09

7l7w io day 2

Nov 8th, 2011
3,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IO 2.68 KB | None | 0 0
  1. #
  2. # Write a Fibonacci program...
  3. #
  4. Proto := Object clone
  5.  
  6. # ...recursive version
  7. Proto fib := method(n, if (n<=2, return 1, return (fib(n-2)+fib(n-1))))
  8.  
  9. # ...iterative version
  10.  
  11. Proto fib := method(n,
  12.   numbers := list(1,1)
  13.   if (n <= 2,
  14.     return numbers at(n-1),
  15.     for(i, 2, n, numbers := numbers append (numbers at(i-2) + numbers at(i-1)))
  16.     numbers at(n-1)
  17.   )
  18. )
  19.  
  20. Proto fib(4)
  21. ==> 3
  22.  
  23. #
  24. # Change / to return 0 if denominator 0
  25. #
  26.  
  27. # save old method
  28. Io> Number div := Number getSlot("/")
  29. ==> Number_/()
  30.  
  31. # write "delegate"
  32. Io> Number / := method(if (call evalArgAt(0) == 0, 0, self div (call evalArgAt(0))))
  33.  
  34. #
  35. # Write a programm to add up all numbers in a two-dimensional array
  36. #
  37.  
  38. Proto := Object clone
  39.  
  40. Proto liste := list(list())
  41. Proto liste at(0) append(2)
  42. Proto liste at(0) append(3)
  43. Proto liste append(list())
  44. Proto liste at(1) append(4)
  45. Proto liste at(1) append(5)
  46.  
  47. Proto sum := method(
  48.   self val := 0
  49.   for(i, 0, liste size - 1, for(j, 0, liste at(0) size - 1, self val := self val + (liste at(i) at(j))))
  50. )
  51.  
  52. #
  53. # Add a slot called myAverage to List which computes the average of all elements
  54. #
  55.  
  56. # this is even empty list-safe
  57. List myAverage := method(if (sum, sum / size, 0))
  58.  
  59. #
  60. # Prototype for a two-dimensional list. dim(x,y) allocates list of lists, get(x,y) gets element at list(y) list(x), set(x,y,value) sets it
  61. #
  62.  
  63. 2dim := List clone
  64.  
  65. 2dim dim := method(y, x, removeAll for (i, 0, x-1, append(list()) for (j, 0, y-1, at(i) append(nil))))
  66.  
  67. 2dim set := method(y, x, value, if (value, at(x) atPut(y, value)))
  68.  
  69. 2dim get := method(y, x, if (at(x), at(x) at(y)))
  70.  
  71. #
  72. # Write a transpose method
  73. #
  74.  
  75. 2dim transpose := method(transpose := self clone
  76.     transpose dim(size, at(0) size)
  77.     for (x, 0, size, for (y, 0, at(0) size, transpose set(x, y, get(y, x))))
  78. return transpose)
  79.  
  80. #
  81. # Write the matrix to a file
  82. #
  83.  
  84. 2dim write := method(file,
  85.     f := File with(file)
  86.     f remove
  87.     f openForUpdating
  88.     f write(self serialized)
  89.     f close
  90. )
  91.  
  92. 2dim read := method(file,
  93.     cnt := doFile(file)
  94.     cnt foreach (v, append(v))
  95. )
  96.  
  97. #
  98. # 10 tries to guess a number between 1 and 100 with hints...
  99. #
  100.  
  101. input := File standardInput
  102. rand := (Random value(99) + 1) floor
  103. last := 0
  104. cnt := 0
  105. while((cnt < 10) and (last != rand),
  106.     write("guess" .. rand .. "? ")
  107.     num := input readLine asNumber
  108.     if (num == rand, writeln("~ YOU GOT IT ~"), write("nope... ")
  109.         if (cnt > 0,    if ((last - rand) abs > (num - rand) abs, writeln("but warmer..."), writeln("and even cooler or equally bad...")), writeln))
  110.     last = num
  111.     cnt = cnt + 1
  112. )
  113. if ((cnt == 10) and (last != rand), writeln("Looser! The number was " .. rand), writeln ("and with only " .. cnt .. " tries."))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement