Skip to content
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(a, b)
combos = []
a.each do |a_item|
b.each do |b_item|
combos << a_item + b_item
end
end
combos
end

combinations(["on", "in"], ["to", "rope"])
24 changes: 24 additions & 0 deletions counting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def counting(player_num, count_limit)
count = 1
players = []
player_id = 1
direction = 1

while count <= count_limit
increment = 1
# if count is divisible by 7, switch the direction
if count % 7 == 0
direction *= -1
# if count is divisible by 11, skip the next person
end
if count % 11 == 0
increment = 2
end
puts "player#{player_id} says #{count}"
count += 1
player_id = (player_id + increment * direction) % player_num
end

end

counting(10, 100)
7 changes: 7 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def factorial(num)
tmp = 1
1.upto(num) do |i|
tmp = tmp * i
end
tmp
end
15 changes: 15 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# NOTE: this function assumes that the two coordinates provided for each rectangle are left-bottom and right-top, in that order.
def overlap(a, b)
# if x coordinates overlap
if (b[0][0] < a[1][0] && b[0][0] > a[0][0]) || (b[1][0] < a[1][0] && b[1][0] > a[0][0])
# check if y coordinates overlap
if (b[0][1] < a[1][1] && b[0][1] > a[0][1]) || (b[1][1] < a[1][1] && b[1][1] > a[0][1])
return true
end
end
return false

end

overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] )
overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] )
14 changes: 14 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Write a method power which takes two integers (base and exponent) and returns the base raised to the power of exponent. Do not use Ruby’s ** operator for this!

# > power(3,4)
# => 81 # (3*3*3*3)

def power(base, exponent)
val = 1
exponent.times do
val = val * base
end
val
end

puts power(3,4)
14 changes: 14 additions & 0 deletions primes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def is_prime?(num)
i = 1
factors = []
while i <= num
if num % i == 0
factors << i
end
i += 1
end
if factors.length > 2
return false
end
return true
end
12 changes: 12 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def uniques(array)
no_duplicates = []
array.each do |item|
unless no_duplicates.include? item
no_duplicates << item
end
end
no_duplicates

end

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