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
numbers = (1..10).to_a | |
# With Each and Push | |
new_numbers = [] | |
numbers.each do |number| | |
number = number ** 2 | |
new_numbers << number | |
end | |
# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
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 palindrome(string) | |
data = string.gsub(/[^A-Za-z]/, "").split("") | |
data == data.reverse | |
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 common_substring(string1, string2) | |
(string1.split("") & string2.split("")).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
def caesar_cipher(string) | |
data = string.split("") | |
new = [] | |
data.each do |letter| | |
if letter.ord > 122 or letter.ord < 65 | |
new << letter | |
elsif letter == letter.upcase | |
transform = (letter.ord + 13) | |
transform -= 26 if transform > 90 | |
new << transform.chr |
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
# With help from: http://math.stackexchange.com/questions/389675/largest-prime-factor-of-600851475143 | |
def largest_prime_factor(number) | |
divisor = 2 | |
largest_divisor = 0 | |
until number == 1 | |
if number % divisor == 0 | |
number /= divisor | |
largest_divisor = divisor | |
else |
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 recursive_fib(num) | |
if num <= 1 | |
return num | |
else | |
answer = recursive_fib(num - 2) + recursive_fib(num - 1) | |
end | |
return answer.abs | |
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) | |
if n == 0 | |
return 1 | |
else | |
i = n - 1 | |
while i > 0 | |
n *= i | |
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 remove_duplicates(arr) | |
new = [] | |
arr.each do |i| | |
if ! new.include?(i) | |
new << i | |
end | |
end | |
return new | |
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(string) | |
arr = string.split("") | |
n = arr.length | |
index = -1 | |
new = [] | |
until index == (-n - 1) | |
new << arr[index] | |
index -= 1 | |
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 Car | |
attr_accessor :doors, :type, :name | |
def initialize(doors, type, name) | |
@doors = doors | |
@type = type | |
@name = name | |
end | |
def forward |