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 = 0 | |
array.length.times do |i| |
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 longest_string(array) | |
longest = 0 | |
array.length.times do |i| | |
if i == 0 | |
longest = array[i] | |
elsif | |
longest.length < array[i].length | |
longest = array[i] | |
else | |
longest |
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) | |
results = 1 | |
n.times do |i| | |
results = n * results | |
n = n - 1 | |
end | |
results | |
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 times_table(rows) | |
# number of columns determined by the multiplier | |
counter = 1 | |
results = "" | |
multiples = 1 | |
rows.times do | |
rows.times do | |
if rows >= counter |
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(array) | |
results = array.split(" ") | |
new_results = [] | |
results.length.times do |i| | |
new_results[i] = results[i].reverse | |
end | |
return new_results.join(" ") |
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 separate_comma(number) | |
results = number.to_s.split("") | |
reverse_counter = results.length - 1 | |
output = "" | |
comma_counter = 0 | |
results.length.times do |i| | |
output = results[reverse_counter] + output | |
reverse_counter = reverse_counter - 1 | |
comma_counter = comma_counter + 1 | |
if comma_counter%3 == 0 && comma_counter/number.to_s.length != 1 |
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 is_fibonacci?(i) | |
array = [0,1] | |
counter = 0 | |
while array.max < i | |
array.push(array[counter]+array[counter+1]) | |
counter = counter + 1 | |
end | |
array.include?(i) | |
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 print_triangle(rows) | |
rows.times do |i| | |
puts "*"*(i+1) | |
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
def median(array) | |
mid_point = 0 | |
lower_mid_point = 0 | |
upper_mid_point = 0 | |
results = 0 | |
if array.length % 2 == 1 | |
mid_point = (array.length + 1 ) / 2 -1 | |
puts mid_point | |
results = array[mid_point] |
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) | |
hash = {} | |
maxnum = 0 | |
resultsarray = [] | |
array.each do |i| | |
if hash[i] | |
hash[i] += 1 |
OlderNewer