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
12 changes: 12 additions & 0 deletions Combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def combinations (first, second)
res = []
first.each do |f|
second.each do |s|
res << f + s
end
end
return res
end

#test
#puts combinations(["on","in"],["to","rope"])
27 changes: 27 additions & 0 deletions Counting_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def next_friend(friend, count_of_friends, is_forward)
if is_forward
friend += 1
friend = 1 if friend > count_of_friends
else
friend -= 1
friend = count_of_friends if friend == 0
end
return friend
end

def count_game_begin(count_of_friends, max_count)
friend = 0
is_forward = true

1.upto(max_count) do |n|
friend = next_friend(friend, count_of_friends, is_forward)

puts "#{friend} says #{n}"
is_forward = !is_forward if n%7 == 0

friend = next_friend(friend, count_of_friends, is_forward) if n%11 == 0
end
end

#test
#count_game_begin(10, 100)
12 changes: 12 additions & 0 deletions Primes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def is_prime?(num)
(num/2).downto(2) do |n|
return false if num%n == 0
end
return true
end

#test
#puts is_prime?(7)

#test
#puts is_prime?(14)
131 changes: 131 additions & 0 deletions Rectangle_overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

class Coord
attr_accessor :X
attr_accessor :Y
end

class Rectangle
attr_accessor :A
attr_accessor :B
def initialize(rec)
@A = Coord.new
@B = Coord.new
@A.X = rec[0][0]
@A.Y = rec[0][1]
@B.X = rec[1][0]
@B.Y = rec[1][1]
end
end

def order_coord_of_rectangles(rec)
# ------------- B --------------- A A---------------- B ---------------
# | | | | | | | |
# |correct | | | | | | |
# A ------------ B --------------- ----------------- B --------------- A
if rec.B.X < rec.A.X
temp = Coord.new
temp = rec.A
rec.A = rec.B
rec.B = temp
end

if rec.A.Y > rec.B.Y
diff = rec.A.Y - rec.B.Y
rec.A.Y -= diff
rec.B.Y += diff
end

return rec
end

# order the rectangles as per assumptions
def order_rectangles(rec1, rec2)
r1 = Rectangle.new(rec1)
r2 = Rectangle.new(rec2)
r1 = order_coord_of_rectangles(r1)
r2 = order_coord_of_rectangles(r2)

if r2.A.X < r1.A.X
temp = r1
r1 = r2
r2 = temp
end

ret = [r1, r2]
return ret
end

def overlap?(rec1, rec2)
rects = order_rectangles(rec1, rec2)
r1 = rects[0]
r2 = rects[1]
# -------------
# | |
# | |
# -------------
# -----------------
# | |
# | |
# -----------------
#------------------------------------
# -------------
# | |
# | --------|--------
# | | | |
# ----|-------- |
# | |
# -----------------
#------------------------------------
# ---------------------
# | | | |
# | | | |
# ---------------------
#------------------------------------
# ---------
# | ----|----
# | | | |
# | ----|----
# ---------
#------------------------------------
# -------------
# ----|---- |
# | | | |
# ----|---- |
# -------------
#------------------------------------
# ---------
# | |
# ----|---- |
# | | | |
# | ----|----
# | |
# ---------
#------------------------------------
# ---------
# | |
# | |
# ---------
# ----------
# | |
# | |
# ----------

if r2.A.X < r1.B.X
if r1.A.Y > r2.B.Y
return false
elsif r2.A.Y > r1.B.Y
return false
else
return true
end
else
return false
end
end


#test assumptions: first rectangle along x-axis first and first co-ordinate along axes first for each rectangle, both co-ordinates on diagonally opposite corners
#test: overlap is true
#puts overlap?( [ [0,0],[3,3] ], [ [1,1],[4,5] ] )
#test: overlap is false
#puts overlap?( [ [0,0],[1,4] ], [ [1,1],[3,2] ] )
13 changes: 13 additions & 0 deletions Uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def uniques(uniq)
uniq.each_with_index do |v, i|
for j in (i+1).upto(uniq.length - 1)
if v == uniq[j]
uniq.delete_at(j)
end
end
end
return uniq
end

#test
#puts uniques([1,5,"frog", 2,1,3,"frog"])
10 changes: 10 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def factorial(n)
res = 1
(2).upto(n) do |val|
res *= val
end
return res
end

#test: positive integer
#puts factorial(5)
10 changes: 10 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def power(base, exp)
res = 1
exp.times do
res *= base
end
return res
end

#test : positive integers
#puts power(2, 4)