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

Homework + Tic-Tac-Toe #102

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cb06e99
Fix order of operations test, implement pending tests
Thesoro Oct 9, 2013
e04fbc3
Homework for week 1.
Thesoro Oct 16, 2013
9ab8025
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Oct 16, 2013
88f889c
Rob W's week 2 homework
Thesoro Oct 22, 2013
e8d5ea1
Updating to week 3
Thesoro Oct 23, 2013
1f18990
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Oct 23, 2013
2b7d372
Week 3 Homework
Thesoro Oct 29, 2013
2624986
Fixing location of week 3 hw
Thesoro Oct 29, 2013
cadb1f3
Typo fix
Thesoro Oct 29, 2013
ed75ae9
adding week 4 material
Thesoro Oct 30, 2013
644248c
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Oct 30, 2013
0901fc3
Add answers to week 4 questions
Thesoro Nov 5, 2013
7220d27
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 6, 2013
bdc6401
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 6, 2013
94946d8
Add week 5 materials for real
Thesoro Nov 6, 2013
177c0de
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 8, 2013
1f19309
Add midterm prompt
Thesoro Nov 8, 2013
629f095
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 13, 2013
ae84829
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 20, 2013
249c35d
add week 7 material
Thesoro Nov 20, 2013
93c8ae9
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Nov 27, 2013
551a2b4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Thesoro Dec 4, 2013
302cd12
Add tic-tac-toe folder, features.
Thesoro Dec 10, 2013
ad831b9
Fixing tictactoe folder.
Thesoro Dec 10, 2013
0196284
Fixing it for real now.
Thesoro Dec 10, 2013
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
1 change: 1 addition & 0 deletions Untitled Folder/tictactoe
Submodule tictactoe added at 43d6d8
1 change: 1 addition & 0 deletions backup/tictactoe
Submodule tictactoe added at 43d6d8
5 changes: 5 additions & 0 deletions example_gems/prevet_mir/lib/prevet_mir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PrevetMir
def self.hi
puts "Prevet, mir!"
end
end
Binary file added example_gems/prevet_mir/prevet_mir-0.0.0.gem
Binary file not shown.
11 changes: 11 additions & 0 deletions example_gems/prevet_mir/prevet_mir.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Gem::Specification.new do |s|
s.name = 'prevet_mir'
s.version = '0.0.0'
s.date = '2013-11-19'
s.summary = "Making a test gem"
s.description = "This gem was the first gem made by man. No gem has so far matched it for gemliness. Test_gem is a hundred dollars in your pocket right away. Don't turn your back on test_gem; it's trying to give you an ice cream cone and you'll get ice cream on your back."
s.authors = ["Rob Whitehead"]
s.email = '[email protected]'
s.files = ["lib/prevet_mir.rb"]
s.homepage = 'https://rubygems.org/gems/prevet_mir'
end
9 changes: 9 additions & 0 deletions example_gems/test_gem/bin/test_gem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

require 'test_gem'

h = HelloWorld.new

h.test

h.test_args ARGV.first
2 changes: 2 additions & 0 deletions example_gems/test_gem/lib/test_gem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
puts "GEM GEM GEM GET OVER HERE AND GET DOWN WITH YOUR OWN BAD SELVES FLY YO FREAK FLAGS"

12 changes: 12 additions & 0 deletions example_gems/test_gem/test_gem.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Gem::Specification.new do |s|
s.name = 'test_gem_chiteri'
s.version = '0.0.0'
s.date = '2013-11-12'
s.summary = "Making a test gem"
s.description = "This gem was the first gem made by man. No gem has so far matched it for gemliness. Test_gem is a hundred dollars in your pocket right away. Don't turn your back on test_gem; it's trying to give you an ice cream cone and you'll get ice cream on your back."
s.authors = ["Rob Whitehead"]
s.email = '[email protected]'
s.files = ["lib/test_gem.rb"]
s.homepage = 'https://rubygems.org/gems/test_gem'
s.executables << 'test_gem'
end
38 changes: 38 additions & 0 deletions midterm/even_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class EvenNumber
include Enumerable
attr_reader :input
def initialize *input
@input = input
check = true
@input.each do |i|
if i.odd?
check = false
end
end
if not check
@input = "even numbers only buddy"
end
@current = -1
end

def compare a, b
if @input.is_a?(Array)
output = false
if @input[a] == @input[b]
output = true
end
output
end
end

def range a,b
r = @input[a]..@input[b]
out = r.step(2).to_a
out
end

def next
@current += 1
@input[@current]
end
end
37 changes: 37 additions & 0 deletions midterm/even_number_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#- Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber.
# - The EvenNumber class should:
# - Only allow even numbers
# - Get the next even number
# - Compare even numbers
# - Generate a range of even numbers

require "#{File.dirname(__FILE__)}/even_number.rb"


describe EvenNumber do
it "should only allow even numbers" do
a = EvenNumber.new(3, 4, 5)
a.input.should == "even numbers only buddy"
end

it "should be able to get the next even number" do
a = EvenNumber.new(2, 4, 6, 8)
a.next.should eq 2
a.next.should eq 4
a.next.should eq 6
a.next.should eq 8
end

it "should compare even numbers" do
a = EvenNumber.new(2, 4)
a.compare(0,1).should be false
b = EvenNumber.new(2,2)
b.compare(0,1).should be true
end

it "should generate a range of even numbers" do
a = EvenNumber.new(2,8)
a = a.range(0,1)
a.should eq [2,4,6,8]
end
end
22 changes: 22 additions & 0 deletions midterm/instructions_and_questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,37 @@ Instructions for Mid-Term submission and Git Review (10pts):

Questions (20pts):
- What are the three uses of the curly brackets {} in Ruby?
Curly braces in Ruby are used for delimiting blocks (by convention, single line blocks--multiline blocks are typically done with do/end), string interpolation (e.g. "hello #{world}", where world is a variable or expression), and defining hashes.

- What is a regular expression and what is a common use for them?
Regular expressions are a set of symbols used to search out specific patterns of text. A regular expression might be used to verify that input is shaped like an email address, or pick out any capitalized words in a sentence.

- What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby?
Strings are stored as literal objects, each instance of which is unique. If an identical string is assigned twice, i.e. a = "hello", b = "hello", there will be two objects with different memory addresses.

In contrast, any symbol with an identical name is assigned to the same memory address.

There is a similar distinction between FixNums and Floats--FixNums of the same value have the same memory address, and floats of the same value do not.

- Are these two statements equivalent? Why or Why Not?
1. x, y = "hello", "hello"
2. x = y = "hello"

The two statements are not equivalent. #1 saves them as two different objects. If x is altered, for example if x.chop! is called, removing the last character, y will be unaffected.
In #2, both variables point to the same object. If x.chop! is called, y will also be affected--it points to the same string that just got chopped.


- What is the difference between a Range and an Array?
Ranges define a set of numbers in a certain order incrementing in a certain way. Their specific values are calculated at run-time. Until then they are stored as a Range object. An array can contain objects in any order and can be changed at any time. Arrays can contain any kind of object as elements, but the objects that define a range must respond to the 'succ' method to define the next object in succession.

- Why would I use a Hash instead of an Array?
Hashes are more versatile in use--instead of keeping track of the order of the array and make sure it's not disrupted as you move things around, you can simply assign keys and Ruby will keep it straight. On the other hand, hashes are much more expensive, processor-wise.

- What is your favorite thing about Ruby so far?
Its forgiveness--I find myself using a lot of Python syntax by mistake (for example, I put method arguments in parens) and Ruby usually doesn't care.

- What is your least favorite thing about Ruby so far?
When something in Ruby is opaque, it is REALLY opaque. The syntax allows for very elegant code, but it can be difficult to understand what is going on.

Programming Problems (10pts each):
- Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber.
Expand Down
4 changes: 2 additions & 2 deletions midterm/mid_term_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "#{File.dirname(__FILE__)}/turkey"
require "#{File.dirname(__FILE__)}/turkey.rb"

describe Turkey do

Expand All @@ -20,7 +20,7 @@

end

require "#{File.dirname(__FILE__)}/thanksgiving_dinner"
require "#{File.dirname(__FILE__)}/thanksgiving_dinner.rb"

describe ThanksgivingDinner do

Expand Down
93 changes: 93 additions & 0 deletions midterm/thanksgiving_dinner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
=begin
responds to: guests, menu, diet, proteins (array w/strings), veggies(array w/symbols and strings), dessert (hash)
kind of: Dinner
takes parameter: :diet
method: seating_chart_size (inject)
whats_for_dinner
whats_for_dessert
=end
class Dinner
end

class ThanksgivingDinner < Dinner
attr_accessor :diet, :guests, :menu, :proteins, :veggies, :desserts
def initialize diet
@diet = diet
@guests = []
@menu = {diet: @diet}
if @menu[:diet] == :vegan
@menu = { diet: :vegan, proteins: ["Tofurkey", "Hummus"],
veggies: [:ginger_carrots , :potatoes, :yams],
desserts: {:pies => [:pumkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}}
end
end

def seating_chart_size
@guests.inject(0) {|sum, element| sum + element.length}
end

def guests
@guests
end

def whats_for_dinner
"Tonight we have proteins #{things_lister(@menu[:proteins])}, and veggies #{things_lister(@menu[:veggies])}."
end

def whats_for_dessert
"Tonight we have #{dessert_counter(@menu[:desserts])} delicious desserts: #{things_lister(@menu[:desserts][:pies])}, #{things_lister(@menu[:desserts][:other])}, and #{@menu[:desserts][:molds].length} molds: #{things_lister(@menu[:desserts][:molds], true)}."
end

def things_lister input, molds = false
d = list_preparer(input)
if d.length == 1
output = d[0]
elsif d.length == 2
output = d[0] + " and " + d[1]
else
count = 0
output = ""
if not molds
d.each do |i|
if count < d.length-1
output += (i + ", ")
count += 1
else
output += ("and " + i)
end
end

else
d.each do |i|
if count < d.length-1
output += (i + " and ")
count += 1
else
output += i
end
end
end
end

output
end

def list_preparer input
d = input.flatten(-1).map! do |i| #turn the input into a flat array of words
i.to_s.capitalize
end
d.delete_if {|x| x == "Pies" or x == "Other" or x == "Molds"}
d.each do |i|
pos = /[_ ]/ =~ i
if pos
i[pos] = " "
i[pos+1] = i[pos+1].capitalize
end
d
end
end

def dessert_counter input
input[:pies].length + input[:other].length + input[:molds].length
end
end
45 changes: 45 additions & 0 deletions midterm/turkey.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=begin
attributes: weight
responds to: gobble_speak
inherits from: Animal
=end
class Animal
end

class Turkey < Animal
attr_reader :weight
def initialize weight
@weight = weight
end

def gobble_speak input
text = input.split
out = text.map do |i|
gobble_maker(i)
end
out.join(" ")
end

def gobble_maker word
cap = false
out = "gobble"
punct = ""
if word[0] =~ /[A-Z]/ #check if word is capitalized and match the case
out = out.capitalize
end
while word[-1] =~ /[;:.,!?'"]/ #find and grab all punctuation marks from end of word
punct += word[-1]
word.chop!
end
punct.reverse!
apost = word=~/'/ #finds the apostrophe and inserts it proportionally where it should go in "gobble", rounding down to discourage end-of-word apostrophe placement
if apost
apost = ((Float(apost+1) / word.length) * 6) - 0.5
out.insert(apost.round, "'")
end
out + punct #put it all together

end


end
18 changes: 18 additions & 0 deletions midterm/wish_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#- Make the rspec tests in wish_list_spec.rb pass by writing a WishList class
# - The WishList class should:
# - Mixin Enumerable
# - Define each so it returns wishes as strings with their index as part of #the string
class WishList
attr_accessor :wishes
include Enumerable
def initialize
@wishes = []
end

def each
@wishes.each_index do |i|
yield "#{i+1}. #{@wishes[i]}"
end
end
end

2 changes: 1 addition & 1 deletion midterm/wish_list_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "#{File.dirname(__FILE__)}/wish_list"
require "#{File.dirname(__FILE__)}/wish_list.rb"

describe WishList do
before :each do
Expand Down
Loading