diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..1e9d875 --- /dev/null +++ b/combinations.rb @@ -0,0 +1,25 @@ +# INSTRUCTIONS +# method takes two arrays of strings +# returns an array of new strings +# each string is a combination of item in first array with item in second array + +# APPROACH +# initialize new array +# I'm going to need 2 nested iterators which will combine the strings using "each" +# and finally return the new array + +def combinations(arr1, arr2) + combined_array = [] + arr1.each do |first_string| + arr2.each do |second_string| + new_word = first_string + second_string + combined_array.push(new_word) + end + end + puts combined_array +end + +arr1 = ["on","in"] +arr2 = ["to", "rope"] + +combinations(arr1, arr2) \ No newline at end of file diff --git a/combinations2.rb b/combinations2.rb new file mode 100644 index 0000000..abc7a76 --- /dev/null +++ b/combinations2.rb @@ -0,0 +1,23 @@ +# TASK +# Write a method that takes 2 arrays of strings +# Returns 1 array with all of the combinatiosn of the items in them + +# APPROACH +# Create 2 each loops, one nested inside another +# Iterate over items and select them +# Concatenate items and push to new array + +def combinations(arr1,arr2) + new_array = [] + arr1.each do |i| + arr2.each do |j| + new_array.push(i + j) + end + end + puts new_array +end + +arr1 = ["on", "in"] +arr2 = ["to", "rope"] + +combinations(arr1,arr2) \ No newline at end of file diff --git a/counting_game.rb b/counting_game.rb new file mode 100644 index 0000000..e0739b5 --- /dev/null +++ b/counting_game.rb @@ -0,0 +1,59 @@ +# INSTRUCTIONS +# 10 friends sit in a circle and count from 1 to 100. +# When the number is divisible by 7, they switch directions +# When the number is divisible by 11, they skip the next person +# Find who says the number "100" + +# APPROACH +# create a while loop that continues until num == 100 +# inside that loop create a variable, person +# person will begin at 1 +# when one loop finishes, it will increment +# when person equals anything larger than 10, we will subtract 10 from it +# when person equals anything smaller than 1, we will add 10 to it +# also inside that while loop, call a method called count direction +# that decides whether to increment or decrement person +# this count direction method will check if num if divisible by 7 +# it will also check if num if divisible by 11, and skip if necessary +# the main method will puts the person at the end + +def counting_game(num_players, final_num) + player = 1 + num = 1 + reversed = false + while num <= final_num + + puts "Player: #{player}" + puts "Num: #{num}" + + if num % 7 == 0 + reversed == false ? reversed = true : reversed = false + end + + if num % 11 == 0 + increment = 2 + else + increment = 1 + end + + if reversed + player -= increment + else + player += increment + end + + if player > num_players + player -= num_players + end + + if player < 1 + player += num_players + end + + num += 1 + + end +end + +counting_game(10, 100) + diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..3b4775c --- /dev/null +++ b/factorial.rb @@ -0,0 +1,12 @@ +# will take a number +# return the product of every number up to and including that number + +def factorial(num) + product = 1 + 1.upto(num) do |i| + product *= i + end + puts product +end + +factorial(5) \ No newline at end of file diff --git a/is_prime?.rb b/is_prime?.rb new file mode 100644 index 0000000..e6746b3 --- /dev/null +++ b/is_prime?.rb @@ -0,0 +1,23 @@ +# INSTRUCTIONS +# method takes a number +# returns true if the number is prime + +# APPROACH +# I'll know for sure that a number is prime if it has no factors +# I can check if the number has any factors by iterating from 2 up to the number +# If the original number can be evenly divided by the iterated number, then I'll know it's not prime +# Otherwise, it is prime! +# Let's do this! + +def is_prime?(num) + prime = true + 2.upto(num - 1) do |i| + if num % i == 0 + prime = false + end + end + puts prime +end + +is_prime?(7) +is_prime?(14) \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..d8fa7ea --- /dev/null +++ b/overlap.rb @@ -0,0 +1,57 @@ +# INSTRUCTIONS +# method takes two rectangles +# defined by the coordinates of their corners +# for example => [[0,0],[3,3]] and [[1,1],[4,6]] +# I must determine whether the rectangles overlap +# all coordinates are positive ints +# it doesn't count if the edges only touch but don't cross + +# APPROACH +# By definition, "overlapping" is when the 2 rectangles share an area +# I can detect if they share an area by splitting each large rectangle into many small cells +# If any of the bottom left coordinatees of the cells match, I will know that the rectangles overlap +# So first I need to generate an array of x and y cell coordinates for each rectangle +# then I can loop through the array of one rectangle +# and loop through the array of the other rectangle inside +# if any of the coordinates match, then the rectangles overlap + +def overlap?(rect1, rect2) + + rect1_coords = coordinate_generator(rect1) + rect2_coords = coordinate_generator(rect2) + z = rect1_coords - rect2_coords + + if z == rect1_coords + puts false + else + puts true + end + +end + +def coordinate_generator(rect) + + rect_x_coord = [] + rect_y_coord = [] + y = rect[0][1] + + while y < rect[1][1] + x = rect[0][0] + while x < rect[1][0] + rect_x_coord.push(x) + rect_y_coord.push(y) + x += 1 + end + y += 1 + end + + combined_coords = rect_x_coord.zip(rect_y_coord) + return combined_coords + +end + +rect1 = [[0,0],[3,3]] +rect2 = [[1,1],[4,5]] +rect3 = [[7,7],[9,9]] + +overlap?(rect1,rect2) diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..b0397b2 --- /dev/null +++ b/power.rb @@ -0,0 +1,15 @@ +# define a method power that takes base and exponent +# it will multiply base by base exponent times +# and return the result + +def power(base, exponent) + result = 1 + exponent.times do |i| + result *= base + end + puts result +end + +power(3,4) + + diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..8c9449c --- /dev/null +++ b/uniques.rb @@ -0,0 +1,24 @@ +# INSTRUCTIONS +# method takes an array of items +# return that array without duplicates +# without Ruby's "uniq" method + +# HOW I'LL DO THIS +# Take array +# Def new array +# use an enumerable method, perhaps "include?" +# call include? on new array, if false, push item to new array + +def uniques(array) + unique_array = [] + array.each do |item| + if !unique_array.include?(item) + unique_array.push(item) + end + end + puts unique_array +end + +array = [1,5,"frog",2,1,3,"frog"] + +uniques(array) \ No newline at end of file