From 1f0d5f39d7d43c6d625cc3dec2aa5c69096079ae Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Sun, 1 Jan 2017 14:09:19 +0900 Subject: [PATCH 1/6] finish POWER and FACTORIAL --- ruby_judo.rb | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 ruby_judo.rb diff --git a/ruby_judo.rb b/ruby_judo.rb new file mode 100644 index 0000000..31bfe76 --- /dev/null +++ b/ruby_judo.rb @@ -0,0 +1,122 @@ + + +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?" +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} + return a +end + +if choice == "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" + 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 + +=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 + +=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 +=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 From 989ed96ff264438cfce478489555c8697d777a09 Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Mon, 2 Jan 2017 01:17:41 +0900 Subject: [PATCH 2/6] add uniques method --- ruby_judo.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ruby_judo.rb b/ruby_judo.rb index 31bfe76..1867074 100644 --- a/ruby_judo.rb +++ b/ruby_judo.rb @@ -73,8 +73,30 @@ def factorial(b) uniques([1,5,”frog”, 2,1,3,”frog”]) => [1,5,”frog”,2,3] =end +def uniques(array) + array = array.split(",") + array.each do |x| + position = array.index(x) + 1 + while position < array.length + if x === array[position] + array.delete_at(position) + else + position += 1 + end + end + end + return array +end + +if choice == "uniques" + puts "Enter an array with repeating elements separated by commas like this : 1, 2, cat, dog, 2, cat" + arrays = gets.chomp + 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, From b66105a174f8ac44760421c1c8ed5efdc2983c73 Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Mon, 2 Jan 2017 01:56:43 +0900 Subject: [PATCH 3/6] add combinations method --- ruby_judo.rb | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/ruby_judo.rb b/ruby_judo.rb index 1867074..ad8c95f 100644 --- a/ruby_judo.rb +++ b/ruby_judo.rb @@ -18,7 +18,7 @@ def power(base, exponent) a = base - (exponent-1).times{a *= base} + (exponent-1).times{a *= base} #exponent -1 because times starts at 0 return a end @@ -74,12 +74,12 @@ def factorial(b) => [1,5,”frog”,2,3] =end def uniques(array) - array = array.split(",") - array.each do |x| + #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) + while position < array.length + if x === array[position] + array.delete_at(position) #removes repeating elements else position += 1 end @@ -89,12 +89,15 @@ def uniques(array) end -if choice == "uniques" +if choice == "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 @@ -106,6 +109,29 @@ def uniques(array) => [“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 + puts new_array +end + +if choice = "combinations" + puts "This method takes 2 lists of strings and returns a list of combinations made from them. 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 "hi" + puts combinations(array_1, array_2) +end + + =begin RECTANGLE OVERLAP Write a method overlap which takes two rectangles defined by the coordinates of From a71342dc1ec0fe36ed4e16299031c2321f348f68 Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Mon, 2 Jan 2017 04:58:53 +0900 Subject: [PATCH 4/6] possible rectangle solution in comments --- ruby_judo.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ruby_judo.rb b/ruby_judo.rb index ad8c95f..d49bebf 100644 --- a/ruby_judo.rb +++ b/ruby_judo.rb @@ -127,7 +127,6 @@ def combinations(a,b) array_2 = gets.chomp array_1 = array_1.split(",") array_2 = array_2.split(",") - puts "hi" puts combinations(array_1, array_2) end @@ -142,6 +141,21 @@ def combinations(a,b) => 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 =begin From ee620e7196fccf26bca0d6284e5a1a2d3b9767c9 Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Wed, 4 Jan 2017 02:11:21 +0900 Subject: [PATCH 5/6] possible rectangle solution2 --- ruby_judo.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ruby_judo.rb b/ruby_judo.rb index d49bebf..a82894e 100644 --- a/ruby_judo.rb +++ b/ruby_judo.rb @@ -158,6 +158,26 @@ def combinations(a,b) if 1b x is greater than 2a x = overlap =end +def rectangle_overlap (*c) + a1_x = [*c[0], *c[1]]. #Box 1 bottom left corner X coordinate. + a1_y = [*c[2], *c[3]] + b1_x = [*c[4], *c[5]] + b1_y = [*c[6], *c[7]] + + a2_x = [*c[8], *c[9]]. #Box 2 bottom left corner X coordinate. + a2_y = [*c[10], *c[11]] + b2_x = [*c[12], *c[13]] + b2_y = [*c[14], *c[15]] + + if + + +return a1 +end + +puts rectangle_overlap "0", "0", "3", "3", "1", "1", "4", "5", "0", "0", "1", "4", "1", "1", "3", "2" + + =begin THE COUNTING GAME Let's take on a more challenging logic problem. Remember the counting game that you From a15f4a6c70216479619b93141dd08746d6cb6b05 Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Sat, 7 Jan 2017 03:00:55 +0900 Subject: [PATCH 6/6] finished ruby judo --- ruby_judo.rb | 99 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 25 deletions(-) diff --git a/ruby_judo.rb b/ruby_judo.rb index a82894e..4e3e87d 100644 --- a/ruby_judo.rb +++ b/ruby_judo.rb @@ -2,7 +2,7 @@ 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?" +puts "Which would you like to try? Please enter the name or number of the method." choice = gets.chomp.downcase @@ -22,7 +22,8 @@ def power(base, exponent) return a end -if choice == "power" +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) @@ -57,7 +58,8 @@ def factorial(b) return b end -if choice == "factorial" +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) @@ -73,8 +75,7 @@ def factorial(b) uniques([1,5,”frog”, 2,1,3,”frog”]) => [1,5,”frog”,2,3] =end -def uniques(array) - #converts incoming string to array +def uniques(array) #converts incoming string to array array.each do |x| position = array.index(x) + 1 while position < array.length @@ -89,7 +90,8 @@ def uniques(array) end -if choice == "uniques" +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(",") @@ -117,11 +119,14 @@ def combinations(a,b) new_array << new_string end end - puts new_array + return new_array end -if choice = "combinations" - puts "This method takes 2 lists of strings and returns a list of combinations made from them. Enter a list of strings separated by commas with no spaces. Like this: on, to, my" + +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 @@ -159,24 +164,32 @@ def combinations(a,b) =end def rectangle_overlap (*c) - a1_x = [*c[0], *c[1]]. #Box 1 bottom left corner X coordinate. - a1_y = [*c[2], *c[3]] - b1_x = [*c[4], *c[5]] - b1_y = [*c[6], *c[7]] - - a2_x = [*c[8], *c[9]]. #Box 2 bottom left corner X coordinate. - a2_y = [*c[10], *c[11]] - b2_x = [*c[12], *c[13]] - b2_y = [*c[14], *c[15]] - - if - - -return a1 + 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 -puts rectangle_overlap "0", "0", "3", "3", "1", "1", "4", "5", "0", "0", "1", "4", "1", "1", "3", "2" - +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 @@ -202,3 +215,39 @@ def rectangle_overlap (*c) 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 \ No newline at end of file