diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..ba3c2f7 --- /dev/null +++ b/combinations.rb @@ -0,0 +1,13 @@ +array1 = (["on", "in"]) +array2 = (["to", "rope"]) + +def combinations array1, array2 + result = [] + array1.each do |a1| + array2.each do |a2| + result << (a1 + a2) + end + end + result +end +p combinations(array1, array2) \ No newline at end of file diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..7d6115d --- /dev/null +++ b/factorial.rb @@ -0,0 +1,9 @@ +def factorial(variable) + product = 1 + variable.times do + |n| product *= (n + 1) + end + product +end + +puts factorial(5) \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..b40f2bd --- /dev/null +++ b/overlap.rb @@ -0,0 +1,12 @@ +Rect_A = [(0,0),(3,3)] = [(A.x1,A.y1),(A.x2,A.y2)] +Rect_B = [(1,1),(4,5)] = [(B.x1,B.y1),(B.x2,B.y2)] + +def overlap(Rect_A, Rect_B) + if(A.x2 < B.x1 || A.x1 > B.x2) + return false + if (A.y2 > B.y1 || A.y1 < B.y2) + return false + end + return true +end +p overlap \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..dbf118d --- /dev/null +++ b/power.rb @@ -0,0 +1,12 @@ +def power(base, exponent) + total = 1 + counter = 1 + + while counter <= exponent + total = total * base + counter += 1 + end + total +end + +puts power(3,4) \ No newline at end of file diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..ca9c452 --- /dev/null +++ b/primes.rb @@ -0,0 +1,15 @@ + +def is_prime?(number) + number.times do |x| + if x > 1 && number % x == 0 + return false + end + end + return true +end + +p is_prime?(7) +p is_prime?(14) + + + diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..3205467 --- /dev/null +++ b/uniques.rb @@ -0,0 +1,10 @@ +array = [1,5,"frog",2,1,3,"frog"] + +def uniques(array) + singulars = Array.new + array.each do |element| + singulars << element unless singulars.include?(element) + end + singulars +end +p uniques(array) \ No newline at end of file