Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions combinations.rb
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions countinggame.rb
Original file line number Diff line number Diff line change
@@ -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)






12 changes: 12 additions & 0 deletions factorial.rb
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions overlap.rb
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions power.rb
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions primes.rb
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions uniques.rb
Original file line number Diff line number Diff line change
@@ -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)