Skip to content

Ruby Challenges from Markup & Coding Prep Course #120

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

Open
wants to merge 7 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
11 changes: 11 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def combinations(arr1, arr2)
new_array = []
arr1.each{ |x|
arr2.each{ |y|
new_array.push(x+y)
}
}
return new_array
end

combinations(["on","in"],["to","rope"])
25 changes: 25 additions & 0 deletions countinggame.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def counting_game(total_players, high_count)
clockwise = true
person = 0

high_count.times do |count|
puts "Player " + (person+1).to_s + " says " + (count+1).to_s

if (count + 1) % 7 == 0
clockwise = !clockwise
end

if (count + 1) % 11 == 0
jump = 2
else jump = 1
end

if clockwise
person = (person + jump) % total_players
else person = (person - jump) % total_players
end

end
end

counting_game(10, 100)
14 changes: 14 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=begin Write a method factorial which takes a number and returns the product of every number up to the current number multiplied together.
=end

def factorial(num)
count = 1
product = 1
num.times {
product *= count
count += 1
}
return product
end

factorial(6)
11 changes: 11 additions & 0 deletions is_prime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def is_prime?(num)
(2...num).each do |divisor|
if num % divisor == 0
return false
end
end
return true
end

is_prime?(7)
is_prime?(14)
12 changes: 12 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# works on given examples but doesn't work on all inputs
# maybe you can create an array with all corners and compare that way

def overlap(rect1, rect2)
if rect1[1][0] > rect2[0][0] && rect1[1][1] > rect2[0][0]
return true
else
return false
end
end

overlap( [ [1,7],[5,4] ], [ [4,5],[7,1] ] )
7 changes: 7 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def power (base, exponent)
product = 1
exponent.times{ product *= base }
return product
end

power (5,2)
12 changes: 12 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def uniques(arr)
unique_array = []
arr.each do |x|
if unique_array.include?(x) == false
unique_array.push(x)
end
end
return unique_array
end


uniques([1,5,"frog", 2,1,3,"frog"])