Saturday, June 18, 2011

Rock, Paper, Scissors, Shoot.

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.

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:
def ice_cream excited
'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)

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!

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.

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.

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.

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.

Tuesday, June 14, 2011

Writing Methods


Wrapping up chapter 9 right now, finishing the second problem, which was rewriting the previously assignment: an arabic to roman numeral translator. I think I need to work on it a bit more, especially after this surprise:
Quietly raging now. If the picture isn't big enough, ruby is using up almost a gig of memory, and kicked up my VM to 20TB. After forcing it shut, the VM went back to a respectable 140GB, which I'm okay with. Going to make dinner (fresh gnocchi!) and then hopefully fix this crisis and finish up chapter 10 on time.

Monday, June 13, 2011

arrays and ranges

Thomson showed me today I can use a range for the 'do' keyword. so I can do something like...


(0..2).each do |i|


and this lets me create a set loop. I used this so I could use the each command when I wrote up arrays for the table of contents. So my whole program ended up like this:


chapter = ['Chapter 1: Getting Started', 'Chapter 2: Numbers', 'Chapter 3: Letters']
page = ['page 1', 'page 9', 'page 13']
line_width = 70
puts ('Table of Contents'.center(line_width))
chapter = ['Chapter 1: Getting Started', 'Chapter 2: Numbers', 'Chapter 3: Letters', 'Chapter 4: Variables and Assignent', 'Chapter 5: Mixing It Up', 'Chapter 6: More about Methods', 'Chapter 7: Flow Control', 'Chapter 8: Arrays and Iterators', 'Chapter 9: Writing Your Own Methods']
page = ['page 1', 'page 9', 'page 13', 'page 18', 'page 22', 'page 29', 'page 41', 'page 57', 'page 64']
(0..8).each do |i|
puts chapter[i].ljust(line_width/2) + page[i].rjust(line_width/2)
end


I also pushed a number game to github that I wrote as my final project tonight. It (I think) uses everything I've done from yesterday and today. It combines a few different ideas: using sort to organize guesses in an array, joining slots of an array with commas, branching to cover all possible responses with something appropriate. Not yet quite real-life Ruby scenario, but fun.

See you tomorrow!

Get the Git

I joined github yesterday. Should be more useful later on, but for now I'm just using it to watch other coders and challenge myself to figure out what they're doing on paper.

Should have Pine's book down by end of the week. Once ready, goal is to move on to:

1) projecteuler.net to move on to solving problems.
2) learn next languages: ruby on rails, perhaps python or C as well. This will be next week.

One more post tonight after finishing Chapter 8 tonight. I read it last night after having major headaches from staring at the computer all day, but now I want to go back and figure out all of chapter 7's and 8's problems. I'll post something original instead of the solutions. I got stuck on chapter 8 last year, but that was after a couple weeks. Let's see what happens tonight!

Flow Control

if works when it meets the requirement listed.
else works when the requirement doesn't fit the if
elsif is like the next if. I think it can go on infinitely.
Now for an example using numbers instead of strings. This program asks for your coffee size and how much sugar. Too much sugar will get you a beating from the barista.


puts 'What size coffee would you like? S/M/L'
coffee_size = gets.chomp
if coffee_size.capitalize == 'S' || coffee_size.capitalize == 'M' || coffee_size.capitalize == 'L'
puts 'And how many teaspoons of sugar for that coffee?'
sugar_tsp = gets.chomp
if coffee_size.capitalize == 'S'
if sugar_tsp.to_i > 1
puts 'Sir! That is way too much sugar for a small coffee!'
else
puts 'You got it! Coming right up!'
end
elsif coffee_size.capitalize == 'M'
if sugar_tsp.to_i > 2
puts 'Sir! That is way too much sugar for a medium coffee!'
else
puts 'You got it! Coming right up!'
end
elsif coffee_size.capitalize == 'L'
if sugar_tsp.to_i > 5
puts 'Sir! That is way too much sugar for large coffee!'
else
puts 'You got it! Coming right up!'
end
end
else
puts 'I\'m sorry, but we don\'t offer that size.'
end

Sunday, June 12, 2011

Day 1 Complete

This is going to be a fun yet awkward relationship.

# First problem: Program that asks each part of someone's name individually,
# then greets with whole name.
puts 'Hi, what is your first name?'
fname = gets.chomp
puts 'And your middle name?'
mname = gets.chomp
puts 'And your last name?'
lname = gets.chomp
puts 'Well, nice to meet you, ' + fname + ' ' + mname + ' ' + lname + '.'
puts 'My name is Ruby. I\'m very cute, don\'t you think? :*'
#Second problem: Ruby thinks you two are meant for each other.
puts 'So, ' + fname + ', what is your favorite number?'
fnum = gets.chomp
puts 'You like the number ' + fnum + '?'
rnum = fnum.to_i + 1
puts 'Funny, because ' + rnum.to_s + ' is my lucky number.'
puts 'We must be made for each other!'
puts 'RUBY. NOT EVEN ONCE.'

Puts and Numbers

Read chapter 2 today as review after glancing through intro and chapter 1. I installed xcode3.6 and macports 1.8 yesterday to help with compiling.

Puts does exactly what you'd think it does: puts whatever comes after it. You can put text or numbers after the puts command, though as of now I remember that text needs to be in parens in Ruby.

Numbers work two days: float (with decimals) and integers (no decimals... like in math class!). Integers work best for now because they are whole numbers. You can do whatever you want with numbers it seems: add, subtract, multiply, divide, for starters.

Not the most original code in the world, but I divided each of the exercises at the end of chapter 2 with puts commands that explain what each number represents. I don't remember yet how to set numbers to strings, so that'll come later.


puts "hours in a year"
puts 24*365
puts "minutes in a decade"
puts 60*24*365*10
puts "my age in seconds"
puts (60*60*365*25)
puts "The answer to 2.4 tough question"
puts 912000000/60/60/24/365

Nothing really exciting yet. I'll get through another chapter today and try something more original later.

Introduction

With the never-ending pink slips in education, I recently lost my job after two years having my own classroom in California. While filling in unemployment forms and other nonsense, I'm spending my summer to get back to my roots of programming from my high school days of QBASIC.

The material I'm learning from this year--which I started last year and never finished--is from Learn to Program by Chris Pine. My goal is to get through the book effectively, using other sources as well, to become a halfway decent amateur Ruby programmer by the end of my summer: September. Every day I will post some kind of original code and some review for myself so I can remember what I learned each day. This post will probably be my most elaborate; expect shorthand from here on out. Thanks for following!