diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..52a5a44 --- /dev/null +++ b/combinations.rb @@ -0,0 +1,31 @@ +def combinations(arr1, arr2) + + # return if any array is empty + if arr1.length == 0 or arr2.length == 0 + return arr1.length == 0 ? (arr2) : (arr1) + end + + # initialize + combined_array = [] + + # combine arrays + arr1.each do |val1| + arr2.each do |val2| + combined_array << val1 + val2 + end + end + + return combined_array + +end + +# tests +puts combinations(["on","in"],["to","rope"]) +puts "---------------" +puts combinations(["a "], ["burrito", "coffee", "computer", "mouse", "rhino"]) +puts "---------------" +puts combinations(["a", "b", "c", "d", "e"], ["1", "2", "3", "4", "5", "6", "7", "8", "9"]) +puts "---------------" +puts combinations([],["1","a"]) +puts "---------------" +puts combinations([],[]) \ No newline at end of file diff --git a/counting_game.rb b/counting_game.rb new file mode 100644 index 0000000..dbbf971 --- /dev/null +++ b/counting_game.rb @@ -0,0 +1,36 @@ +def counting_game(num_friends, count) + + friend = 1 + number = 1 + direction = 1 # 1 or -1 depending on direction of friends counting + + while number <= count + + puts "Friend " + friend.to_s + " said " + number.to_s # statement + + if number % 11 == 0 # important that this precedes friend change & number change + friend += direction + end + + friend += direction # friend change + + if friend > num_friends # back to first friend + friend -= num_friends + end + if friend <= 0 # back to last friend + friend += num_friends + end + + number += 1 # number change + + if number % 7 == 0 # important that this comes after friend change & number change + direction *= -1 + end + + end + +end + +# tests +counting_game(10, 100) +# counting_game(6,200) diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..b036f2c --- /dev/null +++ b/factorial.rb @@ -0,0 +1,26 @@ +def factorial(num) + + if num < 0 + return "out of scope for this method" + elsif num < 2 + return 1 + else + return num*factorial(num-1) # recursion + end + +end + +# tests +puts factorial(5) +puts "---------------" +puts factorial(0) +puts "---------------" +puts factorial(1) +puts "---------------" +puts factorial(2) +puts "---------------" +puts factorial(9) +puts "---------------" +puts factorial(12) +puts "---------------" +puts factorial(-1) \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..a7471b3 --- /dev/null +++ b/power.rb @@ -0,0 +1,31 @@ +def power(base,exp) + + # immediately return if exp = 0 + if exp == 0 + return 1 + end + + # initialize + result = 1 + + # iterate to find result + exp.abs.times do + result *= base + end + + # return based on whether exp is pos or neg + return exp < 0 ? (1.0/result) : (result) + +end + +# tests +puts power(1,3) +puts "---------------" +puts power(8,10) +puts "---------------" +puts power(2,-2) +puts "---------------" +puts power(10,-5) +puts "---------------" +puts power(1000000000000000000000000,0) + diff --git a/prime.rb b/prime.rb new file mode 100644 index 0000000..7d85989 --- /dev/null +++ b/prime.rb @@ -0,0 +1,32 @@ +def is_prime?(num) + + # any number less than or equal to 1 is not prime + if num <= 1 + return false + end + + (2..num/2).to_a.each do |i| + if num % i == 0 + return false + end + end + + return true + +end + +# tests +puts is_prime?(7) +puts "---------------" +puts is_prime?(14) +puts "---------------" +puts is_prime?(-1) +puts "---------------" +puts is_prime?(0) +puts "---------------" +puts is_prime?(1) +puts "---------------" +puts is_prime?(2) +puts "---------------" +puts is_prime?(5) + diff --git a/rect_overlap.rb b/rect_overlap.rb new file mode 100644 index 0000000..40d659f --- /dev/null +++ b/rect_overlap.rb @@ -0,0 +1,47 @@ + +def overlap(rect1, rect2) + + # assumptions + # the bottom-left and top-right corners are always given, rather than the bottom-right and top-left + # the coordinates for each rectangle are given as [bottom-left, top-right] or [top-right, bottom-left] + # the first rectangle given isn't always the most bottom and/or most left + + # define corners of each rectange (bl = bottom-left, tr = top-right) + rect1_bl = rect1[0][0] < rect1[1][0] ? rect1[0] : rect1[1] + rect1_tr = rect1[0][0] > rect1[1][0] ? rect1[0] : rect1[1] + rect2_bl = rect2[0][0] < rect2[1][0] ? rect2[0] : rect2[1] + rect2_tr = rect2[0][0] > rect2[1][0] ? rect2[0] : rect2[1] + + # make sure that rect1 is the most "bottom-left" & (crudely) swap if that is not the case + if rect1_bl[0] > rect2_bl[0] or rect1_bl[1] > rect2_bl[1] + temp = rect1_bl + rect1_bl = rect2_bl + rect2_bl = temp + temp = rect1_tr + rect1_tr = rect2_tr + rect2_tr = temp + end + + # print rectangles to check if swap (if necessary) worked + # puts '[' + rect1_bl[0].to_s + ',' + rect1_bl[1].to_s + '], [' + rect1_tr[0].to_s + ',' + rect1_tr[1].to_s + ']' + # puts '[' + rect2_bl[0].to_s + ',' + rect2_bl[1].to_s + '], [' + rect2_tr[0].to_s + ',' + rect2_tr[1].to_s + ']' + + # check if rects overlap by comparing rect1_tr and rect2_bl + return rect2_bl[0] < rect1_tr[0] && rect2_bl[1] < rect1_tr[1] + +end + +puts overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) # scenario 1, true +puts "---------------" +puts overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) # scenario 2, false +puts "---------------" +puts overlap( [ [3,3],[0,0] ], [ [1,1],[4,5] ] ) # scenario 1, coords of rect1 swapped, true +puts "---------------" +puts overlap( [ [0,0],[1,4] ], [ [3,2],[1,1] ] ) # scenario 2, coords of rect2 swapped, false +puts "---------------" +puts overlap( [ [1,1],[4,5] ], [ [0,0],[3,3] ] ) # scenario 1, rect1 & rect2 swapped, true +puts "---------------" +puts overlap( [ [0,4],[5,5] ], [ [0,0],[4,4] ] ) # scenario 3, edge case, false + + + diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..007670d --- /dev/null +++ b/uniques.rb @@ -0,0 +1,28 @@ +def uniques(arr) + + # initialize + no_dupes = [] + + # remove duplicates by iteration and checking preceding elements + arr.each_with_index do |val_outer, ind| + has_preceding_dupe = false + arr[0...ind].each do |val_inner| # hinges on use of "..." (last val exclusive) NOT ".." (last val inclusive) + if val_outer == val_inner + has_preceding_dupe = true + end + end + if not has_preceding_dupe + no_dupes << val_outer + end + end + + return no_dupes + +end + +# tests +puts uniques([1,5,"frog", 2,1,3,"frog"]) +puts "---------------" +puts uniques([1,2,3,2,3,5,7,1,4,20,6,1,2]) +puts "---------------" +puts uniques(["a", "A", "eh", "a", "eyy", "eyyy", "eyy"]) \ No newline at end of file