From cb46f6e427ba542204e7d9727107acda90054710 Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 15 Oct 2013 17:46:04 -0700 Subject: [PATCH 1/6] homework 1 --- week1/homework/questions.txt | 15 +++++++++++++++ week1/homework/strings_and_rspec_spec.rb | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..a9e1e7b 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,28 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +1a. An object is an instance of a class. 2. What is a variable? +2a. A variable is used to keep track of an object -- it is a reference to an object. 3. What is the difference between an object and a class? +3a. An object is an instance of a class; a class is simply a combination +of a state and methods that use that state. For example, a class may be a song, +and an object would be a specific song. All songs have the same actions that +can be applied to them -- i.e. they can all be played, recorded, deleted. 4. What is a String? +4a. A string is a sequence of characters -- normally holding printable characters, but not necessarily so. They can also +contain binary data. Strings are objects of the class String. 5. What are three messages that I can send to a string object? Hint: think methods +5a. One can split a string into items by specific characters (.split), +trimming strings of repeated characters (.squeeze!), or similarly to split, using +.scan to specify the pattern of character chunks to match before dividing the +string into said chunks. 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +6a. String literals can be defined using single-quotes or double-quotes, the +difference between which are the escape sequences that are supported in each +case. \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..1d04f5e 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,15 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" - it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + it "should be able to count the characters" do + @my_string.should have(66).characters + end + it "should be able to split on the . character" do + result = @my_string.split(/\s*\.\s*/) result.should have(2).items end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + @my_string.encoding.should eq(Encoding.find("UTF-8")) end end end From 1c68cd067cb0acf546419e344d3cf017f7e2090f Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 22 Oct 2013 19:13:02 -0700 Subject: [PATCH 2/6] adding homework for week2 --- week2/homework/questions.txt | 32 ++++++++++++++++++++++++++++++++ week2/homework/simon_says.rb | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 week2/homework/questions.txt create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt new file mode 100644 index 0000000..cbdcee3 --- /dev/null +++ b/week2/homework/questions.txt @@ -0,0 +1,32 @@ +Please Read The Chapters on: +Containers, Blocks, and Iterators +Sharing Functionality: Inheritance, Modules, and Mixins + +1. What is the difference between a Hash and an Array? +An array is a collection of object references, indexed +by integers. A hash is also a collection of object references, +but its elements can be indexed with objects of any type, e.g., +symbols, strings, regular expressions, etc. + +2. When would you use an Array over a Hash and vice versa? +An array is useful for breaking objects down into simple elements of a list, whereas +a hash would be more advantageous for parsing through that list and indexing the +elements by the number of times they appear. +In the example where one would be looking to calculate the word frequency +in a given block of text, an array would be advantageous to split the strings +in the text into individual word elements in an array. The hash object would +be created to index by the words in the array. + +3. What is a module? Enumerable is a built in Ruby module, what is it? +Modules are a way of grouping together methods, classes, and constants. Modules +provide a namespace and prevent name clashes, as well as suppoprting the mixin facility. +Enumerable is a module that supports operations such as mapping, sorting, searching, +and ordering elements. + +4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No. Ruby classes have only one direct parent. This can be avoided by including +a "mixin" (a partial class difinition), thereby introducing multiple-inheritance-like +capability. + +5. What is the difference between a Module and a Class? +Modules cannot have instances. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..ccc7b54 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + def echo anything #to pass echo tests + anything + end + + def shout anything #to pass shout tests + anything.upcase + end + + def start_of_word (anything,n) #to pass array tests + anything[0...n] + end + + def first_word anything #to pass first word test + anything.split.first + end + + def repeat (anything,n=2) #to pass word repeat tests + ([anything]*n).join(' ') + end +end From 44f2d8c7eea0e94d4663d7eeff48f0351a146817 Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 29 Oct 2013 18:10:15 -0700 Subject: [PATCH 3/6] week 3 homework --- week3/homework/calculator.rb | 48 ++++++++++++++++++++++++++++++++++++ week3/homework/questions.txt | 30 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 week3/homework/calculator.rb create mode 100644 week3/homework/questions.txt diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..5e0ade3 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,48 @@ +module Summable + def sum(whatever) + if whatever != [] + whatever = whatever.flatten + whatever.inject(:+) + else + whatever = 0 + end + end +end + +module Empowerable + def pow(whatever, anything) + whatever**anything + end +end + +module Multipliable + def multiply(whatever,anything) + if anything != nil + anything*whatever + else + anything = 1 + whatever = whatever.flatten + whatever.inject(:*)*anything + end + end +end + +module Factorialize + def fac(whatever) + if whatever<=1 + 1 + else + whatever * fac(whatever-1) + end + end +end + + +class Calculator + include Enumerable + include Summable + include Empowerable + include Multipliable + include Factorialize +end + diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt new file mode 100644 index 0000000..073b7d4 --- /dev/null +++ b/week3/homework/questions.txt @@ -0,0 +1,30 @@ +Please Read: + - Chapter 6 Standard Types + - Review Blocks + - Chapter 7 Regular Expressions + - Chapter 22 The Ruby Language: basic types (symbols), variables and constants + +1. What is a symbol? +A symbol is something used to correspond to a +string of characters, often a name. + +2. What is the difference between a symbol and a string? +Symbols of the same name are initialized and exist in +memory only once during a session of Ruby. + +3. What is a block and how do I call a block? +A block is a chunk of code enclosed between either braces +or the keywords 'do' and 'end'. A block is invoked by +a Ruby iterator; it may only appear in the source adjacent +to a method call; the block is written starting on the +same line as the method's last parameter. + +4. How do I pass a block to a method? What is the method signature? +A block is passed to a method when a yield statement is +executed. + +5. Where would you use regular expressions? +Regular expressions would be used in tests to see whether +strings match a desired pattern, extract parts of code +that match the desired pattern, or find and replace +patterns in given strings with a desired set of characers. \ No newline at end of file From 1ed72b4119773830ccd2671984347dfeb22a2436 Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 5 Nov 2013 20:27:11 -0800 Subject: [PATCH 4/6] added week4 homework --- week4/homework/questions.txt | 20 ++++++++++++++++++++ week4/homework/worker.rb | 11 +++++++++++ 2 files changed, 31 insertions(+) create mode 100644 week4/homework/questions.txt create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt new file mode 100644 index 0000000..5ae55bb --- /dev/null +++ b/week4/homework/questions.txt @@ -0,0 +1,20 @@ +Please Read: +Chapter 10 Basic Input and Output +The Rake Gem: http://rake.rubyforge.org/ + +1. How does Ruby read files? +file = File.open("file_name","r") opens an existing file named "file_name", starting at the beginning of the file, and reads it (r). + +2. How would you output "Hello World!" to a file called my_output.txt? +file = File.open("my_output.txt","r+") do |text| +text << "Hello World!" +end + +3. What is the Directory class and what is it used for? +The Directory class allows you to manipulate things to do with directories (folders) -- primarily navigating within directories, making new directories, and determining what is contained within a directory. + +4. What is an IO object? +Ruby defines a single base class, IO, to handle input and output. An IO object is a bidrectional channel between a Ruby program and some external resource. + +5. What is rake and what is it used for? What is a rake task? +Rake is a Ruby Gem. It is used for task management. A rake task is a unit of work in a Rakefile, which has its associated actions and respective prerequisites. \ No newline at end of file diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..84cf766 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,11 @@ +module Worker + + def self.work n=1 #sets a default value, thus making param optional + + results = nil #initialize results to neutral format + n.times {results = yield} #to satisfy third test + results #show results + + end + +end From 1317f1f6069829669c2eba2da76bc200bd17f88b Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 26 Nov 2013 17:43:31 -0500 Subject: [PATCH 5/6] week 7 hw added --- week7/piratetranslator.rb | 18 ++++++++++++++++++ week7/questions.txt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 week7/piratetranslator.rb create mode 100644 week7/questions.txt diff --git a/week7/piratetranslator.rb b/week7/piratetranslator.rb new file mode 100644 index 0000000..81c146c --- /dev/null +++ b/week7/piratetranslator.rb @@ -0,0 +1,18 @@ +class PirateTranslator + +PIRATE_VOCABULARY = {"Hello Friend" => "Ahoy Matey"} + + def say(what) + @heard = listen_pirate(what) + end + + def translate + @heard + "\n Shiber Me Timbers You Scurvey Dogs!!" + end + +private + def listen_pirate(what) + PIRATE_VOCABULARY[what] + end +end + diff --git a/week7/questions.txt b/week7/questions.txt new file mode 100644 index 0000000..9c36453 --- /dev/null +++ b/week7/questions.txt @@ -0,0 +1,18 @@ + +Please Read Chapters 23 and 24 DuckTyping and MetaProgramming + +Questions: +1. What is method_missing and how can it be used? + Method_missing is a method that receives the symbol of a non-existent method that has been called, an array of the arguments that were passed in the original call, and any block passed to the original method. It can be used to gracefully intercept any unanswerable messages that are sent to your code and return an error message that displays the problem with your input, specifically -- that the method in question is nonexistent. + +2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + An eigenclass is used to capture the creation of signleton methods and the objects for which they are defined. Singleton methods live within the eigenclass. + +3. When would you use DuckTypeing? How would you use it to improve your code? + DuckTyping is used in cases where the focus is what on the code should be doing and not what the code is in and of itself. If the purpose of a class is to act like a String, then we treat it as such regardless of its innate "type". It can be used to make code interface more efficiently with larger data sets. + +4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + Class methods can only be called on classes and instance methods can only be caled on an instance of a class. Instance_eval is used to define class methods. Class_eval is used to define instance methods. + +5. What is the difference between a singleton class and a singleton method? + A singleton method is a method that is specific to a particular object; the resultant anonymous class that gets created in the interim -- behind the scenes -- is the singleton class. From 5746f1b4c3348e8f3820a1fcd3ba76fb6d28ed95 Mon Sep 17 00:00:00 2001 From: Steven Xu Date: Tue, 10 Dec 2013 02:20:39 -0800 Subject: [PATCH 6/6] tictactoe.rb added for final --- final/tic-tac-toe.rb | 127 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 final/tic-tac-toe.rb diff --git a/final/tic-tac-toe.rb b/final/tic-tac-toe.rb new file mode 100644 index 0000000..ef17f6f --- /dev/null +++ b/final/tic-tac-toe.rb @@ -0,0 +1,127 @@ +class TicTacToe + SYMBOLS = [:X,:O] + attr_accessor :player, :board + +private + def board_constructor + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + end + + def player_is(player_sym=nil) + player_sym ||= SYMBOLS.sample + @player_symbol = { + :computer => SYMBOLS.reject{|s| s==player_sym}.first, + :player => player_sym + } + end + +public + def initialize(current_player=nil,player_sym=nil) + board_constructor + @current_player = current_player || [:computer, :player].sample + player_is(player_sym) + @player_win = false + @computer_win = false + end + + def computer_symbol + @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def current_player + {:computer => "Computer", + :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp.capitalize + end + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + puts "Please select another location." + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def computer_move + move = get_computer_move + @board[move] = computer_symbol + @current_player = :player + move + end + + def get_computer_move + @board.select{|k,v| v.to_s.strip.empty?}.collect{|k,v| k}.sample + end + + def current_state + row1 = "| #{@board[:A1]} | #{@board[:A2]} | #{@board[:A3]} |\n" + row2 = "| #{@board[:B1]} | #{@board[:B2]} | #{@board[:B3]} |\n" + row3 = "| #{@board[:C1]} | #{@board[:C2]} | #{@board[:C3]} |\n" + row1 + "-"*row1.size+"\n"+ row2 + "-"*row2.size+"\n"+ row3 + "-"*row3.size+"\n"+ + "+"*row1.size + end + + def determine_winner + p = @board.select{|k,v| v==player_symbol} + c = @board.select{|k,v| v==computer_symbol} + + played = p.collect{|k,v| {k[0].to_sym=>k[1].to_i}} + computed = c.collect{|k,v| {k[0].to_sym=>k[1].to_i}} + [:A, :B, :C].each do |l| + return if @player_win = played.collect{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + return if @computer_win = computed.collect{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + end + [1,2,3].each do |l| + return if @player_win = played.collect{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + return if @computer_win = computed.collect{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + end + return if @player_win = p.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @player_win = p.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + return if @computer_win = c.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @computer_win = c.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + end + + def player_won? + !!@player_win + end + + def computer_won? + !!@computer_win + end + + def draw? + !player_won? && !computer_won? + end + + def over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.collect{|k,v| k} + end +end