Uploaded a really simple RPS class on github today. I want to figure out how to train the computer to figure out what you're going to pick next. So I'll add an initialize that does the random, and then after that it'll do something like either pick the last one you picked, or anticipate. Should be fun to figure out.
Gordon recommends I avoid looking at js tutorials and just hacking through it. Programming is programming, after all.
Saturday, June 18, 2011
Blocks and Procs, or just more neat objects
Finally finished Pine's book! It took me a few extra days, but I thought it was appropriate to at least get through it before I move on to Ruby Quiz over the weekend.
So the last real lesson in the book was on blocks and procedures. Procedures are a lot like methods in that they kind of do the same thing. For example:
This method just writes the above statement (which is true!) as many times as it's called. That is, compared to a proc:
This proc does the exact same thing in a different way! This must be what Pine means by "Tim Toady."
The main difference is that for the proc, we call the code in the proc (between do and end) a block. Blocks and procs... clever right? How's that for assonance?
Procs are useful for two main reasons:
1. You can pass procs into methods (you can't pass methods into methods)
2. Methods can return procs (methods can't return methods)
Apparently you can even pass blocks into methods, but that's all sorts of ridiculous. Is it useful? Pine wrote us a proc that determines how long it takes to do a program. So, I'm going to use that to see how long it took my bubble sorter (and shuffler) to sort or shuffle 100 items in an array.
My Bubble sort 100 words: 0.012246 seconds
My Shuffle 100 words: 0.000309 seconds
That's compared to ruby's built in which is...
Ruby Sort 100 words: 1.7e-05 seconds
Ruby Shuffle 100 words: 4.0e-06 seconds
Then I upped it to 1000 words to see if it made a difference.
My Bubble sort 1000 words: 0.987385 seconds
My Shuffle 1000 words: 0.001996 seconds
Ruby Sort 1000 words: 0.000118 seconds
Ruby Shuffle 1000 words: 4.3e-05 seconds
Good to know that Ruby knows what it is doing better than I do. Goodnight, everybody.
So the last real lesson in the book was on blocks and procedures. Procedures are a lot like methods in that they kind of do the same thing. For example:
def ice_cream excited
'I looooove ice cream! ' * excited
end
puts ice_cream(3)
'I looooove ice cream! ' * excited
end
puts ice_cream(3)
This method just writes the above statement (which is true!) as many times as it's called. That is, compared to a proc:
ice_cream = Proc.new do |i|
puts 'I loooooove ice cream! ' * i
end
ice_cream.call(2)
puts 'I loooooove ice cream! ' * i
end
ice_cream.call(2)
This proc does the exact same thing in a different way! This must be what Pine means by "Tim Toady."
The main difference is that for the proc, we call the code in the proc (between do and end) a block. Blocks and procs... clever right? How's that for assonance?
Procs are useful for two main reasons:
1. You can pass procs into methods (you can't pass methods into methods)
2. Methods can return procs (methods can't return methods)
Apparently you can even pass blocks into methods, but that's all sorts of ridiculous. Is it useful? Pine wrote us a proc that determines how long it takes to do a program. So, I'm going to use that to see how long it took my bubble sorter (and shuffler) to sort or shuffle 100 items in an array.
My Bubble sort 100 words: 0.012246 seconds
My Shuffle 100 words: 0.000309 seconds
That's compared to ruby's built in which is...
Ruby Sort 100 words: 1.7e-05 seconds
Ruby Shuffle 100 words: 4.0e-06 seconds
Then I upped it to 1000 words to see if it made a difference.
My Bubble sort 1000 words: 0.987385 seconds
My Shuffle 1000 words: 0.001996 seconds
Ruby Sort 1000 words: 0.000118 seconds
Ruby Shuffle 1000 words: 4.3e-05 seconds
Good to know that Ruby knows what it is doing better than I do. Goodnight, everybody.
Friday, June 17, 2011
Changing existing classes
Getting through that sort method felt pretty good, and I feel much more confident about my writing now than I did a few days ago. Today I wrote up the bubble_shuffle method, but to use some new knowledge as well from reading through the rest of Chris Pine's book, I just modified the Array class to include my shuffle method, despite there already being a shuffle method defined in the class.
Classes make up the big picture. Even though Ruby counts EVERYTHING as an object (and boy do they mean everything!), classes make up those objects. objects have different classes. For example, Integers are a class, Arrays are a class, Hashes (which are like arrays) are a class. Each class has a built in number of methods, and to my knowledge is ever increasing as Ruby gets updated, but you are allowed to edit classes yourself to write your own methods. Pine gives the example of to_eng for the Integer class, which translates integers into written form, ie. 3.to_eng returns 'three'.
Originally he wanted us to just write a shuffle method in the recursion chapter, but I just went ahead and skipped to the newer exercise, which modifies the Array class to include my shuffler. Now I can put in an array and use the method to have it shuffled and returned.
I saw on rubyquiz.com that there's a solitaire cipher quiz. I think I'll work on that one tonight. I definitely feel confident enough to figure that one out on my own, or at least understand the solutions. In the meantime, I have another job application to submit tonight. Until much later tonight, rubyers!
Classes make up the big picture. Even though Ruby counts EVERYTHING as an object (and boy do they mean everything!), classes make up those objects. objects have different classes. For example, Integers are a class, Arrays are a class, Hashes (which are like arrays) are a class. Each class has a built in number of methods, and to my knowledge is ever increasing as Ruby gets updated, but you are allowed to edit classes yourself to write your own methods. Pine gives the example of to_eng for the Integer class, which translates integers into written form, ie. 3.to_eng returns 'three'.
Originally he wanted us to just write a shuffle method in the recursion chapter, but I just went ahead and skipped to the newer exercise, which modifies the Array class to include my shuffler. Now I can put in an array and use the method to have it shuffled and returned.
I saw on rubyquiz.com that there's a solitaire cipher quiz. I think I'll work on that one tonight. I definitely feel confident enough to figure that one out on my own, or at least understand the solutions. In the meantime, I have another job application to submit tonight. Until much later tonight, rubyers!
No coding today
After my success story this morning, I spent a lot of time revising my résumé while I begin my job search. I haven't had an interview in two years, and although I know I will likely have hundreds of interviews by the time I retire someday, it's really strange to jump back on the application train. This week has flown by fast enough as it is!
Here's hoping that the summer projects I'm working on will be great additions to the paper trail.
Here's hoping that the summer projects I'm working on will be great additions to the paper trail.
Thursday, June 16, 2011
Finally figured out a sorting algorithm!
Eventually I came to the conclusion that my algorithm was not going to work, so I had to start from scratch. I ended up wiki-ing sorting methods, and tried implementing the first one: a bubble sort. Finally, I just ended up borrowing code from dreamincode.net. Here's my final product for a bubble sorter:
Now the better thing for me to do here is explain what's happening. After you provide your list, it goes through the array two at a time next to each other (hence the i and the j) to see if they are backwards (-1). If they are, it fixes them. It keeps going through the list until it doesn't return a "backwards," the returns the new sorted list. I added a clear method after the return so you can just start a new list again.
In order to get more comfortable with all this, I'd like to check out a few more sort methods and use my cards that I made to go through each one manually so I can understand them better. Apparently bubble sort is incredibly slow and not good for larger patterns.
Last night I almost finished the shuffle algorithm, but I think I'm also going to see if I can find some on the internet already, and then play around with those. I'd like to think you could just use bubble sort with rand and just repeat that 100 times or so.
Back to house kinds of things. I need to start packing for my cross country trip, and this place is a mess.
Now the better thing for me to do here is explain what's happening. After you provide your list, it goes through the array two at a time next to each other (hence the i and the j) to see if they are backwards (-1). If they are, it fixes them. It keeps going through the list until it doesn't return a "backwards," the returns the new sorted list. I added a clear method after the return so you can just start a new list again.
In order to get more comfortable with all this, I'd like to check out a few more sort methods and use my cards that I made to go through each one manually so I can understand them better. Apparently bubble sort is incredibly slow and not good for larger patterns.
Last night I almost finished the shuffle algorithm, but I think I'm also going to see if I can find some on the internet already, and then play around with those. I'd like to think you could just use bubble sort with rand and just repeat that 100 times or so.
Back to house kinds of things. I need to start packing for my cross country trip, and this place is a mess.
More recursion
It felt really good figuring out how to get rid of a stack over flow, but I still haven't figured out how to write the sort program properly. I've taken to making flashcards on the ground. I know exactly what needs to happen--heck, I've written it down a million different ways on my notepad--but I keep forgetting something.
Tried playing around with the min method to find the smallest string, push it to the new array, and then delete it, but it's not working the way I want it to. I think what's happening is that it's just deleting the same slot through each run, because all strings come out as 0 for integers, or something along those lines. Either way, it's not deleting the right slot, so I either need to figure out how to make it delete the right slot, or just find the next smallest string in the same array while leaving the original there. min-[i] perhaps?
Late night again. See everyone tomorrow.
Tried playing around with the min method to find the smallest string, push it to the new array, and then delete it, but it's not working the way I want it to. I think what's happening is that it's just deleting the same slot through each run, because all strings come out as 0 for integers, or something along those lines. Either way, it's not deleting the right slot, so I either need to figure out how to make it delete the right slot, or just find the next smallest string in the same array while leaving the original there. min-[i] perhaps?
Late night again. See everyone tomorrow.
Wednesday, June 15, 2011
Chapter 10: Recursion and other nonsense
Up late tonight! This is what I get for sleeping in this morning.
I fixed Pine's psych problem using returns, but now he explains recursion. I asked Jameson if methods can call upon other methods today, to which his response was "duh," and then other utterances. Perfect timing while Pine explains methods calling upon themselves. Very useful if I wanted to rewrite my number guess game.
He uses recursion to find the largest continent in a map drawn by grid. Looking over the code I couldn't find the recursion the first time. But wait a second...
def continent_size world, x, y
if world[y][x] != ' land'
# Either it' s water or we already
# counted it, but either way, we don' t
# want to count it now.
return 0
end
# So first we count this tile...
size = 1 world[y][x] = ' counted land'
# ...then we count all of the
# neighboring eight tiles (and,
# of course, their neighbors by
# way of the recursion).
size = size + continent_size(world, x-1, y-1)
There it is!
I got to write all these chapter 10 programs up tonight. I'm falling behind and really don't want blogger to be an excuse.
I fixed Pine's psych problem using returns, but now he explains recursion. I asked Jameson if methods can call upon other methods today, to which his response was "duh," and then other utterances. Perfect timing while Pine explains methods calling upon themselves. Very useful if I wanted to rewrite my number guess game.
He uses recursion to find the largest continent in a map drawn by grid. Looking over the code I couldn't find the recursion the first time. But wait a second...
def continent_size world, x, y
if world[y][x] != ' land'
# Either it' s water or we already
# counted it, but either way, we don' t
# want to count it now.
return 0
end
# So first we count this tile...
size = 1 world[y][x] = ' counted land'
# ...then we count all of the
# neighboring eight tiles (and,
# of course, their neighbors by
# way of the recursion).
size = size + continent_size(world, x-1, y-1)
There it is!
I got to write all these chapter 10 programs up tonight. I'm falling behind and really don't want blogger to be an excuse.
Subscribe to:
Posts (Atom)