Created
October 9, 2011 21:22
-
-
Save japaz/1274206 to your computer and use it in GitHub Desktop.
7L7W Io - Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Fibonacci sequence starts with two 1s. Each subsequent number | |
// is the sum of the two numbers that came before: 1, 1, 2, 3, | |
// 5, 8, 13, 21, and so on. Write a program to find the nth Fibonacci | |
// number. fib(1) is 1, and fib(4) is 3. As a bonus, solve the problem | |
// with recursion and with loops. | |
fib := method(a, if(a==1, 1, if (a==2, 1, fib(a-1)+fib(a-2)))) | |
fib(15) | |
fib := method(a, if(a<=2, 1, a1:=1; a2:=1; for(i, 3, a, a3:=a1+a2; a1:=a2; a2:=a3); a2)) | |
fib(15) | |
// How would you change / to return 0 if the denominator is zero? | |
origDiv := Number getSlot("/") | |
Number / := method (i, if (i != 0, self origDiv(i), 0)) | |
// Write a program to add up all of the numbers in a two-dimensional array. | |
a:= list(list (1,2,3), list ( 2,3,4)) | |
List sum := method(value:=0; self foreach(v,value:=value+v)) | |
a sum := method (value:=0; self foreach(v,value:=v sum + value)) | |
a sum | |
// Add a slot called myAverage to a list that computes the average of | |
// all the numbers in a list. | |
List myAverage := method(value:=0;self foreach(i, v, value := (i*value + v)/(i+1))) | |
// What happens if there are no numbers in a list? | |
// Each element that is not a number sums zero | |
// (Bonus: Raise an Io exception if any item in the list is not | |
// a number.) | |
List myAverage := method(value:=0;self foreach(i, v, if(v proto == Number,value := (i*value + v)/(i+1), Exception raise("Not a number")))) | |
// Write a prototype for a two-dimensional list. The dim(x, y) method | |
// should allocate a list of y lists that are x elements long. set(x,y, | |
// value) should set a value, and get(x, y) should return that value. | |
List2D := Object clone | |
List2D dim := method(x, y, self x:= x; self y:= y; self array := list(); for(i,1,x,lista:= list(); for(j,1,y, lista append(nil)); array append(lista))) | |
List2D set := method(x, y, value, self array at(x) atPut(y,value)) | |
List2D get := method(x, y, value, self array at(x) at(y)) | |
a := List2D clone | |
a dim(2,2) | |
a set(0,0,1) | |
a set(0,1,2) | |
a set(1,0,3) | |
a set(1,1,4) | |
a get(1,0) | |
// Bonus: Write a transpose method so that (new_matrix get(y, x)) == | |
// matrix get(x, y) on the original list. | |
List2D transpose := method(new_matrix := List2D clone; new_matrix dim(self y, self x); for(i, 0, x-1, for(j, 0, y-1, new_matrix set(j,i, self get(i,j)))); new_matrix) | |
// Write the matrix to a file, and read a matrix from a file. | |
List2D writeTo := method(name, f := File with(name) ; f openForUpdating; for(i,0,x-1, for (j,0,y-1, f writeln ((self get(i,j)) asString))); f close) | |
List2D readFrom := method(name, f := File with(name) ; f openForReading; for(i,0,x-1, for (j,0,y-1, self set(i,j, f readLine))); f close) | |
a writeTo("a.txt") | |
a readFrom("a.txt") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from http://www.bennadel.com/blog/2066-Seven-Languages-In-Seven-Weeks-Io-Day-2.htm | |
// Write a program that gives you ten tries to guess a random number | |
// from 1-100. If you would like, give a hint of "hotter" or | |
// "colder" after the first guess. | |
// The first thing we need to do is select a random number. Random() | |
// allows us to create a floating point number between 0 and N. As | |
// such, to go from 1-100, we are going to go to 99 and add 1. | |
randomNumber := (Random value( 99 ) + 1) floor; | |
// To gather input from the user, we are going to create a file input | |
// stream that uses the Standard Input (stdio) as the input. | |
standardIO := File standardInput; | |
// Because we are going to be giving the user "hotter" and "colder" | |
// feedback, we need to keep track of the previous guess. | |
previousGuess := nil; | |
// We are only going to give the user up to 10 guesses. We can create | |
// a short-hand for a 10x loop with the repeat() method. | |
10 repeat( | |
// Prompt the user for the number. | |
"Guess number (1..100): " println; | |
// Read the value from the user and parse it as number. | |
guess := standardIO readLine asNumber; | |
// Check to see if the guess is correct. | |
if( | |
(guess == randomNumber), | |
// Break out of the loop. The user is done guessing. | |
break; | |
); | |
// Check to see if we have a previous guess yet. | |
if( | |
previousGuess, | |
// Check to see if we are getting closer or farther away | |
// from the target number. We are just going to test to see | |
// if the distance between the guess and the target number | |
// is getting smaller or larger. | |
if( | |
((randomNumber - guess) abs()) >= ((randomNumber - previousGuess) abs()), | |
"Getting colder :(" println(), | |
"Getting warmer :)" println() | |
); | |
); | |
// Store the previous guess so we can calculate our warmer / | |
// colder message on the next guess. | |
previousGuess = guess; | |
); | |
// Now that the user is done guess, we have to see if they guessed | |
// correctly (broke out of loop) or if they guessed poorly (loop | |
// ended naturally). | |
if( | |
(guess == randomNumber), | |
( | |
"Awesome! Excellent guess!" println; | |
), | |
( | |
"Sorry, better luck next time." println; | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment