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

Ruby Judo submission #137

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
253 changes: 253 additions & 0 deletions ruby_judo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@


puts "There are 6 methods to try"
puts "1.POWER 2.FACTORIAL 3.UNIQUES 4.COMBINATIONS 5.RECTANGLE OVERLAP 6.THE COUNTING GAME"
puts "Which would you like to try? Please enter the name or number of the method."
choice = gets.chomp.downcase



=begin
POWER
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)
=end

def power(base, exponent)
a = base
(exponent-1).times{a *= base} #exponent -1 because times starts at 0
return a
end

if choice == "power" || choice == "1"
puts "POWER"
puts "This will return an expoent of a number. First enter the base number."
base = gets.chomp.to_i
if base.is_a?(Integer)
puts "Now enter the exponent."
exponent = gets.chomp.to_i
if exponent.is_a?(Integer)
answer = power(base,exponent)

puts "The answer for a base of #{base} and an exponent of #{exponent} is #{answer}."
end
end
end






=begin
FACTORIAL
Write a method factorial which takes a number and returns the product of every number
up to the current number multiplied together.

> factorial(5)
=> 120 # from 1*2*3*4*5
=end

def factorial(b)
(1...b).each do |x|
b *= x
end
return b
end

if choice == "factorial" || choice == "2"
puts "FACTORIAL"
puts "Enter a number to get the product of very number up to it multiplied together"
number = gets.chomp.to_i
answer = factorial(number)
puts "Your answer is #{answer}"
end


=begin
UNIQUES
Write a method uniques which takes an array of items and returns the array without any duplicates.
Don’t use Ruby’s uniq method!

uniques([1,5,”frog”, 2,1,3,”frog”])
=> [1,5,”frog”,2,3]
=end
def uniques(array) #converts incoming string to array
array.each do |x|
position = array.index(x) + 1
while position < array.length
if x === array[position]
array.delete_at(position) #removes repeating elements
else
position += 1
end
end
end
return array
end


if choice == "uniques" || choice == "3"
puts "UNIQUES"
puts "Enter an array with repeating elements separated by commas like this : 1, 2, cat, dog, 2, cat"
arrays = gets.chomp
arrays = arrays.split(",")
answer = uniques(arrays)
puts "Here is your array with all repeating elements removed: #{answer}"
end


=begin

COMBINATIONS
Write a method combinations which takes two arrays of strings and
returns an array with all of the combinations of the items in them,
listing the first items first.

> combinations([“on”,”in”],[“to”,”rope”])
=> [“onto”,”onrope”,”into”,”inrope”]
=end

def combinations(a,b)
new_array = []
a.each do |x|
b.each do |i|
new_string = x + i #adds the two string together
new_array << new_string
end
end
return new_array
end


if choice == "combinations" || choice == "4"
puts "COMBINATIONS"
puts "This method takes 2 lists of strings and returns a list of combinations made from them."
puts "Enter a list of strings separated by commas with no spaces. Like this: on, to, my"
array_1 = gets.chomp
puts "Enter another list of string"
array_2 = gets.chomp
array_1 = array_1.split(",")
array_2 = array_2.split(",")
puts combinations(array_1, array_2)
end


=begin
RECTANGLE OVERLAP
Write a method overlap which takes two rectangles defined by the coordinates of
their corners, e.g. [[0,0],[3,3]] and [[1,1],[4,6]], and determines whether they overlap.
You can assume all coordinates are positive integers.

> overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] )
=> true
> overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] )
=> false

possible solution

grid is

1a 1b
xy xy


2a 2b
xy xy

if both 2b x and y is greater than 1a x and y = overlap
or
if 1b x is greater than 2a x = overlap
=end

def rectangle_overlap (*c)
c = c[0]
#Box 1 coordinates
a1 = [*c[0], *c[1]] #Box 1 bottom left corner coordinates.
b1 = [*c[2], *c[3]] #Box 1 top right corner coordinate.
a2 = [*c[4], *c[5]] #Box 2 bottom left corner coordinate.
b2 = [*c[6], *c[7]] #Box 2 bottom top right coordinate.
puts "c #{c}"
if (a1[0] < b2[0]) && (a1[1]) < (b2[1]) && (b1[0] > a2[0]) && (b1[1] > a2[1]) # tests if coordinates overlap

return "Your rectangles overlap!"
else
return "No overlap"
end
end

if choice == "rectangle overlap" || choice == "5"
puts "This method checks if 2 rectangles overlap on a grid. You will need to enter 8 numbers for the bottom left and top right coordinates for both rectangles"
puts "Here I would add a better mechanism for getting the coordinates, but I don't want to spend more time on this problem"
coordinates = []
until coordinates.length == 8 do
puts "Please add the coodinate"
input = gets.chomp.to_i
coordinates << input
end
puts rectangle_overlap (coordinates)
end

=begin
THE COUNTING GAME
Let's take on a more challenging logic problem. Remember the counting game that you
pseudocoded during the Pseudocoding Assignment? For a recap:

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".

Tips:

Remember to stick with brute force instead of trying to "figure out" the trick to the problem.
Name your variables well!
Ignore the skipping to start out with. Only add it when you're ready.
Make your method take two inputs -- the number of players and the number you're counting up to.
Then see who says the last number each time!
=end

def counting(players, number)
player_position = 1
count = 1
x = 1
until count == number do
if count % 7
x = - 1
elsif count % 11
x = 2 * x
else
x = x
end
count += 1
player_position = player_position + x
if player_position == players + 1
player_position = 1
elsif player_position == players + 2
player_position = 2
elsif player_position == -1
player_position == players
elsif player_position == -2
player_position = players - 1
end
end
puts "The last player is #{player_position}"
end

if choice == "the counting game" || choice == "6"
puts "This method plays the counting game!!!"
puts "How many players are there?"
players = gets.chomp.to_i
puts "What number are they counting to?"
num = gets.chomp.to_i
puts counting(players, num)
end