diff --git a/1_power.rb b/1_power.rb new file mode 100644 index 0000000..6604f02 --- /dev/null +++ b/1_power.rb @@ -0,0 +1,15 @@ +def power(base, exponent) + answer = base + + if exponent == 0 # 0 exponent + answer = 1 + else + (exponent.abs - 1).times { answer *= base } + end + + puts exponent < 0 ? (1.0/answer) : (answer) +end + +power(3, 0) +power(3, 1) +power(3, -3) \ No newline at end of file diff --git a/2_factorial.rb b/2_factorial.rb new file mode 100644 index 0000000..9b3b183 --- /dev/null +++ b/2_factorial.rb @@ -0,0 +1,14 @@ +def factorial (number) + answer = 1 + i = 1 + + while i <= 5 do + answer *= i + + i += 1 + end + + puts answer +end + +factorial(5) \ No newline at end of file diff --git a/3_uniques.rb b/3_uniques.rb new file mode 100644 index 0000000..ba00707 --- /dev/null +++ b/3_uniques.rb @@ -0,0 +1,11 @@ +def uniques(array) + unique_array = [] + + array.each do |item| + unique_array << item unless unique_array.include? item + end + + puts unique_array +end + +uniques([1, 5, "frog", 2, 1, 3, "frog"]) \ No newline at end of file diff --git a/4_combinations.rb b/4_combinations.rb new file mode 100644 index 0000000..349c959 --- /dev/null +++ b/4_combinations.rb @@ -0,0 +1,18 @@ +def combinations(first, second) + combo_array = [] + current_word = "" + + first.each do |first_val| + second.each do |second_val| + current_word += first_val + current_word += second_val + + combo_array << current_word + current_word = "" + end + end + + return combo_array +end + +puts combinations(["on", "in"], ["to", "rope"]) \ No newline at end of file diff --git a/5_primes.rb b/5_primes.rb new file mode 100644 index 0000000..d58cd68 --- /dev/null +++ b/5_primes.rb @@ -0,0 +1,10 @@ +def is_prime?(number) + return false if number <= 1 # 1 and below are not primes + + Math.sqrt(number).to_i.downto(2).each { |factor| return false if number % factor == 0 } # has a factor, is not prime + + true # prime number +end + +puts is_prime?(7) +puts is_prime?(14) \ No newline at end of file diff --git a/6_rectancle_overlap.rb b/6_rectancle_overlap.rb new file mode 100644 index 0000000..97d2337 --- /dev/null +++ b/6_rectancle_overlap.rb @@ -0,0 +1,14 @@ +# assumes that coordinates given are for the bottom left and top right of each rectangle + +# assumes the first set of coordinates is for the lower/left most rectangle + +def overlap(first, second) + if (first[1][0] > second[0][0]) && (first[1][1] > second[0][1]) + return "rectangles overlap" + else + return "rectangles don't overlap" + end +end + +puts overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) +puts overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) \ No newline at end of file diff --git a/counting_game.rb b/counting_game.rb new file mode 100644 index 0000000..2a87b6a --- /dev/null +++ b/counting_game.rb @@ -0,0 +1,37 @@ +current_number = 1 +direction = true +player = 1 + +while current_number <= 100 do + + puts "Player #{player} says: #{current_number}" + + if current_number % 7 == 0 + direction = !direction + elsif current_number % 11 == 0 + if direction + player += 1 + else + if player == 1 + player = 10 + else + player -= 1 + end + end + end + + if !(1...10).include? player + player = 1 + else + if direction + player += 1 + else + if player == 1 + player = 10 + else + player -= 1 + end + end + end + current_number += 1 +end \ No newline at end of file