diff --git a/README.md b/README.md deleted file mode 100644 index 29e36a8..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -prep_ruby_challenges -==================== - -[The Ruby challenge problems from the Markup and Coding course of the Viking Code School Prep Work](http://www.vikingcodeschool.com/web-markup-and-coding/level-up-your-ruby-judo) diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..71dceaa --- /dev/null +++ b/combinations.rb @@ -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 "" \ No newline at end of file diff --git a/counting.rb b/counting.rb new file mode 100644 index 0000000..9b7cf6d --- /dev/null +++ b/counting.rb @@ -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 diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..586e527 --- /dev/null +++ b/factorial.rb @@ -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)}" \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..de54639 --- /dev/null +++ b/overlap.rb @@ -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 "" \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..5019957 --- /dev/null +++ b/power.rb @@ -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) \ No newline at end of file diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..3a97be6 --- /dev/null +++ b/primes.rb @@ -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) diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..b3fda4b --- /dev/null +++ b/uniques.rb @@ -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 "" \ No newline at end of file