diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/0_README.md similarity index 100% rename from README.md rename to 0_README.md diff --git a/1.power.rb b/1.power.rb new file mode 100644 index 0000000..655209c --- /dev/null +++ b/1.power.rb @@ -0,0 +1,10 @@ +def power(a,b) # define method taking two arguments + sum = a # variable created, first number and final answer + while b > 1 # start while loop to multiply base by power repeatedly + sum *= a # value of base times base added to sum + b -= 1 # power used as count and reduced by one + end # end of while loop run "power" number of times +puts sum # result output +end # end of method + +power(3,4) # returns 81 \ No newline at end of file diff --git a/2.factorial.rb b/2.factorial.rb new file mode 100644 index 0000000..6eaeac2 --- /dev/null +++ b/2.factorial.rb @@ -0,0 +1,11 @@ + +def factorial(num) # define method that takes one argument + sum = 1 # define variable to store answer and start equation + while num > 0 # start while loop to count down to smallest number + sum = num * sum # start recurring equation + num-=1 # lower the number being multipied each time + end # end the "while" loop +puts sum # outpuythe answer +end # end + +factorial(5) # returns 120 \ No newline at end of file diff --git a/3.uniques.rb b/3.uniques.rb new file mode 100644 index 0000000..cf0684e --- /dev/null +++ b/3.uniques.rb @@ -0,0 +1,9 @@ +def uniques(array) # define uniques method + no_dupes = [] # create new array for duplicate free array + array.each do |x| # start iteration on array + no_dupes << x unless no_dupes.include?(x) # push all items to new array unless array item is already included. + end # end of "do" block + puts no_dupes.inspect # display the array (in array format) +end # end of method + +uniques([1,5,"frog",2,1,3,"frog"]) # returns [1, 5, "frog", 2, 3] \ No newline at end of file diff --git a/4.combinations.rb b/4.combinations.rb new file mode 100644 index 0000000..9764480 --- /dev/null +++ b/4.combinations.rb @@ -0,0 +1,12 @@ +# after som investigation this needs to be refacatored to iterate over both arrays (a codeblock within a codeblock) + +def combinations(a, b) # define method that takes two arrays + combined_array = [] # create new empty array + combined_array << a[0]+b[0] # these four lines are not very "DRY" + combined_array << a[0]+b[1] # but they do concatenate the strings + combined_array << a[1]+b[0] # from the 2D arrays in the correct order + combined_array << a[1]+b[1] # will only work though for two srings in each + puts combined_array.inspect # outputs in array format +end # end of method + +combinations(["on","in"],["to","rope"]) # returns ["onto", "onrope", "into", "inrope"] \ No newline at end of file diff --git a/5.primes.rb b/5.primes.rb new file mode 100644 index 0000000..5895d20 --- /dev/null +++ b/5.primes.rb @@ -0,0 +1,19 @@ +def is_prime?(number) # define is_prime? method + prime_checker = [] # empty array to store divisable numbers + range_of_nums = Array(1..number) # array of integars between one and possible prime + range_of_nums.each do |x| # iterate over the array + if (number % x == 0) # check if number eqully divisable + prime_checker << x # if it is, push to prime_checker array + end # end of "if" block + end # end of "do" block + if (prime_checker.length > 2) # check if more than "1" or "number" are in checker array + puts "No, #{number} is not a prime" # if more than those two numbers are in there, cannot be prime + else # else + puts "Yes, #{number} is a prime!" # if just two numbers in there, it must be a prime + end # end of if/else block +end # end of method + +is_prime?(7) +is_prime?(14) + + diff --git a/6.overlap.rb b/6.overlap.rb new file mode 100644 index 0000000..704391a --- /dev/null +++ b/6.overlap.rb @@ -0,0 +1,63 @@ +# Write a method overlap which takes two rectangles defined +# by the coordinates of their corners, and determines whether they overlap. +# You can assume all coordinates are positive integers. +puts "test" +def overlap(arr_one, arr_two) + # if rectangle_one starts lower than rectangle_two + if (arr_one[0][0] + arr_one[0][1] == arr_two[0][0] + arr_two[0][1]) && (arr_one[0][0] < arr_two[0][0]) + rec_one = arr_one # bottom left sum is equal therefore act as though first rectangle lower + rec_two = arr_two + elsif arr_one[0][0] + arr_one[0][1] < arr_two[0][0] + arr_two[0][1] # first box is lower + rec_one = arr_one + rec_two = arr_two + else # second box lower + rec_one = arr_two + rec_two = arr_one + end + + if (rec_one[1][0] > rec_two[0][0]) && (rec_one[1][1] > rec_two[0][0]) + puts "Rectangles overlap" # both the bottom left hand x and y axes of rec_two + puts arr_one.inspect # must be higher than the top right hand x and y axes + puts arr_two.inspect # of rec_one to be sure they don't overlap + puts "" + else + puts "Rectangles do not overlap" + puts arr_one.inspect + puts arr_two.inspect + puts "" + end + +end + +overlap([[0,0],[3,3]], [[1,1],[4,5]]) # returns true/overlap + +overlap([[0,0],[1,4]], [[1,1],[3,2]]) # returns false/do not overlap + +puts "" +puts "####################################################################" +puts "# Additional tests below to cover second box being closer to x #" +puts "# axis or where boxes could be completely inside other boxes #" +puts "# These scenarios were not given as possiblities #" +puts "# in excercise question but are possible #" +puts "####################################################################" +puts "" +overlap([[0,0],[3,1]], [[1,1],[4,5]]) # returns false/do not overlap + +overlap([[6,0],[7,5]], [[0,0],[1,1]]) # returns false/do not overlap + +overlap([[1,1],[2,2]], [[0,0],[1,1]]) # returns false/do not overlap + +overlap([[2,2],[4,4]], [[0,0],[3,3]]) # returns true/overlap + +overlap([[0,2],[2,10]], [[2,0],[10,2]]) # returns false/do not overlap + +overlap([[2,0],[3,1]], [[0,2],[2,100]]) # returns false/do not overlap + +overlap([[1,0],[5,1]], [[0,1],[1,5]]) # returns false/do not overlap + +overlap([[0,1],[1,5]], [[1,0],[5,1]]) # returns false/do not overlap +puts "first box inside second box" +overlap([[3,3],[6,6]], [[2,2],[8,8]]) # returns true/overlap (this is first box inside second box) +puts "second box inside first box" +overlap([[2,2],[8,8]], [[3,3],[6,6]]) # returns true/overlap (this is second box inside first box) + diff --git a/7.counting_game.rb b/7.counting_game.rb new file mode 100644 index 0000000..ab7f208 --- /dev/null +++ b/7.counting_game.rb @@ -0,0 +1,66 @@ +# 10 friends are sitting in a circle around a table and decide to +# play a new game. In it, they count up through the numbers from 1 +# to 100. The first person says "1", the second says "2" and so +# on... but with a few catches: + +# Whenever the number is divisible by 7, they switch directions. +# So person 6 will say "6", person 7 will say "7", then person 6 +# again will say "8". + +# Whenever the number is divisible by 11, they skip the next person +# for the following number. For instance, if person 3 says "33", +# person 5 will say "34" instead (person 4 gets skipped). + +# Your job is to code a program which outputs each number and which +# person said it. Use it to show that player 1 will say the number +# "100". + +count = 0 +person_number = 0 +game_number = 0 +direction = -1 # backwards +direction = 1 # forwards + +while count < 100 + + count += 1 + + if direction == 1 # direction is forward + person_number += 1 # person number increased by 1 + end + + if direction == -1 # direction reversed + person_number -= 1 # person number decreased by 1 + end + + game_number += 1 # game number increases by 1 + + if game_number % 7 == 0 # change direction if game number + direction = direction * -1 # divisable by 7 + end + + if person_number < 1 # if looped around group of peopl + person_number += 100 # subtract total to get back + end + + if person_number > 100 # same as above in other direction + person_number -= 100 + end + +puts "Person Number #{person_number}. says #{game_number}" + + if game_number % 11 == 0 && direction == 1 # if game number multiple + person_number += 1 # of 11 skip a person + end + + if game_number % 11 == 0 && direction == -1 # same as above but in other + person_number -= 1 # direciton + end + +end + + + + + +