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

Week 7 Homework + Final Submission #97

Open
wants to merge 15 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
2 changes: 1 addition & 1 deletion week1/exercises/roster.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
15, Owen, [email protected], owenkelly, n/a, @owenkelly
16, Robert McCreary Mannino, [email protected], psytau, maalaoshi, [email protected]
17, Rukia, [email protected], rukk86, no twitter, Rukia
18,
18, Sarah, [email protected], sarahwheeler, @SeeSarahCode, Sarah Wheeler
19,
20, Zack Walkingstick, [email protected], zackwalkingstick, no twitter, @zack
62 changes: 62 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,77 @@
Sarah Wheeler
1/14/14
Ruby 110

Week 1 Homework

Please read:
Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)



1. What is an object?

An object is simply an instance of a class. Every object
has a unique object id (because it occupies a unique space
in the computer's memory), is associated with a class, and
can be instantiated through the .new method. Multiple
variables can point to the same object, however.

2. What is a variable?

A variable is not an object, but actually holds a reference
to an object. Syntactically, variables should be all lowercase,
can start with or include underscores, and include (but not
begin with) numbers. Depending on whether they are prefixed
by $, @, @@, or no symbol at all, the scope of the variable
changes. For example, you can't access an object's instance
variables until you have written an accessor method in the
class definition, because its scope is restricted to where
it was first created.

3. What is the difference between an object and a class?

Classes are not objects themselves, but they do act as the
blueprints for objects. They allow you to dictate what actions
can be performed with, to, and through objects by giving you
the power to define the methods that will be available to them.
Classes are used to instantiate individual objects that are a
referenced through the use of variables.

4. What is a String?

Strings are sequences of characters (either binary or printable)
that belong to the String class. Strings are created with string
literals (example: string1 = "String!") and can appear between
either single or double quotes.

5. What are three messages that I can send to a string object? Hint: think methods

.capitalize will tell a String object to make the first letter
of your string a capital letter.

.split will divide a String object by the character you pass it
(for example, a comma, period, or vertical bar), and create an
Array out of the pieces.

.length will tell you how many characters a String object contains.


6. What are two ways of defining a String literal? Bonus: What is the difference between them?

Method 1: By using single quotes as your delimiters, like so:

'This is a string.'

Method 2: By using %Q followed by any nonalphanumeric or nonmultibyte
character, which acts like a double quote like so:

%Q{This is also a string.}

The difference between single quotes and double quotes is that double
quotes support more escape sequences, as well as string interpolation
(inserting a variable into text through #{variable}). Since single
quotes perform fewer functions, they are also processed slightly faster
by the computer.

14 changes: 9 additions & 5 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
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
#pending
result = @my_string.split(".")
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"))'
#pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
@my_string.encoding.should eq Encoding.find("UTF-8")

end
end
end
Expand Down
42 changes: 42 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
Sarah Wheeler
1/22/14
Ruby 110

Week 2 Homework

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 indexed with integers, while a hash
can use any object as its index. Unlike arrays,
hashes store two objects when they are populated--
the key (i.e., the index) and its value.

2. When would you use an Array over a Hash and vice versa?

You would want to use an array if you wanted to
store collections of objects that can be accessed
through an integer index. If you found yourself
writing a hash that used integers for its unique
keys, you might be better off using an array instead.

You would want to use a hash when your collection requires
more flexibility with the types of objects it can use for
indexes (i.e., non-integers). If you wanted to associate a
relationship between two strings, for example, a hash would
probably be the most appropriate collection to use. Another
major advantage is that Ruby will remember the order of the
items added to your hash.

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" (Pickaxe, p. 73, 4th ed). Although it isn't
a class and can't have instances, you can use its Mixin
functionality to lend its methods to other classes.

Enumerable is a module that was created to provide collections
(arrays and hashes) with methods that allow them to sort,
search, and iterator through their elements.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?

Ruby is a single-class inheritance language, but through the use
of modules and mixins, it is possible to mimic multiple
inheritance in a controlled environment.

5. What is the difference between a Module and a Class?

A module cannot have instances, which means that it is not meant
to help create objects like a class is--instead, it is meant to
provide a set of specialized methods to another class.
24 changes: 24 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SimonSays

def echo(message)
p message
end

def shout(message)
p message.upcase
end

def repeat(message, n=2)
p ([message] * n).join(' ')
end

def start_of_word(word, n)
p word[0, n] # I REALLY overthought this method the first 80 times I tried it
# ... but at least I won't forget it now!
end

def first_word(message)
word_one = message.split(" ")
p word_one[0]
end
end
21 changes: 21 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Calculator

def sum num
num.inject(0) {|sum, current| sum + current }
end

def multiply x, y=1
n = [x, y].flatten(1)
n.inject(1) { |sum, current| sum * current }
end

def pow n1, n2
n1**n2
end

def fac i
return 1 if i == 0
(1..i).inject(:*)
end

end
35 changes: 35 additions & 0 deletions week3/homework/monster_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sarah Wheeler
# 1/28/14
# Ruby 110

# Monster Questions

# 1. How many monsters are nocturnal?

$monsters.count { |m| m[:nocturnal] }


# 2. What are the names of the monsters that are nocturnal?

$monsters.select { |m| m[:nocturnal]}.collect { |m| m[:name]}


# 3. How many legs do all our monsters have?

$monsters.inject(0) do |total, m|
total += m[:legs]
end


# 4. What are the 2 most common dangers and vulnerabilities?

d_list, v_list = Hash.new(0), Hash.new(0)

d = $monsters.map { |m| m[:dangers] }.flatten
d_array = d.each { |m| d_list[m] += 1 }
d_list = d_array.sort_by { |k, v| v }[0..1]

v = $monsters.map { |m| m[:vulnerabilities] }.flatten
v_array = v.each { |m| v_list[m] += 1 }
v_list = v_array.sort_by { |k, v| v }[0..1]

77 changes: 77 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Sarah Wheeler
1/28/14
Ruby 110

Week 3 Homework

Please Read:
- Chapter 6 Standard Types
- Review Blocks
Expand All @@ -6,10 +12,81 @@ Please Read:

1. What is a symbol?

A symbol is an object that references a name
(and sometimes strings). Symbols are constant,
unique, and don't change (immutable). They are a way of
referencing an object multiple times without
creating multiple copies/object ids of that object,
which helps reduce the amount a memory a program is
using. For these reasons, they are frequently used as
keys in hashes.

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

A string can be changed at any time, whereas a symbol
cannot be changed after it is initially created. Another
important distinction is that symbols take up less memory
space than strings do, and they are also faster when it
comes to processing equality comparisons. (Even strings
with the same value must first compare object id's before
comparing the contents of the string itself; symbols have
unique object id's, so processing time is saved with this
step.)

3. What is a block and how do I call a block?

A block functions similarly to an anonymous method; it is
a piece of code enclosed between either braces ({}) or
"do"/"end" that takes parameters between vertical bars,
and can be passed around to other methods, used for iteration,
or stored in a variable. Blocks are especially useful as
an alternative to loops when combined with the Enumerable
module, or to provide more transactional control (for
example, having a file automatically open/close itself
without explicit instructions from the file user).

You can call a block by inserting the "yield" statement into
a method and invoking that method, or by simply writing the
block after a method call that uses them. For example, the
.map method can take a block object when it is called:

array = ["a", "b", "c"]
array.map { |x| "#{x} is a letter"}


Blocks aren't executed when Ruby first sees them; instead,
Ruby will wait for the method that the block is being passed
to to be called, and then it will execute the block.

If you have made your block into a Proc object, you can also
call the block by using the .call method on the proc.

4. How do I pass a block to a method? What is the method signature?

A block can be passed to a method in a couple ways.
Blocks can be inserted directly into the flow of a method
by using the 'yield' statement, which will initialize the
block before returning to the rest of the method's code.

A block can also be explicitly passed to a method by including
it as a method parameter and prefixing the name with an ampersand
(as in: &block_name). Ruby will then locate the block, convert it
to a Proc object, and treat it like a variable. You can then call
the block inside of the method by using .call and the block's name
(block_name.call). The method signature--which is the method name
followed by its list of arguments--would look something like this:

def method_receives_block (var1, var2, &block_name)
block_name.call
end

This allows the method to use the block as its return value.

5. Where would you use regular expressions?

Regular expressions are typically used to search, match,
and replace string sequences. They allow you to define
a specific pattern for your program to look for, and
depending on what method you used in combination with the
regular expression, that particular action will be applied
to the results (or simply be returned to you by the method).
Loading