Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

tictactoe #184

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added week1/homework/.DS_Store
Binary file not shown.
15 changes: 6 additions & 9 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ Chapter 3 Classes, Objects, and Variables
p.90-94 Strings

1. What is an object?
An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about.
An object is everything that is manipulated, object start as instances of classes

2. What is a variable?
A variable is a name for a location in memory. It can contain, or point to, any type of object.
A variable is a reference to an object, used to track objects

3. What is the difference between an object and a class?
An object is an instance of a class, or a specific thing of that class's type in memory. The class is the specifics that are common to all things of that type. The classification of a concept or a thing is a class. A specific thing or concept of a class's type in memory is an object. For example: All books have titles (Class). This book's title is "Harry Potter and the Goblet of Fire" (Object).
A class is an object but contains methods, object of class Class

4. What is a String?
A string is how Ruby understands text. It is a collection of characters (Bytes), and can be created by making an instance of the String class (String.new) or as a string literal ("",'', %Q[]).
simple sequence of characters, normally holds printable data but not required. Object of class String

5. What are three messages that I can send to a string object? Hint: think methods
chomp! - removes newline characters, or the specified characters, from the end of a string
strip! - removes leading or trailing whitespace from a string
split - returns an array of strings made up of the original string separated on whitespace or the specified characters or regexp
squish, chomp, first

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same.

single and double quoted, double quoted supports more escape sequences (such as \n)
7 changes: 4 additions & 3 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
@my_string.should have(@my_string.size).characters
end
it "should be able to split on the . charater" do
result = @my_string.split('.')
result.should have(2).items
result = @my_string.split(/\s*\.\s*/)
result.should have(2).items
end
it "should be able to give the encoding of the string" do
@my_string.encoding.should eq (Encoding.find("UTF-8"))
results = @my_string.encoding
results.should eq(Encoding.find("UTF-8"))
end
end
end
18 changes: 18 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
<<<<<<< HEAD
Both are a collection of objects that are indexed, an array is indexed with integers while a hash is indexed with any object (symbols, strings, regular expressions, etc)

2. When would you use an Array over a Hash and vice versa?
Use a hash when you want to obtain an object by referencing an object (example, items in a purse: lipstick, wallet, etc) while an array is an ordered collection that is referenced linearly (0, 1, 2, 3)

3. What is a module? Enumerable is a built in Ruby module, what is it?
A module is a way of grouping together methods, classes, and constants. They provide a namespace, prevent name clashes, and supports the mixin facility.

Enumerable: applies a function or operation to all items, since it runs through all objects you are able to use min/max/sort. Seems to be a simple way to evaluate all items instead of running through a loop.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No, but you can use mixin modules to include subsets of information

5. What is the difference between a Module and a Class?
A module contains methods and logic but does not retain information. A class contains objects while a module is not an object.
=======
An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order.

2. When would you use an Array over a Hash and vice versa?
Expand All @@ -16,3 +33,4 @@ No, multiple inheritance is not allowed in Ruby. You can include multiple module

5. What is the difference between a Module and a Class?
A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes.
>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c
26 changes: 26 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
module SimonSays
<<<<<<< HEAD
def echo(message)
message
end

def shout(message)
message.upcase
end

def repeat(message,times_repeated=2)
a = [message]*times_repeated
a.join(" ")
end

def start_of_word(message,char=1)
b = message.split(//)
b[0...char].join
end

def first_word(message)
c = message.split(/ /).to_a
c[0]
end
end
=======
def echo(st)
st
end
Expand All @@ -20,3 +45,4 @@ def repeat(st, t=2)
([st]*t).join(' ')
end
end
>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c
47 changes: 47 additions & 0 deletions week2/homework/simon_says.rb.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module SimonSays
<<<<<<< HEAD
def echo(message)
message
end

def shout(message)
message.upcase
end

def repeat(message,times_repeated=2)
a = [message]*times_repeated
a.join(" ")
end

def start_of_word(message,char=1)
b = message.split(//)
b[0...char].join
end

def first_word(message)
c = message.split(/ /).to_a
c[0]
end
end
=======
def echo(st)
st
end

def shout(st)
st.upcase
end

def first_word(st)
st.split.first
end

def start_of_word(st,i)
st[0...i]
end

def repeat(st, t=2)
([st]*t).join(' ')
end
end
>>>>>>> c60983d0acfe2bba5021801c2cf14f56b297c72c
5 changes: 5 additions & 0 deletions week3/exercises/inclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require /.named_thing.rb

class Vampire
include NamedThing
end
29 changes: 29 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
class Calculator
<<<<<<< HEAD
def sum(num_array)
num_array.inject(0) do |total, value|
total + value
end
end

def mult(x, y)
x*y
end

def multa(num_array)
num_array.inject(1) do |total, value|
total * value
end
end

def raiseby(x,y)
x**y
end

def factorial(x)
(1..x).inject(1) do |total, value|
total*value
end
end
=======
def sum(array)
array.inject(0){|sum, x| sum +x}
end
Expand All @@ -21,4 +48,6 @@ def fac(n)
def pow_fac(base=nil, p)
(1..p).to_a.inject(1){|f,v| f *= base || v}
end
>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597
end

80 changes: 80 additions & 0 deletions week3/homework/calculator_spec_hw.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require "#{File.dirname(__FILE__)}/calculator"

describe Calculator do

before do
@calculator = Calculator.new
end

describe "#sum" do
it "computes the sum of an empty array" do
@calculator.sum([]).should == 0
end

it "computes the sum of an array of one number" do
@calculator.sum([7]).should == 7
end

it "computes the sum of an array of two numbers" do
@calculator.sum([7,11]).should == 18
end

it "computes the sum of an array of many numbers" do
@calculator.sum([1,3,5,7,9]).should == 25
end
end

# Once the above tests pass,
# write tests and code for the following:

describe "#mult" do
it "multiplies two numbers" do
@calculator.mult(7,2).should == 14
end

it "multiplies two numbers, same value" do
@calculator.mult(2,2).should == 4
end
end

describe "#multa" do
it "multiplies an array of numbers" do
@calculator.multa([1,2,3]).should == 6
end

it "multiplies an array of numbers" do
@calculator.multa([5,7,10]).should == 350
end
end

describe "#raiseby" do
it "raises one number to the power of another number" do
@calculator.raiseby(5,2).should == 25
end

it "raises one number to the power of another number" do
@calculator.raiseby(3,5).should == 243
end
end

# http://en.wikipedia.org/wiki/Factorial
describe "#factorial" do
it "computes the factorial of 0" do
@calculator.factorial(0).should == 1
end

it "computes the factorial of 1" do
@calculator.factorial(1).should == 1
end
it "computes the factorial of 2" do
@calculator.factorial(2).should == 2
end
it "computes the factorial of 5" do
@calculator.factorial(5).should == 120
end
it "computes the factorial of 10" do
@calculator.factorial(10).should == 3628800
end
end

end
18 changes: 18 additions & 0 deletions week3/homework/questions_hw3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
1. What is a symbol?
A symbol is an identifier corresponding to a string of characters, often a name.

2. What is the difference between a symbol and a string?
A symbol is an unchangable string.

3. What is a block and how do I call a block?
A block is a bit of code between either braces or do/end, blocks are not objects.
A block can be called by using call, yield or [].
However, you can also call it by name.(arg) which maps to name.call(arg)

4. How do I pass a block to a method? What is the method signature?
You pass a block to a method by listing the last parameter as a prefix with an ampersand. ex. def test(&block)
A method signature is the method name and all if its arguments.

5. Where would you use regular expressions?
A regular expression is to identify a pattern in a string.
I imagine using regular expressions when finding data points based on unique ID format.
12 changes: 11 additions & 1 deletion week4/class_materials/timer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
class Timer
<<<<<<< HEAD
def self.time_code(&my_code)
start_time = Time.now
my_code.call
end_time = Time.now
end_time - start_time
end
end
=======
def self.time_code(n=1)
start_time = Time.now
n.times{yield}
end_time = Time.now
(end_time - start_time) / n.to_f
end
end
end
>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597
21 changes: 21 additions & 0 deletions week4/exercises/odd_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class OddNumber
attr_reader :value

def initialize(value)
@value = value
end

def succ
new_val = nil
if
OddNumber.new(@value + 1)
else
OddNumber.new(@value+1)
end
new_val
end

def <==>(other)
@value <==>other.value
end
end
13 changes: 12 additions & 1 deletion week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
class Worker
<<<<<<< HEAD
def self.work (n=1)
result = nil
n.times do
result = yield
end
result
end
end
=======
def self.work(n=1)
n.times.inject(nil){yield}
end
end
end
>>>>>>> 217a9fddb9c3593e5125cdc0b20bbd32afab6597
12 changes: 12 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
file.gets reads from a file
file.open("file name", "r") opens a file for reading

2. How would you output "Hello World!" to a file called my_output.txt?
file.open("my_output.txt", "r+")
puts "Hello World!"
file.close

3. What is the Directory class and what is it used for?
The Directory class represents directories in a file structure. Directories are used in many ways. You can open files using a directory, update the file, or create a new file.

4. What is an IO object?
Part of IO class, An IO object is a bidirectional channel between a Ruby program and some external resource. Can be used for reading or writing to and from a file using methods

5. What is rake and what is it used for? What is a rake task?
Rank groups tasks, a rank task is an action for a file.
Loading