diff --git a/README.md b/README.md index 29e36a8..5bdd0e7 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,5 @@ 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) + +Tingting Wang \ No newline at end of file diff --git a/combinations.rb b/combinations.rb new file mode 100644 index 0000000..e62b977 --- /dev/null +++ b/combinations.rb @@ -0,0 +1,16 @@ +#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. + +def combinations(array1, array2) + combined = [] + array1.each do |x| + array2.each do |y| + combined << x + y + end + end + puts combined +end + +array1 = ["on", "in"] +array2 = ["to", "rope", "something"] + +combinations(array1, array2) \ No newline at end of file diff --git a/countinggame.rb b/countinggame.rb new file mode 100644 index 0000000..c2a94fe --- /dev/null +++ b/countinggame.rb @@ -0,0 +1,33 @@ +def countinggame(number_of_players, number) + current_player = 1 + current_number = 1 + direction = 1 + + while current_number < number + + current_number += 1 + current_player += 1*direction + if current_number % 7 == 0 + direction *= -1 + elsif current_number % 11 == 0 + current_player += 1*direction + end + + if current_player > number_of_players + current_player = (current_player - number_of_players) + elsif current_player < 1 + current_player = (current_player + number_of_players) + end + + end + +puts current_player +end + +countinggame(10, 100) + + + + + + diff --git a/factorial.rb b/factorial.rb new file mode 100644 index 0000000..7e0e0ac --- /dev/null +++ b/factorial.rb @@ -0,0 +1,12 @@ +#Write a method factorial which takes a number and returns the product of every number up to the current number multiplied together. + +def factorial n + product = 1 + while n > 0 + product = product * n + n -= 1 + end + puts product +end + +factorial(77) \ No newline at end of file diff --git a/overlap.rb b/overlap.rb new file mode 100644 index 0000000..2023564 --- /dev/null +++ b/overlap.rb @@ -0,0 +1,31 @@ +#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. + +def overlap(rectangle1, rectangle2) + +horizontal_overlap = false +vertical_overlap = false + + (rectangle1[0][0]...rectangle1[1][0]).each do |x| + if (rectangle2[0][0]...rectangle2[1][0]).include?(x) + horizontal_overlap = true + end + end + + (rectangle1[0][1]...rectangle1[1][1]).each do |y| + if (rectangle2[0][1]...rectangle2[1][1]).include?(y) + vertical_overlap = true + end + end + + if horizontal_overlap == true && vertical_overlap == true + puts "overlap" + else + puts "no overlap" + end + +end + +rectangle1 = [[0,0],[3,3]] +rectangle2 = [[1,1],[4,5]] + +overlap(rectangle1, rectangle2) \ No newline at end of file diff --git a/power.rb b/power.rb new file mode 100644 index 0000000..cc800a9 --- /dev/null +++ b/power.rb @@ -0,0 +1,12 @@ +#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! + +def power(base, exponent) + base_product = 1 + while exponent > 0 + base_product *= base + exponent -= 1 + end + puts base_product +end + +puts power(4, 0) \ No newline at end of file diff --git a/primes.rb b/primes.rb new file mode 100644 index 0000000..cf9cdb5 --- /dev/null +++ b/primes.rb @@ -0,0 +1,14 @@ +#Write a method is_prime? which takes in a number and returns true if it is a prime number. + +def is_prime?(n) + (2...n).each do |x| + if n % x == 0 + false + end + end + puts "true" + true +end + + +is_prime?(7) \ No newline at end of file diff --git a/uniques.rb b/uniques.rb new file mode 100644 index 0000000..b6c3db0 --- /dev/null +++ b/uniques.rb @@ -0,0 +1,13 @@ +#Write a method uniques which takes an array of items and returns the array without any duplicates. Don’t use Ruby’s uniq method! + +def uniques(array) + unique_array = [] + array.each do |x| + unique_array << x unless unique_array.include?(x) + end + puts unique_array +end + +random = [1, 2, 3, "dog", 3, 2, 1, 3, 5, 6, 7, 8, 8, 8] + +uniques(random) \ No newline at end of file