diff --git a/Combinations.rb b/Combinations.rb new file mode 100644 index 0000000..518c092 --- /dev/null +++ b/Combinations.rb @@ -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"]) \ No newline at end of file diff --git a/Counting_game.rb b/Counting_game.rb new file mode 100644 index 0000000..96b671f --- /dev/null +++ b/Counting_game.rb @@ -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) \ No newline at end of file diff --git a/Primes.rb b/Primes.rb new file mode 100644 index 0000000..98b4ac1 --- /dev/null +++ b/Primes.rb @@ -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) \ No newline at end of file diff --git a/Rectangle_overlap.rb b/Rectangle_overlap.rb new file mode 100644 index 0000000..42b44c9 --- /dev/null +++ b/Rectangle_overlap.rb @@ -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] ] ) diff --git a/Uniques.rb b/Uniques.rb new file mode 100644 index 0000000..8f71141 --- /dev/null +++ b/Uniques.rb @@ -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"]) \ No newline at end of file diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..f4da6aa --- /dev/null +++ b/factorial.rb @@ -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) \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..34762c3 --- /dev/null +++ b/power.rb @@ -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) \ No newline at end of file