Skip to content

Instantly share code, notes, and snippets.

@carolineartz
carolineartz / challenge1_3_die_class1_numeric.rb
Last active January 3, 2016 20:19 — forked from dbc-challenges/phase0_week2_challenge_template.rb
Implement a basic Die class which can be initialized with some number of sides. We can then roll the die, returning a random number. It should work like this: die = Die.new(6) die.sides # returns 6 die.roll # returns a random number between 1 and 6 If we pass Die.new a number less than 1, we should raise an ArgumentError. This is done using the …
#######################################PSEUDOCODE###################################
# INPUT, OUTPUT & PSEUDOCODE:
# Initialize => INPUT: number of sides = integer > 0
# OUTPUT: new Die object
# # set instance variable sides to value of passed argument
# Die#sides => INPUT: none
# OUTPUT: number of sides
# getter/accessor method for sides
# Die#roll => INPUT: none
@carolineartz
carolineartz / challenge1_3_die_class2_arbitrary_symbols.rb
Last active January 3, 2016 20:39 — forked from dbc-challenges/phase0_template_gist.rb
Working off your previous Die class from Die Class 1, implement a new Die class which takes an array of strings as its input. When Die#roll is called, it randomly returns one of these strings. If Die.new is passed an empty array, raise an ArgumentError. It should work like this: `die = Die.new(['A', 'B', 'C', 'D', 'E', 'F’])` `die.sides # still …
#######################################PSEUDOCODE###################################
# INPUT, OUTPUT & PSEUDOCODE:
# Initialize => INPUT: array of strings (labels)
# OUTPUT: new Die object
# a. raises argument error if passed an empty array
# b. Creates instance variabel for labels
# Die#sides => INPUT: none
# OUTPUT: number of sides
# a. calculate and return size of labels array
@carolineartz
carolineartz / challenge1_3_guessing_game.rb
Last active January 3, 2016 20:39 — forked from dbc-challenges/phase0_template_gist.rb
Exercise: Build a simple guessing gameCreate a GuessingGame class which is initialized with an integer called answer.Define an instance method GuessingGame#guess which takes an integer called guess as its input. guess should return the symbol :high if the guess is larger than the answer, :correct if the guess is equal to the answer, and :low if …
####################################### PSEUDOCODE #####################################
# INPUT: initialize a game passing the correct integer answer to instantiate;
# pass a guess value to #guess
# OUPUT: #guess outputs symbol representing
# if the most recent guess is too high, too low, or correct
# #solved? outputs true if the most recent guess is correct and flase otherwise.
# STEPS: #initialize -> set answer to instance variable, answer
#guess -> return corresponding symbol for too high, too low, correct
@carolineartz
carolineartz / challenge1_3_class_warfare.rb
Last active January 3, 2016 20:39 — forked from dbc-challenges/phase0_template_gist.rb
Exercise: Class Warfare, Validate a Credit Card NumberGiven a credit card number we should be able to validate whether it is valid based on the Luhn algorithm. While the word algorithm sounds scary you can just think of them as a series of steps you use to solve a specific problem. An example algorithm:Heat water until boilingAdd pastaIf pasta c…
####################################### PSEUDOCODE #####################################
# INPUT: Initialize object with input 16-digit integer parameter
# OUPUT: #initialize outputs invalid ArgumentError for input integers not 16 digits
# #CreditCard#check_card outputs boolean true/false for
# valid/invalid digits as credit card number
# STEPS: #initialize raise ArgumentError if input parameter is not 16 digits
# set input to instance variable for card number
#card_number isolate digits to double and multiply them by 2
#!/usr/bin/env ruby
require 'open-uri'
require 'net/http'
require 'iconv'
require 'optparse'
require 'fileutils'
require 'cgi'
$options = {}
class Drawer
#don't need attribute reader for content at this point
def initialize
@contents = []
@open = true
end
def open
@open = true
# OBJECTIVE 3: CREATE PEZ CLASS
# See objectives #1, #2, and #6 after #3
class PezDispenser
@@available_flavors = %w(cherry chocolate cola grape lemon orange peppermint raspberry strawberry raspberry-lemon strawberry-vanilla)
# Class variable describes current market available pez flavors (wikipedia; exclues sugarfree & sour)
def initialize(flavors)
unless flavors == flavors.select { |flavor| valid_flavor?(flavor) }
raise ArgumentError.new('Contains invalid flavors') # raises if input includes invalid flavor
end
@carolineartz
carolineartz / 0.2.1-boggle_class_from_methods.rb
Last active August 29, 2015 13:55 — forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(*coords)
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join('')
end
require 'sqlite3'
$db = SQLite3::Database.open 'congress_poll_results.db'
def print_arizona_reps
puts 'AZ REPRESENTATIVES'
az_reps = $db.execute("SELECT name FROM congress_members WHERE location = 'AZ'")
az_reps.each { |rep| puts rep }
end
# REVIEWING ASSERT STATEMENT
# ---------
def assert
raise "Assertion failed!" unless yield
end
name = "bettysue"
assert { name == "bettysue" }
assert { name == "billybob" }