Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep Ruby Challenges - Completed #112

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def combinations(arr1, arr2)

# return if any array is empty
if arr1.length == 0 or arr2.length == 0
return arr1.length == 0 ? (arr2) : (arr1)
end

# initialize
combined_array = []

# combine arrays
arr1.each do |val1|
arr2.each do |val2|
combined_array << val1 + val2
end
end

return combined_array

end

# tests
puts combinations(["on","in"],["to","rope"])
puts "---------------"
puts combinations(["a "], ["burrito", "coffee", "computer", "mouse", "rhino"])
puts "---------------"
puts combinations(["a", "b", "c", "d", "e"], ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
puts "---------------"
puts combinations([],["1","a"])
puts "---------------"
puts combinations([],[])
36 changes: 36 additions & 0 deletions counting_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def counting_game(num_friends, count)

friend = 1
number = 1
direction = 1 # 1 or -1 depending on direction of friends counting

while number <= count

puts "Friend " + friend.to_s + " said " + number.to_s # statement

if number % 11 == 0 # important that this precedes friend change & number change
friend += direction
end

friend += direction # friend change

if friend > num_friends # back to first friend
friend -= num_friends
end
if friend <= 0 # back to last friend
friend += num_friends
end

number += 1 # number change

if number % 7 == 0 # important that this comes after friend change & number change
direction *= -1
end

end

end

# tests
counting_game(10, 100)
# counting_game(6,200)
26 changes: 26 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def factorial(num)

if num < 0
return "out of scope for this method"
elsif num < 2
return 1
else
return num*factorial(num-1) # recursion
end

end

# tests
puts factorial(5)
puts "---------------"
puts factorial(0)
puts "---------------"
puts factorial(1)
puts "---------------"
puts factorial(2)
puts "---------------"
puts factorial(9)
puts "---------------"
puts factorial(12)
puts "---------------"
puts factorial(-1)
31 changes: 31 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def power(base,exp)

# immediately return if exp = 0
if exp == 0
return 1
end

# initialize
result = 1

# iterate to find result
exp.abs.times do
result *= base
end

# return based on whether exp is pos or neg
return exp < 0 ? (1.0/result) : (result)

end

# tests
puts power(1,3)
puts "---------------"
puts power(8,10)
puts "---------------"
puts power(2,-2)
puts "---------------"
puts power(10,-5)
puts "---------------"
puts power(1000000000000000000000000,0)

32 changes: 32 additions & 0 deletions prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def is_prime?(num)

# any number less than or equal to 1 is not prime
if num <= 1
return false
end

(2..num/2).to_a.each do |i|
if num % i == 0
return false
end
end

return true

end

# tests
puts is_prime?(7)
puts "---------------"
puts is_prime?(14)
puts "---------------"
puts is_prime?(-1)
puts "---------------"
puts is_prime?(0)
puts "---------------"
puts is_prime?(1)
puts "---------------"
puts is_prime?(2)
puts "---------------"
puts is_prime?(5)

47 changes: 47 additions & 0 deletions rect_overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

def overlap(rect1, rect2)

# assumptions
# the bottom-left and top-right corners are always given, rather than the bottom-right and top-left
# the coordinates for each rectangle are given as [bottom-left, top-right] or [top-right, bottom-left]
# the first rectangle given isn't always the most bottom and/or most left

# define corners of each rectange (bl = bottom-left, tr = top-right)
rect1_bl = rect1[0][0] < rect1[1][0] ? rect1[0] : rect1[1]
rect1_tr = rect1[0][0] > rect1[1][0] ? rect1[0] : rect1[1]
rect2_bl = rect2[0][0] < rect2[1][0] ? rect2[0] : rect2[1]
rect2_tr = rect2[0][0] > rect2[1][0] ? rect2[0] : rect2[1]

# make sure that rect1 is the most "bottom-left" & (crudely) swap if that is not the case
if rect1_bl[0] > rect2_bl[0] or rect1_bl[1] > rect2_bl[1]
temp = rect1_bl
rect1_bl = rect2_bl
rect2_bl = temp
temp = rect1_tr
rect1_tr = rect2_tr
rect2_tr = temp
end

# print rectangles to check if swap (if necessary) worked
# puts '[' + rect1_bl[0].to_s + ',' + rect1_bl[1].to_s + '], [' + rect1_tr[0].to_s + ',' + rect1_tr[1].to_s + ']'
# puts '[' + rect2_bl[0].to_s + ',' + rect2_bl[1].to_s + '], [' + rect2_tr[0].to_s + ',' + rect2_tr[1].to_s + ']'

# check if rects overlap by comparing rect1_tr and rect2_bl
return rect2_bl[0] < rect1_tr[0] && rect2_bl[1] < rect1_tr[1]

end

puts overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) # scenario 1, true
puts "---------------"
puts overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) # scenario 2, false
puts "---------------"
puts overlap( [ [3,3],[0,0] ], [ [1,1],[4,5] ] ) # scenario 1, coords of rect1 swapped, true
puts "---------------"
puts overlap( [ [0,0],[1,4] ], [ [3,2],[1,1] ] ) # scenario 2, coords of rect2 swapped, false
puts "---------------"
puts overlap( [ [1,1],[4,5] ], [ [0,0],[3,3] ] ) # scenario 1, rect1 & rect2 swapped, true
puts "---------------"
puts overlap( [ [0,4],[5,5] ], [ [0,0],[4,4] ] ) # scenario 3, edge case, false



28 changes: 28 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def uniques(arr)

# initialize
no_dupes = []

# remove duplicates by iteration and checking preceding elements
arr.each_with_index do |val_outer, ind|
has_preceding_dupe = false
arr[0...ind].each do |val_inner| # hinges on use of "..." (last val exclusive) NOT ".." (last val inclusive)
if val_outer == val_inner
has_preceding_dupe = true
end
end
if not has_preceding_dupe
no_dupes << val_outer
end
end

return no_dupes

end

# tests
puts uniques([1,5,"frog", 2,1,3,"frog"])
puts "---------------"
puts uniques([1,2,3,2,3,5,7,1,4,20,6,1,2])
puts "---------------"
puts uniques(["a", "A", "eh", "a", "eyy", "eyyy", "eyy"])