Skip to content

Instantly share code, notes, and snippets.

@kmandreza
kmandreza / Write a method called product
Created January 5, 2013 22:23
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 = nil
x.each do |y|
if p.nil?
p = y
else
p = p * y
end
end
@kmandreza
kmandreza / Create a method to pad an array
Created January 4, 2013 03:06
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil] And ruby [1…
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
@kmandreza
kmandreza / Implement the factorial function
Created January 4, 2013 03:05
Write a factorial method which takes as its input a non-negative integer and calculates the factorial of that number. The factorial of a number is the product of all integers from 1 up to that number. For example: factorial(5) == 5 * 4 * 3 * 2 * 1 == 120 The factorial of 0 is defined to be 1. See the Wikipedia article on the factorial for more i…
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
@kmandreza
kmandreza / Reverse words
Created January 3, 2013 10:01
Write a method reverse_words which takes a sentence as a string and reverse each word in it.
def reverse_words(str)
array = str.split(' ')
array.each do |x|
x.reverse!
end
array.join(' ')
end
@kmandreza
kmandreza / Find the smallest integer in an array
Created January 3, 2013 08:12
Write a method smallest_integer which takes as its input an Array of integers and returns the smallest integer in the Array. For example: smallest_integer([1, 2, 3]) # => 1 smallest_integer([0, -10, 10]) # => -10 smallest_integer([-10, -20, -30]) # => -30 If the input Array is empty smallest_integer should return nil.
# 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|
@kmandreza
kmandreza / Count the numbers in an array between a given range
Created January 3, 2013 07:55
Write a method count_between which takes three arguments as input: An Array of integers An integer lower bound An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array is empty. Some examples: count_between([1,2,3], 0, 100) # => 3 count_bet…
#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
@kmandreza
kmandreza / Find the longest string in an array
Created January 3, 2013 06:37
Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array. For example: # 'zzzzzzz' is 7 characters long longest_string(['cat', 'zzzzzzz', 'apples']) # => "zzzzzzz" If the input Array is empty longest_string should return nil.
# 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|
@kmandreza
kmandreza / Find the shortest string in an array
Created January 3, 2013 06:36
Write a method shortest_string which takes as its input an Array of Strings and returns the shortest String in the Array. For example: # 'cat' is 3 characters long shortest_string(['cat', 'zzzzzzz', 'apples']) # => "cat" # '' is 0 characters long, but it's the only string shortest_string(['']) # => '' shortest_string([]) # => nil If the input Ar…
# 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|
@kmandreza
kmandreza / Find the largest integer in an array
Created January 3, 2013 04:33
Write a method largest_integer which takes as its input an Array of integers and returns the largest integer in the Array. For example: largest_integer([-10, 0, 10]) # => 10 largest_integer([-10, -20, -30]) # => -10
# 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)
@kmandreza
kmandreza / Calculating the array mode
Created January 2, 2013 03:30
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array. For example, mode([1,2,3,3]) # => [3] mode([4.5, 0, 0]) # => [0] mode([1.5, -1, 1, 1.5]) # => [1.5] mode([1,1,2,2]) # => [1,2] mode([1,2,3]) # => [1,2,3], b…
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