Last active
December 10, 2015 20:48
-
-
Save rsepassi/4490275 to your computer and use it in GitHub Desktop.
Ryan's comments for (Ketabchi-Oleinikov)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loopstuff() | |
i = 0 | |
# Instead of having this separate some_bool, just break out of the loop with 'break' | |
# when the condition is met | |
some_bool = true | |
while some_bool | |
if i >250 && i % 7 == 0 | |
print i | |
some_bool = false | |
end | |
i += 1 | |
def super_print(string, options = {}) | |
defaults = { | |
:times => 1, | |
:upcase => false, | |
:reverse => false | |
} | |
options = defaults.merge(options) | |
# make sure to clean up your debugging lines (i.e. this shouldn't be here) | |
p options | |
# These three things below would be better as one liners | |
# ex: string = string.upcase if options[:upcase] == true | |
if options[:upcase] == true | |
string = string.upcase | |
end | |
if options[:reverse] == true | |
string = string.reverse | |
end | |
# Interersting thought, but let it just return an error if they enter a negative number | |
# Because it has to print the thing once at least. | |
if options[:times] < 1 | |
options[:times] = 1 | |
end | |
options[:times].times do | |
puts string | |
end | |
end | |
end | |
end | |
def print_factors() | |
(1..100).each do |i| | |
puts "The factors for #{i} are:" | |
# This each loop below can be on one line | |
(1..i).each do |j| | |
puts j if i % j == 0 | |
end | |
end | |
end | |
# lowest to highest | |
# [1,2,3,5,3] | |
# >> [1,2,3,3,5] | |
def bubble_sort(some_array) | |
swapped = true | |
until !swapped | |
swapped = false | |
i = 0 | |
while i <= some_array.length - 2 | |
if some_array[i] > some_array[i+1] | |
# Do parallel assignment here | |
# some_array[i+1], some_array[i] = some_array[i], some_array[i+1] | |
temp = some_array[i+1] | |
some_array[i+1] = some_array[i] | |
some_array[i] = temp | |
swapped = true | |
end | |
i += 1 | |
end | |
end | |
# Generally think about the fact that you've now changed the handed in array in place | |
# instead of handing back a copy that's sorted (i.e. you've created a 'bang' method | |
# without any real indication that it messes with the initial array | |
return some_array | |
end | |
def get_substrings(string) | |
substrings = [] | |
i = 0 | |
while i < string.length | |
j = i | |
while j < string.length | |
substrings.push(string[i..j]) | |
j += 1 | |
end | |
i += 1 | |
end | |
return substrings | |
end | |
def check_for_words(substrings) | |
# Put dictionary into an array | |
dictionary_words = [] | |
File.foreach("2of12inf.txt") do |line| | |
# I don't quite understand the double chomp | |
dictionary_words << line.chomp.chomp('%') | |
end | |
# This is where the final answers go: | |
substrings_words = [] | |
substrings.each do |substring| | |
substrings_words.push(substring) if dictionary_words.include?(substring) | |
end | |
return substrings_words | |
end | |
def print_words_contained(string) | |
check_for_words(get_substrings(string)) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rps(option) | |
computer = ["Rock", "Paper", "Scissors"] | |
computers_answer = computer.shuffle[0] | |
if computers_answer == option | |
puts "#{computers_answer}, Draw" | |
else | |
case computers_answer | |
when "Rock" | |
if option == "Paper" | |
puts "#{computers_answer}, You Win" | |
else | |
puts "#{computers_answer}, You Lose. You're Awful at this!" | |
end | |
when "Paper" | |
if option == "Scissors" | |
puts "#{computers_answer}, You Win" | |
else | |
puts "#{computers_answer}, You Lose. You're Awful at this!" | |
end | |
when "Scissors" | |
if option == "Rock" | |
puts "#{computers_answer}, You Win" | |
else | |
puts "#{computers_answer}, You Lose. You're Awful at this!" | |
end | |
end | |
end | |
end | |
def shuffle_swingers(swingers) | |
men = [] | |
women = [] | |
result = [] | |
swingers.each do |couple| | |
men.push(couple[0]) | |
women.push(couple[1]) | |
end | |
new_couples(men, women) | |
end | |
def new_couples(men, women) | |
couples = [] | |
women = women.shuffle | |
men.each do |man| | |
couple = [] | |
couple[0] = man | |
couple[1] = women.pop | |
couples.push(couple) | |
end | |
couples | |
end | |
# To Test just uncomment below and load "methods.rb" and it will run! | |
''' | |
swingers = [ | |
["Clyde", "Bonnie"], | |
["Paris", "Helen"], | |
["Romeo", "Juliet"], | |
["Jack", "Jill"], | |
["John", "Lucy"] | |
] | |
puts "Original Swingers:" | |
p swingers | |
puts " " | |
p shuffle_swingers(swingers) | |
''' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student | |
attr_reader :first_name, :last_name, :courses_enrolled | |
def initialize(first_name, last_name) | |
@first_name = first_name | |
@last_name = last_name | |
@courses_enrolled = [] | |
end | |
def name | |
"#{@first_name} #{@last_name}" | |
end | |
# Could be much more concise | |
# @courses_enrolled.map(&:name) | |
def courses | |
course_list = [] | |
@courses_enrolled.each do |course| | |
course_list << course.course_name | |
end | |
course_list | |
end | |
# Use unless and get rid of the empty conditional branch | |
def enroll(course) | |
if @courses_enrolled.include?(course) | |
# don't re-add | |
else | |
@courses_enrolled << course | |
course.add_student(self.name) | |
end | |
end | |
# Can use Hash.new(0) to initialize so you don't have to check if the key exists | |
# Then just say department_credits[course.department] += course.credits | |
def course_load | |
department_credits = {} | |
@courses_enrolled.each do |course| | |
if department_credits.has_key?(course.department) | |
department_credits[course.department] += course.number_of_credits | |
else | |
department_credits[course.department] = course.number_of_credits | |
end | |
end | |
department_credits | |
end | |
end | |
class Course | |
attr_reader :course_name, :department, :number_of_credits, :student_list | |
def initialize(course_name, department, number_of_credits) | |
@course_name = course_name | |
@department = department | |
@number_of_credits = number_of_credits | |
@student_list = [] | |
end | |
# Same as above, just use unless | |
def add_student(student_name) | |
if @student_list.include?(student_name) | |
# don't re-add | |
else | |
@student_list << student_name | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def super_print(string, options = {}) | |
defaults = { | |
:times => 1, | |
:upcase => false, | |
:reverse => false | |
} | |
options = defaults.merge(options) | |
# Get rid of debugging lines like this | |
p options | |
# Use one line if statements | |
if options[:upcase] == true | |
string = string.upcase | |
end | |
if options[:reverse] == true | |
string = string.reverse | |
end | |
# Don't really need to account for negatives - let it raise an error | |
# Because it has to print the thing once at least. | |
if options[:times] < 1 | |
options[:times] = 1 | |
end | |
options[:times].times do | |
puts string | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# not complete | |
class Game | |
def initialize(players) | |
@number_of_human_players = players | |
@gameboard = {} | |
# Create the empty gameboard | |
(1..9).each { |square| @gameboard[square] = nil } | |
# Create the players | |
if @number_of_human_players == 1 | |
@player1 = HumanPlayer.new('X') | |
@player2 = ComputerPlayer.new('O') | |
else | |
@player1 = HumanPlayer.new('X') | |
@player2 = HumanPlayer.new('O') | |
end | |
end | |
def print_board | |
puts "1 (#{@gameboard[1]}) | 2 (#{@gameboard[2]}) | 3 (#{@gameboard[3]}) " | |
18.times { print "-" } | |
print "\n" | |
puts "4 (#{@gameboard[4]}) | 5 (#{@gameboard[5]}) | 6 (#{@gameboard[6]}) " | |
18.times { print "-" } | |
print "\n" | |
puts "7 (#{@gameboard[7]}) | 8 (#{@gameboard[8]}) | 9 (#{@gameboard[9]}) " | |
end | |
def play | |
turn = 1 | |
#while win? == false | |
#get_player_move | |
#make_move | |
#return only when game has completed / when someone wins, exit the loop | |
#is_game_complete | |
end | |
def make_move(player) | |
@gameboard[player.get_move] = player.name | |
end | |
def win? | |
# check for row or column wins | |
(1..3).each do |i| | |
if get_row(i).uniq.length == 1 return true | |
end | |
if get_column(i).uniq.length == 1 return true | |
end | |
end | |
# check for diagonal win | |
(1..2).each do {|i| return true if get_diagonal(i).uniq.length == 1} | |
end | |
# Methods to get rows and columns and diagonals and return arrays of the values in those rows | |
def get_row(row_int) | |
case row_int | |
when 1 | |
create_row(row_int) | |
when 2 | |
row_int = 4 | |
create_row(row_int) | |
when 3 | |
row_int = 7 | |
create_row(row_int) | |
end | |
end | |
def create_row(row_int) | |
row_values = [] | |
(row_int..row_int+2).each {|i| row_values << @gameboard[i]} | |
row_values | |
end | |
def get_column(column_int) | |
column_values = [] | |
range = column_int..column_int+6 | |
range.step(3) {|i| column_values << @gameboard[i]} | |
return column_values | |
end | |
def get_diagonal(diagonal_int) | |
diagonal_values = [] | |
if diagonal_int == 1 | |
diagonal_values << @gameboard[1] << @gameboard[5] << @gameboard[9] | |
else | |
diagonal_values << @gameboard[3] << @gameboard[5] << @gameboard[7] | |
end | |
return diagonal_values | |
end | |
end | |
class HumanPlayer | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def get_move | |
puts "Claim your square #{self.name}'s! (Enter number of square you would like to move to." | |
@move = gets.chomp | |
@move | |
end | |
end | |
''' | |
class ComputerPlayer | |
def initialize(name) | |
@name = name | |
end | |
def get_move() | |
@move = calculate_move() | |
end | |
def calculate_move | |
end | |
end | |
''' | |
def start_game | |
puts "How many people are playing? (1 or 2)" | |
players = gets.chomp | |
Game.new(players) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment