Permalink
Browse files

bunch more exercises

  • Loading branch information...
1 parent 1bb8538 commit a152a2ff61ba7b136df1118b7e842b948a123a81 @jholton committed Jan 18, 2013
Showing with 672 additions and 2 deletions.
  1. +3 −0 copied.txt
  2. +26 −0 ex17.rb
  3. +1 −0 ex17b.rb
  4. +25 −0 ex18.rb
  5. +21 −0 ex19.rb
  6. +37 −0 ex20.rb
  7. +38 −0 ex21.rb
  8. +39 −0 ex24.rb
  9. +45 −0 ex25.rb
  10. +99 −0 ex26.rb
  11. +62 −0 ex28.rb
  12. +34 −0 ex29.rb
  13. +26 −0 ex30.rb
  14. +41 −0 ex31.rb
  15. +34 −0 ex32.rb
  16. +18 −0 ex33.rb
  17. +87 −0 ex35.rb
  18. +23 −0 ex5b.rb
  19. +1 −1 ex7.rb
  20. +11 −0 notes.txt
  21. +1 −1 test.txt
View
3 copied.txt
@@ -0,0 +1,3 @@
+this is the first line
+and a second
+lastly, a third
View
26 ex17.rb
@@ -0,0 +1,26 @@
+from_file, to_file = ARGV
+script = $0
+
+puts "Copying from #{from_file} to #{to_file}"
+
+# we could do these two on one line too, how?
+input = File.open(from_file)
+indata = input.read
+#indata = File.open(from_file).read()
+
+puts "The input file is #{indata.length} bytes long"
+
+puts "Does the output file exist? #{File.exists? to_file}"
+puts "Ready, hit RETURN to continue, CTRL-C to abort."
+STDIN.gets
+
+output = File.open(to_file, 'w')
+output.write(indata)
+
+puts "Alright, all done."
+
+#output.close()
+input.close()
+#indata.close()
+
+
View
1 ex17b.rb
@@ -0,0 +1 @@
+File.open(ARGV.last, 'w').write(File.open(ARGV.first).read)
View
25 ex18.rb
@@ -0,0 +1,25 @@
+# this one is like your scripts with argv
+def puts_two(*args)
+ arg1, arg2 = args
+ puts "arg1: #{arg1}, arg2: #{arg2}"
+end
+
+# ok, that *args is actually pointless we can just do this
+def puts_two_again(arg1, arg2)
+ puts "arg1: #{arg1}, arg2: #{arg2}"
+end
+
+# this just takes one argument
+def puts_one(arg1)
+ puts "arg1: #{arg1}"
+end
+
+# this one takes no arguments
+def puts_none()
+ puts "I got nothin'."
+end
+
+puts_two("Johnny", "Holton")
+puts_two_again("Johnny", "Holton")
+puts_one("First")
+puts_none()
View
21 ex19.rb
@@ -0,0 +1,21 @@
+def cheese_and_crackers(cheese_count, boxes_of_crackers)
+ puts "You have #{cheese_count} cheeses!"
+ puts "You have #{boxes_of_crackers} boxes of crackers!"
+ puts "Man that's enough for a party!"
+ puts "Get a blanket."
+ puts # a blank line
+end
+
+puts "We can just give the function numbers directly:"
+cheese_and_crackers(20, 30)
+
+puts "OR, we can use variables from our script:"
+amount_of_cheese = 10
+amount_of_crackers = 50
+cheese_and_crackers(amount_of_cheese, amount_of_crackers)
+
+puts "We can even do math inside too:"
+cheese_and_crackers(10 + 20, 5 + 6)
+
+puts "And we can combine the two, variables and math:"
+cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
View
37 ex20.rb
@@ -0,0 +1,37 @@
+input_file = ARGV[0]
+
+def print_all(f)
+ puts f.read()
+end
+
+def rewind(f)
+ f.seek(0, IO::SEEK_SET)
+end
+
+def print_a_line(line_count, f)
+ puts "#{line_count} #{f.readline()}"
+end
+
+current_file = File.open(input_file)
+
+puts "First let's print the whole file:"
+puts # a blank line
+
+print_all(current_file)
+
+puts "Now let's rewind, kind of like a tape."
+
+rewind(current_file)
+
+puts "Let's print three lines:"
+
+current_line = 1
+print_a_line(current_line, current_file)
+
+current_line = current_line + 1
+print_a_line(current_line, current_file)
+
+current_line = current_line + 1
+print_a_line(current_line, current_file)
+
+
View
38 ex21.rb
@@ -0,0 +1,38 @@
+def add(a, b)
+ puts "ADDING #{a} + #{b}"
+ a + b
+end
+
+def subtract(a, b)
+ puts "SUBTRACTING #{a} - #{b}"
+ a - b
+end
+
+def multiply(a, b)
+ puts "MULTIPLYING #{a} * #{b}"
+ a * b
+end
+
+def divide(a, b)
+ puts "DIVIDING #{a} / #{b}"
+ a / b
+end
+
+puts "Let's do some math with functions!"
+
+age = add(30, 5)
+height = subtract(78, 4)
+weight = multiply(90, 2)
+iq = divide(100, 2)
+
+puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}"
+
+# A puzzle for extra credit, type it in anyway.
+puts "Here is a puzzle."
+
+what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
+#35 + (74 - (180*(50/2)))
+
+puts "That becomes: #{what} Can you do it by hand?"
+
+
View
39 ex24.rb
@@ -0,0 +1,39 @@
+puts "Let's practice everything."
+puts "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs."
+
+poem = <<MULTI_LINE_STRING
+
+\tThe lovely world
+with logic so firmly planted
+cannot discern \n the needs of love
+nor comprehend passion from intuition
+and requires an explanation
+\n\t\twhere there is none.
+
+MULTI_LINE_STRING
+
+puts "--------------"
+puts poem
+puts "--------------"
+
+five = 10 - 2 + 3 - 6
+puts "This should be five: #{five}"
+
+def secret_formula(started)
+ jelly_beans = started * 500
+ jars = jelly_beans / 1000
+ crates = jars / 100
+ return jelly_beans, jars, crates
+end
+
+start_point = 10000
+beans, jars, crates = secret_formula(start_point)
+
+puts "With a starting point of: #{start_point}"
+puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
+
+start_point = start_point / 10
+
+puts "We can also do that this way:"
+puts "We'd have %s beans, %s jars, and %s crates." % secret_formula(start_point)
+
View
45 ex25.rb
@@ -0,0 +1,45 @@
+module Ex25
+ def self.break_words(stuff)
+ # This function will break up words for us.
+ words = stuff.split(' ')
+ words
+ end
+
+ def self.sort_words(words)
+ # Sorts the words.
+ words.sort()
+ end
+
+ def self.print_first_word(words)
+ # Prints the first word and shifts the others down by one.
+ word = words.shift()
+ puts word
+ end
+
+ def self.print_last_word(words)
+ # Prints the last word after popping it off the end.
+ word = words.pop()
+ puts word
+ end
+
+ def self.sort_sentence(sentence)
+ # Takes in a full sentence and returns the sorted words.
+ words = break_words(sentence)
+ sort_words(words)
+ end
+
+ def self.print_first_and_last(sentence)
+ # Prints the first and last words of the sentence.
+ words = break_words(sentence)
+ print_first_word(words)
+ print_last_word(words)
+ end
+
+ def self.print_first_and_last_sorted(sentence)
+ # Sorts the words then prints the first and last one.
+ words = sort_sentence(sentence)
+ print_first_word(words)
+ print_last_word(words)
+ end
+end
+
View
99 ex26.rb
@@ -0,0 +1,99 @@
+# This function will break up words for us.
+def break_words(stuff)
+ words = stuff.split(' ')
+ return words
+end
+
+# Sorts the words.
+def sort_words(words)
+ return words.sort
+end
+
+# Prints the first word after popping it off.
+def puts_first_word(words)
+ word = words.shift
+ puts word
+end
+
+# Prints the last word after popping it off.
+def puts_last_word(words)
+ word = words.pop
+ puts word
+end
+
+# Takes in a full sentence and returns the sorted words.
+def sort_sentence(sentence)
+ words = break_words(sentence)
+ return sort_words(words)
+end
+
+# Puts the first and last words of the sentence.
+def puts_first_and_last(sentence)
+ words = break_words(sentence)
+ puts_first_word(words)
+ puts_last_word(words)
+end
+
+# Sorts the words then prints the first and last one.
+def puts_first_and_last_sorted(sentence)
+ words = sort_sentence(sentence)
+ puts_first_word(words)
+ puts_last_word(words)
+end
+
+
+puts "Let's practice everything."
+puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'
+
+poem = <<POEM
+\tThe lovely world
+with logic so firmly planted
+cannot discern \n the needs of love
+nor comprehend passion from intuition
+and requires an explantion
+\n\t\twhere there is none.
+POEM
+
+
+puts "--------------"
+puts poem
+puts "--------------"
+
+five = 10 - 2 + 3 - 6
+puts "This should be five: %s" % five
+
+def secret_formula(started)
+ jelly_beans = started * 500
+ jars = jelly_beans / 1000
+ crates = jars / 100
+ return jelly_beans, jars, crates
+end
+
+start_point = 10000
+beans, jars, crates = secret_formula(start_point)
+
+puts "With a starting point of: %d" % start_point
+puts "We'd have %d jeans, %d jars, and %d crates." % [beans, jars, crates]
+
+start_point = start_point / 10
+
+puts "We can also do that this way:"
+puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)
+
+
+sentence = "All good\tthings come to those who weight."
+
+words = break_words(sentence)
+sorted_words = sort_words(words)
+
+puts_first_word(words)
+puts_last_word(words)
+puts_first_word(sorted_words)
+puts_last_word(sorted_words)
+sorted_words = sort_sentence(sentence)
+print sorted_words
+
+puts_first_and_last(sentence)
+
+puts_first_and_last_sorted(sentence)
+
View
62 ex28.rb
@@ -0,0 +1,62 @@
+something = "I dunno"
+
+puts true and true
+puts "I thought it would be #{something}"
+
+puts false and true
+puts "I thought it would be #{something}"
+
+puts 1 == 1 and 2 == 1
+puts "I thought it would be #{something}"
+
+puts "test" == "test"
+puts "I thought it would be #{something}"
+
+puts 1 == 1 or 2 != 1
+puts "I thought it would be #{something}"
+
+puts true and 1 == 1
+puts "I thought it would be #{something}"
+
+puts false and 0 != 0
+puts "I thought it would be #{something}"
+
+puts true or 1 == 1
+puts "I thought it would be #{something}"
+
+puts "test" == "testing"
+puts "I thought it would be #{something}"
+
+puts 1 != 0 and 2 == 1
+puts "I thought it would be #{something}"
+
+puts "test" != "testing"
+puts "I thought it would be #{something}"
+
+puts "test" == 1
+puts "I thought it would be #{something}"
+
+puts (not (true and false))
+puts "I thought it would be #{something}"
+
+puts (not (1 == 1 and 0 != 1))
+puts "I thought it would be #{something}"
+
+puts (not (10 == 1 or 1000 == 1000))
+puts "I thought it would be #{something}"
+
+puts (not (1 != 10 or 3 == 4))
+puts "I thought it would be #{something}"
+
+puts (not ("testing" == "testing" and "Zed" == "Cool Guy"))
+puts "I thought it would be #{something}"
+
+puts (1 == 1 and not ("testing" == 1 or 1 == 0))
+puts "I thought it would be #{something}"
+
+puts ("chunky" == "bacon" and not (3 == 4 or 3 == 3))
+puts "I thought it would be #{something}"
+
+puts (3 == 3 and not ("testing" == "testing" or "Ruby" == "Fun"))
+puts "I thought it would be #{something}"
+
View
34 ex29.rb
@@ -0,0 +1,34 @@
+people = 20
+cats = 30
+dogs = 15
+
+if people < cats
+ puts "Too many cats! The world is doomed!"
+end
+
+if people > cats
+ puts "Not many cats! The world is saved!"
+end
+
+if people < dogs
+ puts "The world is drooled on!"
+end
+
+if people > dogs
+ puts "The world is dry!"
+end
+
+dogs += 5
+
+if people >= dogs
+ puts "People are greater than or equal to dogs."
+end
+
+if people <= dogs
+ puts "People are less than or equal to dogs."
+end
+
+if people == dogs
+ puts "People are dogs."
+end
+
View
26 ex30.rb
@@ -0,0 +1,26 @@
+people = 30
+cars = 40
+buses = 15
+
+if cars > people
+ puts "We should take the cars."
+elsif cars < people
+ puts "We should not take the cars."
+else
+ puts "We can't decide."
+end
+
+if buses > cars
+ puts "That's too many buses."
+elsif buses < cars
+ puts "Maybe we could take the buses."
+else
+ puts "We still can't decide."
+end
+
+if people > buses
+ puts "Alright, let's just take the buses."
+else
+ puts "Fine, let's stay home then."
+end
+
View
41 ex31.rb
@@ -0,0 +1,41 @@
+def prompt
+ print "> "
+end
+
+puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
+
+prompt; door = gets.chomp
+
+if door == "1"
+ puts "There's a giant bear here eating a cheese cake. What do you do?"
+ puts "1. Take the cake."
+ puts "2. Scream at the bear."
+
+ prompt; bear = gets.chomp
+
+ if bear == "1"
+ puts "The bear eats your face off. Good job!"
+ elsif bear == "2"
+ puts "The bear eats your legs off. Good job!"
+ else
+ puts "Well, doing #{bear} is probably better. Bear runs away."
+ end
+
+elsif door == "2"
+ puts "You stare into the endless abyss at Cthuhlu's retina."
+ puts "1. Blueberries."
+ puts "2. Yellow jacket clothespins."
+ puts "3. Understanding revolvers yelling melodies."
+
+ prompt; insanity = gets.chomp
+
+ if insanity == "1" or insanity == "2"
+ puts "Your body survives powered by a mind of jello. Good job!"
+ else
+ puts "The insanity rots your eyes into a pool of muck. Good job!"
+ end
+
+else
+ puts "You stumble around and fall on a knife and die. Good job!"
+end
+
View
34 ex32.rb
@@ -0,0 +1,34 @@
+the_count = [1, 2, 3, 4, 5]
+fruits = ['apples', 'oranges', 'pears', 'apricots']
+change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
+
+# this first kind of for-loop goes through an array
+for number in the_count
+ puts "This is count #{number}"
+end
+
+# same as above, but using a block instead
+fruits.each do |fruit|
+ puts "A fruit of type: #{fruit}"
+end
+
+# also we can go through mixed arrays too
+for i in change
+ puts "I got #{i}"
+end
+
+# we can also build arrays, first start with an empty one
+elements = []
+
+# then use a range object to do 0 to 5 counts
+for i in (0..5)
+ puts "Adding #{i} to the list."
+ # push is a function that arrays understand
+ elements.push(i)
+end
+
+# now we can puts them out too
+for i in elements
+ puts "Element was: #{i}"
+end
+
View
18 ex33.rb
@@ -0,0 +1,18 @@
+i = 0
+numbers = []
+
+while i < 6
+ puts "At the top i is #{i}"
+ numbers.push(i)
+
+ i = i + 1
+ puts "Numbers now: #{numbers}"
+ puts "At the bottom i is #{i}"
+end
+
+puts "The numbers: "
+
+for num in numbers
+ puts num
+end
+
View
87 ex35.rb
@@ -0,0 +1,87 @@
+def prompt()
+ print "> "
+end
+
+def gold_room()
+ puts "This room is full of gold. How much do you take?"
+
+ prompt; next_move = gets.chomp
+ if next_move.include? "0" or next_move.include? "1"
+ how_much = next_move.to_i()
+ else
+ dead("Man, learn to type a number.")
+ end
+
+ if how_much < 50
+ puts "Nice, you're not greedy, you win!"
+ Process.exit(0)
+ else
+ dead("You greedy bastard!")
+ end
+end
+
+
+def bear_room()
+ puts "There is a bear here."
+ puts "The bear has a bunch of honey."
+ puts "The fat bear is in front of another door."
+ puts "How are you going to move the bear?"
+ bear_moved = false
+
+ while true
+ prompt; next_move = gets.chomp
+
+ if next_move == "take honey"
+ dead("The bear looks at you then slaps your face off.")
+ elsif next_move == "taunt bear" and not bear_moved
+ puts "The bear has moved from the door. You can go through it now."
+ bear_moved = true
+ elsif next_move == "taunt bear" and bear_moved
+ dead("The bear gets pissed off and chews your leg off.")
+ elsif next_move == "open door" and bear_moved
+ gold_room()
+ else
+ puts "I got no idea what that means."
+ end
+ end
+end
+
+def cthulhu_room()
+ puts "Here you see the great evil Cthulhu."
+ puts "He, it, whatever stares at you and you go insane."
+ puts "Do you flee for your life or eat your head?"
+
+ prompt; next_move = gets.chomp
+
+ if next_move.include? "flee"
+ start()
+ elsif next_move.include? "head"
+ dead("Well that was tasty!")
+ else
+ cthulhu_room()
+ end
+end
+
+def dead(why)
+ puts "#{why} Good job!"
+ Process.exit(0)
+end
+
+def start()
+ puts "You are in a dark room."
+ puts "There is a door to your right and left."
+ puts "Which one do you take?"
+
+ prompt; next_move = gets.chomp
+
+ if next_move == "left"
+ bear_room()
+ elsif next_move == "right"
+ cthulhu_room()
+ else
+ dead("You stumble around the room until you starve.")
+ end
+end
+
+start()
+
View
23 ex5b.rb
@@ -0,0 +1,23 @@
+my_name = 'Zed A. Shaw'
+my_age = 35 #not a lie
+my_height = 74 #inches
+#Decimal was added here
+my_weight = 180.0 #lbs
+#This was added as extra
+weight_in_england = my_weight * 0.46
+my_eyes = 'Blue'
+my_teeth = 'White'
+my_hair = 'Brown'
+
+puts "Let's talk about %s." % my_name
+puts "He's %d inches tall." % my_height
+puts "He's %d pounds heavy." % my_weight
+puts "If he visited England they would say he was %d kilos heavy." % weight_in_england
+puts "Actually that's not too heavy."
+puts "He's got %s eyes and %s hair." % [my_eyes, my_hair]
+puts "His teeth are usually %s depending on the coffee." % my_teeth
+
+#this line is tricky, try to get it exactly right
+puts "If I add %d, %d, and %d I get %d." % [
+ my_age, my_height, my_weight, my_age + my_height + my_weight]
+
View
2 ex7.rb
@@ -22,5 +22,5 @@
print end7 + end8 + end9 + end10 + end11 + end12
# this just is polite use of the terminal, try removing it
-puts
+#puts
View
11 notes.txt
@@ -0,0 +1,11 @@
+Exercise 21:
+
+Write a formula that subtracts 9 from 22. Multiply the result by 4.
+Divide the result by 2. Add 99 to that.
+
+Exercise 22:
+Block text
+Write a method that adds two numbers.
+Give me an example of a local variable, instance variable, and constant.
+
+
View
2 test.txt
@@ -1,3 +1,3 @@
this is the first line
and a second
-lastly, athird
+lastly, a third

0 comments on commit a152a2f

Please sign in to comment.