Skip to content

Instantly share code, notes, and snippets.

# Create a 9x9 Array
string = ['E', 'B', 'D', 'R', 'T', 'U', 'P', 'W', 'F', 'Q', 'I', 'N', 'S', 'V', 'Z', 'D', 'R', 'T', 'U', 'P', 'W', 'D', 'R', 'T']
new_array = Array.new(4) {string.shift(6)}
# => [["E", "B", "D", "R", "T", "U"], ["P", "W", "F", "Q", "I", "N"], ["S", "V", "Z", "D", "R", "T"], ["U", "P", "W", "D", "R", "T"]]
puts
# Print nicely
new_array.map{|letter| letter.join(" ")}.join "\n"
# => E B D R T U
# P W F Q I N
class Baker
end
class Oven
end
class Cookies
$local_var1 = "A String" # This is defined in the global scope. The $ tells us that the variable is accessible globally
def get_local_var1
$local_var1 # Without the $ this varible is undefined and we get and error.
# local_var1 = 2 will override the global variable and return the 2.
end
puts get_local_var1 # the string is returned
# PSEUDOCODE
# Base case - Return solved grid if no num in grid less than 9 times
# CREATE the board and numbers
# Find the most common number
# Loop through the boxes starting from the top left, searching for num
# IF block has num
# go to next box
# ELSIF box does not have num
# Look to respective columns and rows to see if num is there
# ELSE where rows and columns do not contain num, add num to that respective square
def mean(numbers)
sum = numbers.inject(:+)
return sum / numbers.length
end
# Added brackets and created this as an array to make work
sample_avg = mean([5, 3, 6, 10])
def mean(*numbers)
sum = numbers.inject(:+)
return sum / numbers.length
end
# This will throw an error. Change this line so that it works.
sample_avg = mean(5, 3, 6, 10)