Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Judo Level Up! #119

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
File renamed without changes.
10 changes: 10 additions & 0 deletions 1.power.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions 2.factorial.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions 3.uniques.rb
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 12 additions & 0 deletions 4.combinations.rb
Original file line number Diff line number Diff line change
@@ -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"]
19 changes: 19 additions & 0 deletions 5.primes.rb
Original file line number Diff line number Diff line change
@@ -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)


63 changes: 63 additions & 0 deletions 6.overlap.rb
Original file line number Diff line number Diff line change
@@ -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)

66 changes: 66 additions & 0 deletions 7.counting_game.rb
Original file line number Diff line number Diff line change
@@ -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