diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..e17daeb --- /dev/null +++ b/combinations.rb @@ -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"]) \ No newline at end of file diff --git a/counting.rb b/counting.rb new file mode 100644 index 0000000..19463b9 --- /dev/null +++ b/counting.rb @@ -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) \ No newline at end of file diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..30fc44e --- /dev/null +++ b/factorial.rb @@ -0,0 +1,7 @@ +def factorial(num) + tmp = 1 + 1.upto(num) do |i| + tmp = tmp * i + end + tmp +end diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..1dfb1ee --- /dev/null +++ b/overlap.rb @@ -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] ] ) \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..7d3d199 --- /dev/null +++ b/power.rb @@ -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) \ No newline at end of file diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..d9efd19 --- /dev/null +++ b/primes.rb @@ -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 diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..9f3b1fe --- /dev/null +++ b/uniques.rb @@ -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"]) \ No newline at end of file