From 6005e67033e03f473d0cf5b119e6efb657838bab Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Tue, 25 Oct 2016 20:10:34 -0600 Subject: [PATCH 1/8] Done Power Exercise --- power.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 power.rb diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..48cd8e5 --- /dev/null +++ b/power.rb @@ -0,0 +1,15 @@ +$stdout.sync = true + +print "Enter a base: " +base = gets.chomp.to_i +print "Enter an exponent: " +exponent = gets.chomp.to_i + +def power(base, exponent) + + 2.upto(exponent) { base=base*base } + return base + +end + +puts power(base, exponent) \ No newline at end of file From d6b7911bf52bd4ed7c8b597e4d45e03b445d3e7b Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Tue, 25 Oct 2016 22:12:55 -0600 Subject: [PATCH 2/8] Done Factorial Exercise --- power.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power.rb b/power.rb index 48cd8e5..ad2e393 100644 --- a/power.rb +++ b/power.rb @@ -7,7 +7,7 @@ def power(base, exponent) - 2.upto(exponent) { base=base*base } + 2.upto(exponent) { base*=base } return base end From 026562d4e415905a98c0bd70744d2ece4e78516d Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Wed, 26 Oct 2016 07:31:08 -0600 Subject: [PATCH 3/8] Done Uniques Exercise --- factorial.rb | 18 ++++++++++++++++++ uniques.rb | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 factorial.rb create mode 100644 uniques.rb diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..adf834e --- /dev/null +++ b/factorial.rb @@ -0,0 +1,18 @@ +$stdout.sync = true + +print "Enter a number 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/uniques.rb b/uniques.rb new file mode 100644 index 0000000..9517c3c --- /dev/null +++ b/uniques.rb @@ -0,0 +1,22 @@ +$stdout.sync = true + +puts "Add items to list seperated by a \",\"" +array_of_items = gets.chomp + +def uniques(array_of_items) + + unique_items = [] + + array_of_items.each { |a| + unless unique_items.include?(a) then unique_items << a end + } + + return unique_items + +end + + +print array_of_items.strip.split(/\s*,\s*/) +puts "" +print uniques(array_of_items.strip.split(/\s*,\s*/)) +puts "" \ No newline at end of file From 462d84fffcb275dd618f58ffdc1bb991ef6cd93d Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Wed, 26 Oct 2016 08:32:10 -0600 Subject: [PATCH 4/8] Done Combinations Exercise --- combinations.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 combinations.rb diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..f663d80 --- /dev/null +++ b/combinations.rb @@ -0,0 +1,25 @@ +$stdout.sync = true + +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 = [] + + 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 From 3348dfe20a60588d56f3425d8f1408b6725f7d92 Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Wed, 26 Oct 2016 09:46:24 -0600 Subject: [PATCH 5/8] Done Prime Exercise --- primes.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 primes.rb diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..d55dabe --- /dev/null +++ b/primes.rb @@ -0,0 +1,25 @@ +$stdout.sync = true + +require 'prime' + +print "Enter a number: " +number = gets.chomp.to_i + +def is_prime?(number) + Prime.prime?(number) +end + +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) From ac79a4b9c997fa514a152a9d1bcd8fdbf7ade8c4 Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Thu, 27 Oct 2016 07:34:17 -0600 Subject: [PATCH 6/8] Done Overlap Exercise --- overlap.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 overlap.rb diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..60c94f5 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,43 @@ +$stdout.sync = true + +box_1 = Hash.new +box_2 = Hash.new + +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 "" + +def get_box_size(box_1) + + box_size = box_1["ll"][0].to_i - box_1["ur"][0].to_i + + return box_size.abs +end + +def check_overlap(box_1, box_2, size) + + overlap = box_1["ll"][0].to_i - box_2["ll"][0].to_i + if overlap.abs < size then return true end + overlap = box_1["ll"][1].to_i - box_2["ll"][1].to_i + if overlap.abs < size then return true end + overlap = box_1["ur"][0].to_i - box_2["ur"][0].to_i + if overlap.abs < size then return true end + overlap = box_1["ur"][1].to_i - box_2["ur"][1].to_i + if overlap.abs < size then return true end + + return false + +end + +box_size = get_box_size(box_1) + +puts check_overlap(box_1, box_2, box_size)? "Boxes Overlap" : "Boxes do not Overlap" +puts "" \ No newline at end of file From 64368e5a0d9142865e26f070fb7cb1618b4fdc6e Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Fri, 28 Oct 2016 17:03:34 -0600 Subject: [PATCH 7/8] Done Counting Excersise --- CountingGame.rb | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ counting.rb | 79 +++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 CountingGame.rb create mode 100644 counting.rb diff --git a/CountingGame.rb b/CountingGame.rb new file mode 100644 index 0000000..2395f93 --- /dev/null +++ b/CountingGame.rb @@ -0,0 +1,130 @@ +$stdout.sync = true +class CountingGame + def initialize(players, last_number) + @players = players # number of players + @game = Array.new(@players) + @number = 1 # the "number" being said by players + @game[0] = 1 + @last_number = last_number # number counting to + end + + def test + # puts "current player is: #{current_player}" + # puts "number is: #{@number}" + end + + def play_game + @counter = 1 + + while (@counter < @last_number) + puts "Player #{current_player} says #{@number}" + @counter += 1 + forward + end + winner + end + + private + + # move forward one player + def forward + if (@number % 11 == 0) + skip_forward + elsif current_index == last + @game[0] = next_num + say_number + else + @next = current_index + @next = @next.to_i + 1 + @game[@next] = next_num + say_number + end + end + + # change direction and move one player when + # @number is divisible by 7 + def reverse + + end + + # skip over one player when @number divisible by 11 + def skip_forward + if (@number % 11 == 0) + if current_index == last - 1 # @game[-2] + @game[0] = next_num + say_number + elsif current_index == last # @game[-1] + @game[1] = next_num + say_number + else + @next = current_index + @next = @next.to_i + 2 + @game[@next] = next_num + say_number + end + end + end + + # skip over one player when @number divisible by 11 + def skip_reverse + if (@number % 11 == 0) + if current_index == first + 1 # @game[1] + @game[-1] = next_num + say_number + elsif current_index == first # @game[0] + @game[-2] = next_num + say_number + else + @next = current_index + @next = @next.to_i - 2 + @game[@next] = next_num + say_number + end + end + end + + # returns the player who "says" the @last_number + def winner + if @number == @last_number + puts "Player #{current_player} says #{@number} **WINNER**" + end + end + + # return index (player location) + def current_index + @game.index(@number) + end + + def current_player + current_index + 1 + end + + # increments @number by 1 + def say_number + @number += 1 + end + + def next_num + @number + 1 + end + + # first index in @game array + def first + @game[0] + end + + # last index in @game array + def last + @game.length - 1 + end + + # keep track of which direction game is going + def direction + + end +end + + +game = CountingGame.new(10, 100) + +game.play_game \ 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 From 719a666d358433cbee95d896fb7df53f6faf9292 Mon Sep 17 00:00:00 2001 From: Aaron Olsen Date: Sun, 30 Oct 2016 22:00:09 -0600 Subject: [PATCH 8/8] Fixed counting game | added comments to all files --- CountingGame.rb | 130 ------------------------------------------------ README.md | 4 -- combinations.rb | 6 ++- factorial.rb | 14 +++++- overlap.rb | 62 ++++++++++++++--------- power.rb | 15 +++++- primes.rb | 16 +++++- uniques.rb | 19 ++++--- 8 files changed, 97 insertions(+), 169 deletions(-) delete mode 100644 CountingGame.rb delete mode 100644 README.md diff --git a/CountingGame.rb b/CountingGame.rb deleted file mode 100644 index 2395f93..0000000 --- a/CountingGame.rb +++ /dev/null @@ -1,130 +0,0 @@ -$stdout.sync = true -class CountingGame - def initialize(players, last_number) - @players = players # number of players - @game = Array.new(@players) - @number = 1 # the "number" being said by players - @game[0] = 1 - @last_number = last_number # number counting to - end - - def test - # puts "current player is: #{current_player}" - # puts "number is: #{@number}" - end - - def play_game - @counter = 1 - - while (@counter < @last_number) - puts "Player #{current_player} says #{@number}" - @counter += 1 - forward - end - winner - end - - private - - # move forward one player - def forward - if (@number % 11 == 0) - skip_forward - elsif current_index == last - @game[0] = next_num - say_number - else - @next = current_index - @next = @next.to_i + 1 - @game[@next] = next_num - say_number - end - end - - # change direction and move one player when - # @number is divisible by 7 - def reverse - - end - - # skip over one player when @number divisible by 11 - def skip_forward - if (@number % 11 == 0) - if current_index == last - 1 # @game[-2] - @game[0] = next_num - say_number - elsif current_index == last # @game[-1] - @game[1] = next_num - say_number - else - @next = current_index - @next = @next.to_i + 2 - @game[@next] = next_num - say_number - end - end - end - - # skip over one player when @number divisible by 11 - def skip_reverse - if (@number % 11 == 0) - if current_index == first + 1 # @game[1] - @game[-1] = next_num - say_number - elsif current_index == first # @game[0] - @game[-2] = next_num - say_number - else - @next = current_index - @next = @next.to_i - 2 - @game[@next] = next_num - say_number - end - end - end - - # returns the player who "says" the @last_number - def winner - if @number == @last_number - puts "Player #{current_player} says #{@number} **WINNER**" - end - end - - # return index (player location) - def current_index - @game.index(@number) - end - - def current_player - current_index + 1 - end - - # increments @number by 1 - def say_number - @number += 1 - end - - def next_num - @number + 1 - end - - # first index in @game array - def first - @game[0] - end - - # last index in @game array - def last - @game.length - 1 - end - - # keep track of which direction game is going - def direction - - end -end - - -game = CountingGame.new(10, 100) - -game.play_game \ No newline at end of file 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 index f663d80..71dceaa 100644 --- a/combinations.rb +++ b/combinations.rb @@ -1,4 +1,5 @@ -$stdout.sync = true +$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 @@ -9,6 +10,9 @@ 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 = [] diff --git a/factorial.rb b/factorial.rb index adf834e..586e527 100644 --- a/factorial.rb +++ b/factorial.rb @@ -1,6 +1,16 @@ -$stdout.sync = true +$stdout.sync = true # Magically solves my bash buffering issue so .gets + # show up before puts. Windows 10 issue possibly. -print "Enter a number find its factorial: " +=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) diff --git a/overlap.rb b/overlap.rb index 60c94f5..de54639 100644 --- a/overlap.rb +++ b/overlap.rb @@ -1,8 +1,21 @@ -$stdout.sync = true +$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: " @@ -15,29 +28,32 @@ puts "" -def get_box_size(box_1) - - box_size = box_1["ll"][0].to_i - box_1["ur"][0].to_i +# 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 - return box_size.abs end -def check_overlap(box_1, box_2, size) - - overlap = box_1["ll"][0].to_i - box_2["ll"][0].to_i - if overlap.abs < size then return true end - overlap = box_1["ll"][1].to_i - box_2["ll"][1].to_i - if overlap.abs < size then return true end - overlap = box_1["ur"][0].to_i - box_2["ur"][0].to_i - if overlap.abs < size then return true end - overlap = box_1["ur"][1].to_i - box_2["ur"][1].to_i - if overlap.abs < size then return true end - - return false - -end - -box_size = get_box_size(box_1) - -puts check_overlap(box_1, box_2, box_size)? "Boxes Overlap" : "Boxes do not Overlap" +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 index ad2e393..5019957 100644 --- a/power.rb +++ b/power.rb @@ -1,4 +1,15 @@ -$stdout.sync = true +$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 @@ -7,6 +18,8 @@ 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 diff --git a/primes.rb b/primes.rb index d55dabe..3a97be6 100644 --- a/primes.rb +++ b/primes.rb @@ -1,14 +1,28 @@ -$stdout.sync = true +$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) diff --git a/uniques.rb b/uniques.rb index 9517c3c..b3fda4b 100644 --- a/uniques.rb +++ b/uniques.rb @@ -1,13 +1,16 @@ -$stdout.sync = true +$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 \",\"" -array_of_items = gets.chomp +list_of_items = gets.chomp -def uniques(array_of_items) +def uniques(list_of_items) unique_items = [] - array_of_items.each { |a| +# 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 } @@ -15,8 +18,10 @@ def uniques(array_of_items) end - -print array_of_items.strip.split(/\s*,\s*/) +# 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 "" -print uniques(array_of_items.strip.split(/\s*,\s*/)) +# 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