Skip to content

Instantly share code, notes, and snippets.

class Crossword
attr_accessor :line1, :line2, :line3, :line4, :line5, :line6
def initialize(layout)
layout_array = layout.split("\n")
@line1 = layout_array[0]
@line2 = layout_array[1]
@line3 = layout_array[2]
@line4 = layout_array[3]
@line5 = layout_array[4]
@kmandreza
kmandreza / Sudoku_solver
Last active December 12, 2015 03:38
Teacher's solution for Sudoku_solver
=begin
#PSEUDOCODE FOR PHASE 2
solve!
#STEP 1...next step is either step 2 or 3
return false if board is valid
valid? => checks for duplicates
if attempt causes some repetition of number
#STEP 2...
@kmandreza
kmandreza / The Power of Enumerable
Created January 30, 2013 00:47
When you need to operate on a Ruby Array, you can accomplish just about anything you can imagine with the each method. Often, though, using each is not the best way to work with Arrays. For example, when you find yourself adding conditional logic inside your each block, or using each to search, sort, or transform the Array, consider the power of…
# Print the 1st, 3rd, 5th, 7th, etc. elements of a list on separate lines.
def print_odd_indexed_integers(array)
array.each_with_index do |item, index|
if index.even?
#should be .odd? in the spec
puts item
end
end
end
@kmandreza
kmandreza / Build a simple guessing game
Created January 20, 2013 18:35
Create 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 the guess is lower than the answer. D…
class GuessingGame
def initialize(answer)
@answer = answer
end
def guess(guess)
@guess = guess
if guess > @answer
return :high
elsif guess == @answer
@kmandreza
kmandreza / Pseudocode
Created January 12, 2013 04:28
Using pseudocode, write a simple hangman game. The point of pseudocode is to represent the logical structureof a program without getting bogged down in the syntax of a particular language. For example, a program to select the largest integer from an array of integers might be written like this in pseudocode initialize max_so_far to the first ele…
# generate a word at random and store it in a variable
# display the length of the word to the user
# correct_guesses is less than the length of the word
# prompt the user to guess a letter
# if the guess is correct increment correct_guesses by 1
# if the guess is incorrect increment incorrect_guesses by 1
# and draw the next part of the hangman
# if the incorrect_guesses is greater than 8, tell the user
# they lost and exit the program
# if correct_guesses is equal to the length of the word, tell the user they won
@kmandreza
kmandreza / Refactor a method
Created January 12, 2013 04:27
Refactor a method
def method_4(array)
results = []
array.each do |num|
if num % 2 == 0
results << num
end
end
return results
@kmandreza
kmandreza / fizzblam
Created January 12, 2013 04:24
Write a method called fizzblam that prints the numbers from 1 to 1000. But for multiples of 5 print “Fizz” instead of the number and for the multiples of 7 print “Blam”. For numbers which are multiples of both 5 and 7 print “FizzBlam.”
def fizzblam
1000.times do |n|
if (n+1) %7 == 0 && (n+1) %5 == 0
puts "FizzBlam"
elsif (n+1) %5 == 0
puts "Fizz"
elsif (n+1) %7 == 0
puts "Blam"
else
puts n + 1
@kmandreza
kmandreza / returns the product of all the odd integers in an array
Created January 12, 2013 04:21
Write a method called product_odd which takes as its input an array of integers and returns the product of all the odd integers in the array. Remember that technically 0 is an even integer. For example: product_odd([1,2,3]) # returns 3, because 2 is even product_odd([0,-1,-10]) # returns -1, because 0 and -10 are even product_odd([1,2,3,4,5]) # …
def product_odd(x)
odd = x.select {|y| y%2 == 1}
p = 1
odd.each do |y|
p = p * y
end
p
end
@kmandreza
kmandreza / return the product of an array of integers
Created January 12, 2013 04:20
Write a method called product which takes as its input an array of integers and returns their product. For example product([1,2,3]) # returns 6 product([0,-1,-10]) # returns 0 product([1,-1,-10]) # returns -11 If you need to iterate over the array, please use Array#each. Don’t use, e.g., inject.
def product(x)
p = 1
x.each do |y|
p = p * y
end
p
end
@kmandreza
kmandreza / concatenate strings
Created January 12, 2013 04:19
Create a method new_cat which takes as its input two strings. It should concatenate the strings and return the concatenated value, without modifying either of the input strings. For example, # One does not simply walk into... new_cat("Mor", "dor") # => "Mordor" Call this method and send your first and last name as arguments.
def new_cat(str1, str2)
str1 + str2
end
puts new_cat("Mor","dor")