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

Prep Ruby Challenges #123

Open
wants to merge 8 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
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

29 changes: 29 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

print "Enter first list seperated by a space: "
first_array = gets.chomp
print "Enter second_array seperated by a space: "
second_array = gets.chomp

def combinations(first_array, second_array)

combinations_array = []

# cycles through all items in first array. On each item cycles through all
# items in second array. Add both items on current cycle to combo array, make that into
# a string, and then add it to the combinations_array
first_array.each { |a|
second_array.each { |b|
combo = []
combo << a << b
combinations_array << combo.join
}
}

return combinations_array

end

print combinations(first_array.split, second_array.split)
puts ""
79 changes: 79 additions & 0 deletions counting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

=begin

THE COUNTING GAME

Takes "x" number of players and "x" number players will be counting to.
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).

Just so we're clear, any number divisable by both 7 and 11 is considered "canceled out"
since going back one then skipping ahead is just a convoluted way of saying "go ahead 1".

=end

# Get number players

puts "How many players?: "
number_of_players = gets.chomp.to_i

# Get number players will count to

puts "What number will you count too?: "
number_counting_too = gets.chomp.to_i


def counting(number_of_players, number_counting_too)

number = 1
player = 1

until number > number_counting_too # Play game until a player says last number

puts "Player #{player} says #{number}"

# if number is divisible by 7 and not 11 (so something divisible by both is skipped)
# then subtract 1 from player to go back
if ((number)%7==0) && ((number)%11 != 0)
player -= 1
puts "***Go back***"
# if number is divisible by 11 and not 7 then add to 2 to player to skip a player
elsif ((number)%11 == 0) && ((number)%7 != 0)
player += 2
puts "***Skip***"
else
# otherwise just move to next player
player+=1
end
# if player is now greater than number of players then subtract total number of players
# to cycle back to first player
if player >= number_of_players then player = player - number_of_players end
# if player is now less than 1 (bacause we can't have 0 players) then add total number
# of players to cycle ahead to next player
if player < 1 then player = player + number_of_players end

number+=1

end
end

puts ""
counting(number_of_players, number_counting_too) # play the game!

=begin *** Note ***

I'm not sure how to get player 1 to say 100. If I initialized number of players
starting with 0, then player 1 would say 100, since player 9 would say 99 and we would skip
player 0 and go to player 1. Otherwise all rules seem to play out. Even if I prioritized %7
or %11 then we end up with something other than player 1 saying 100.

=end
28 changes: 28 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

=begin

Factorial

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

=end

print "Enter a number to find its factorial: "
number = gets.chomp.to_i

def factorial(number)

factorial = 1

1.upto(number-1) { |counter|
factorial = factorial*(counter+1)
counter+=1 }

return factorial

end

puts "Factorial of #{number} = #{factorial(number)}"
59 changes: 59 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

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

=end

box_1 = Hash.new
box_2 = Hash.new

# Takes lower left and upper right coordinates of both boxes.
print "Enter Lower Left coordinates (x y) of box 1: "
box_1["ll"] = gets.chomp.split
print "Enter Upper Right coordinates (x y) of box 1: "
box_1["ur"] = gets.chomp.split

print "Enter Lower Left coordinates (x y) of box 2: "
box_2["ll"] = gets.chomp.split
print "Enter Upper Right coordinates (x y) of box 2: "
box_2["ur"] = gets.chomp.split

puts ""

# Checks if a box is either to the right of the other box or above the other box
# Returns false if it is (does not overlap) and true if they are not
# ll 0 = left side
# ll 1 = bottom
# ur 0 = right side
# ur 1 = top

def check_overlap(box_1, box_2)

if box_1["ur"][0].to_i <= box_2["ll"][0].to_i
puts "Box 1 is to the left of box 2"
return false
elsif box_2["ur"][0].to_i <= box_1["ll"][0].to_i
puts "Box 2 is to the left of box 1"
return false
elsif box_1["ll"][1].to_i >= box_2["ur"][1].to_i
puts "Box 1 is above box 2"
return false
elsif box_2["ll"][1].to_i >= box_1["ur"][1].to_i
puts "Box 2 is above box 1"
return false
else
return true
end

end

puts check_overlap(box_1, box_2)? "Boxes Overlap" : "Boxes do not Overlap"
puts ""
28 changes: 28 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

=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!

=end

print "Enter a base: "
base = gets.chomp.to_i
print "Enter an exponent: "
exponent = gets.chomp.to_i

def power(base, exponent)

# cycling from 2 (otherwise it does an extra multiply) up to exponent.
# On each cycle multiply base by itself
2.upto(exponent) { base*=base }
return base

end

puts power(base, exponent)
39 changes: 39 additions & 0 deletions primes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

=begin
5. Primes

Write a method is_prime? which takes in a number and returns true if it is a prime number.
=end

require 'prime'

print "Enter a number: "
number = gets.chomp.to_i


# The cheap way, using ruby's prime method
def is_prime?(number)
Prime.prime?(number)
end

# The long hand-hand way. Checks to see if the user entered
# number is divisible by every number except 1 and itself up to
# the user entered number's squareroot. Returns false as soon as it
# hits a divisible number, otherwise returns true.

def is_prime_long?(number)

sqrt = Math.sqrt(number)

2.upto(sqrt) { |a|
if number%a==0 then return false end
}

return true

end

puts is_prime?(number)
puts is_prime_long?(number)
27 changes: 27 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$stdout.sync = true # Magically solves my bash buffering issue so .gets
# show up before puts. Windows 10 issue possibly.

puts "Add items to list seperated by a \",\""
list_of_items = gets.chomp

def uniques(list_of_items)

unique_items = []

# Cycle through entire list_of_items array. On each item cycle through
# unique_items array to see if it's already there. If not, add the item
list_of_items.each { |a|
unless unique_items.include?(a) then unique_items << a end
}

return unique_items

end

# Strips white space from either side of a "," and seperates each string by ","
# makes it into an array and puts that to display
print list_of_items.strip.split(/\s*,\s*/)
puts ""
# Does same thing as above, but sends it to uniques method before putsing it to display
print uniques(list_of_items.strip.split(/\s*,\s*/))
puts ""