Skip to content

Instantly share code, notes, and snippets.

@kennyt
Created January 9, 2013 06:26
Show Gist options
  • Save kennyt/4491112 to your computer and use it in GitHub Desktop.
Save kennyt/4491112 to your computer and use it in GitHub Desktop.
w1d2 review
## General Comments
## You guys didn't comment enough early on in the day, I felt kind of lost on some methods.
## Later on you guys turned on beast mode though, nice commenting.
##
##
##Below I took out pieces of code on which I commented on, instead of uploading all files
def rps(human_throw)
human_formatted = human_throw.downcase
computer_stored = computer_throw
throw_result = "Human throws #{human_formatted}, computer throws #{computer_stored}: "
if !@possible_throws.include?(human_formatted) ### Use unless instead
puts "Invalid input."
return
end
#make a helper method to do all of below
if human_formatted == computer_stored
puts throw_result + "Tie game."
elsif human_formatted == "rock" && computer_stored == "scissors"
puts throw_result + "Human wins."
elsif human_formatted == "paper" && computer_stored == "rock"
puts throw_result + "Human wins."
elsif human_formatted == "scissors" && computer_stored == "paper"
puts throw_result + "Human wins."
else
puts throw_result + "Computer wins."
end
end
--------------------------------------------------------------------------------------
class UserRPN
def initialize #I believe you don't need initialize if you don't use it. I can be wrong.
end
def print_intro
puts "RPN PARTY!"
puts "Enter values or operators. When you're done, type 'END'"
end
def run
print_intro
if ARGV.empty?
input = get_user_input
else
input = File.read(ARGV.first).split
input << "END"
end
puts calculate(input)
end
def get_user_input
input_array = []
input = ""
until input == "END"
print "> "
input = gets.chomp.upcase
input_array << input unless input == "END"
end
input_array
end
def calculate(arr)
RPNCalculator.evaluate(arr.join(" "))
end
end
------------------------------------------------------------------------------------
def swingers(couples)
randomized_couples = couples.shuffle
randomized_couples.each_with_index do |couple, index|
if index != randomized_couples.length - 1 #Use unless index = etc. etc.
randomized_couples[index][1], randomized_couples[index+1][1] = randomized_couples[index+1][1], randomized_couples[index][1]
end
end
end
--------------------------------------------------------------------------------------
def factors
numbers = (1..100)
computed_factors = {}
numbers.each do |number|
found_factors = []
(1..number).each do |number_in_range|
if number % number_in_range == 0
found_factors << number_in_range
end
computed_factors[number] = found_factors
end
end
computed_factors # indent this and put it next to the last end
#here
end
--------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment