This file contains 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 product(x) | |
p = nil | |
x.each do |y| | |
if p.nil? | |
p = y | |
else | |
p = p * y | |
end | |
end | |
This file contains 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 Array | |
def pad!(min_size, value = nil) | |
x = min_size - self.count #take the argument and subtract the count from it. | |
x.times do | |
self << value | |
end# I want to add the value x number of times to the array | |
self | |
end | |
This file contains 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 factorial(n) | |
result = 1 # you need somewhere to store the result, and since this is multiplication, you can't start at 0. | |
#THIS HAS TO BE OUTSIDE THE LOOP. Otherwise everytime it goes to the next i element result will go back to being 1. | |
n.downto(1) do |i| | |
result = result * i # here result and i are multiplied first, then the result is assigned to the result variable. Let me know if this is confusing. | |
end | |
# now, we've looped through all of the numbers and multiplied them, but the loop does not return anything. | |
return result # this will return the result that we ended up with after looping or iterating through. | |
end |
This file contains 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 reverse_words(str) | |
array = str.split(' ') | |
array.each do |x| | |
x.reverse! | |
end | |
array.join(' ') | |
end |
This file contains 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
# smallest_integer is a method that takes an array of integers as its input | |
# and returns the smallest integer in the array | |
# | |
# +array+ is an array of integers | |
# smallest_integer(array) should return the smallest integer in +array+ | |
# | |
# If +array+ is empty the method should return nil | |
def smallest_integer(array) | |
min = nil | |
array.each do |x| |
This file contains 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
#1 first attempt | |
def count_between(array, lower_bound, upper_bound) | |
count = 0 | |
array.each do |x| | |
count += 1 if x >= lower_bound && x <= upper_bound | |
end | |
count | |
end |
This file contains 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
# longest_string is a method that takes an array of strings as its input | |
# and returns the longest string | |
# | |
# +array+ is an array of strings | |
# longest_string(array) should return the longest string in +array+ | |
# | |
# If +array+ is empty the method should return nil | |
def longest_string(array) | |
longest = nil | |
array.each do |x| |
This file contains 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
# shortest_string is a method that takes an array of strings as its input | |
# and returns the shortest string | |
# | |
# +array+ is an array of strings | |
# shortest_string(array) should return the shortest string in +array+ | |
# | |
# If +array+ is empty the method should return nil | |
def shortest_string(array) | |
shortest = nil | |
array.each do |x| |
This file contains 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
# largest_integer is a method that takes an array of integers as its input | |
# and returns the largest integer in the array | |
# | |
# +array+ is an array of integers | |
# largest_integer(array) should return the largest integer in +array+ | |
# | |
# If +array+ is empty the method should return nil | |
#using sort will go through the array a bunch of times, which is less efficient | |
def largest_integer(array) |
This file contains 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 mode(array) | |
count = Hash.new(0) | |
array.each { |element| count[element] += 1 } #count how many times an element[s] appears the most | |
# element of the array => number of times element occured in an array | |
# ex. {5 => 2, 6 =>2, 7 =>1} | |
max = count.values.max | |
count.keep_if { |key, val| val == max} | |
# count is {5 => 2, 6 =>2} | |
count.keys #return an array of those elements | |
end |